diff --git a/stacktach/dbapi.py b/stacktach/dbapi.py index 60d07cf..1d0cc4d 100644 --- a/stacktach/dbapi.py +++ b/stacktach/dbapi.py @@ -121,27 +121,48 @@ def _deletes_model_factory(service): if service == 'glance': return {'klass': models.ImageDeletes, 'order_by': 'deleted_at'} - @api_call def list_usage_launches(request): - service = request.GET.get('service', 'nova') + return {'launches': list_usage_launches_with_service(request, 'nova')} + +@api_call +def list_usage_images(request): + return { 'images': list_usage_launches_with_service(request, 'glance')} + + +def list_usage_launches_with_service(request, service): model = _usage_model_factory(service) objects = get_db_objects(model['klass'], request, model['order_by']) dicts = _convert_model_list(objects) - return {'launches': dicts} + return dicts +def get_usage_launch_with_service(launch_id, service): + model = _usage_model_factory(service) + return {'launch': _get_model_by_id(model['klass'], launch_id)} + @api_call def get_usage_launch(request, launch_id): - service = request.GET.get('service', 'nova') - model = _usage_model_factory(service) - return {'launch': _get_model_by_id(model['klass'], launch_id)} + return get_usage_launch_with_service(launch_id, 'nova') + + +@api_call +def get_usage_image(request, image_id): + return get_usage_launch_with_service(image_id, 'glance') @api_call def list_usage_deletes(request): - service = request.GET.get('service', 'nova') + return list_usage_deletes_with_service(request, 'nova') + + +@api_call +def list_usage_deletes_glance(request): + return list_usage_deletes_with_service(request, 'glance') + + +def list_usage_deletes_with_service(request, service): model = _deletes_model_factory(service) objects = get_db_objects(model['klass'], request, model['order_by']) @@ -151,8 +172,13 @@ def list_usage_deletes(request): @api_call def get_usage_delete(request, delete_id): - service = request.GET.get('service', 'nova') - model = _deletes_model_factory(service) + model = _deletes_model_factory('nova') + return {'delete': _get_model_by_id(model['klass'], delete_id)} + + +@api_call +def get_usage_delete_glance(request, delete_id): + model = _deletes_model_factory('glance') return {'delete': _get_model_by_id(model['klass'], delete_id)} @@ -163,7 +189,15 @@ def _exists_extra_values(exist): @api_call def list_usage_exists(request): - service = request.GET.get('service', 'nova') + return list_usage_exists_with_service(request, 'nova') + + +@api_call +def list_usage_exists_glance(request): + return list_usage_exists_with_service(request, 'glance') + + +def list_usage_exists_with_service(request, service): model = _exists_model_factory(service) try: custom_filters = {} @@ -192,6 +226,11 @@ def get_usage_exist(request, exist_id): return {'exist': _get_model_by_id(models.InstanceExists, exist_id, _exists_extra_values)} +@api_call +def get_usage_exist_glance(request, exist_id): + return {'exist': _get_model_by_id(models.ImageExists, exist_id, + _exists_extra_values)} + @api_call def exists_send_status(request, message_id): diff --git a/stacktach/models.py b/stacktach/models.py index 2666e14..6cb930a 100644 --- a/stacktach/models.py +++ b/stacktach/models.py @@ -437,10 +437,6 @@ class ImageUsage(models.Model): size = models.BigIntegerField(max_length=20) last_raw = models.ForeignKey(GlanceRawData, null=True) - @property - def launched_at(self): - return self.created_at - class ImageDeletes(models.Model): uuid = models.CharField(max_length=50, db_index=True) diff --git a/stacktach/urls.py b/stacktach/urls.py index 8503787..5579957 100644 --- a/stacktach/urls.py +++ b/stacktach/urls.py @@ -36,15 +36,37 @@ urlpatterns = patterns('', url(r'db/usage/launches/$', 'stacktach.dbapi.list_usage_launches'), + url(r'db/usage/nova/launches/$', + 'stacktach.dbapi.list_usage_launches'), + url(r'db/usage/glance/images/$', + 'stacktach.dbapi.list_usage_images'), url(r'db/usage/launches/(?P\d+)/$', 'stacktach.dbapi.get_usage_launch'), + url(r'db/usage/nova/launches/(?P\d+)/$', + 'stacktach.dbapi.get_usage_launch'), + url(r'db/usage/glance/images/(?P\d+)/$', + 'stacktach.dbapi.get_usage_image'), url(r'db/usage/deletes/$', 'stacktach.dbapi.list_usage_deletes'), + url(r'db/usage/nova/deletes/$', + 'stacktach.dbapi.list_usage_deletes'), + url(r'db/usage/glance/deletes/$', + 'stacktach.dbapi.list_usage_deletes_glance'), url(r'db/usage/deletes/(?P\d+)/$', 'stacktach.dbapi.get_usage_delete'), + url(r'db/usage/nova/deletes/(?P\d+)/$', + 'stacktach.dbapi.get_usage_delete'), + url(r'db/usage/glance/deletes/(?P\d+)/$', + 'stacktach.dbapi.get_usage_delete_glance'), url(r'db/usage/exists/$', 'stacktach.dbapi.list_usage_exists'), + url(r'db/usage/nova/exists/$', 'stacktach.dbapi.list_usage_exists'), + url(r'db/usage/glance/exists/$', 'stacktach.dbapi.list_usage_exists_glance'), url(r'db/usage/exists/(?P\d+)/$', 'stacktach.dbapi.get_usage_exist'), + url(r'db/usage/nova/exists/(?P\d+)/$', + 'stacktach.dbapi.get_usage_exist'), + url(r'db/usage/glance/exists/(?P\d+)/$', + 'stacktach.dbapi.get_usage_exist_glance'), url(r'db/confirm/usage/exists/(?P[\w\-]+)/$', 'stacktach.dbapi.exists_send_status'), diff --git a/tests/unit/test_dbapi.py b/tests/unit/test_dbapi.py index 8c539c6..4b335a5 100644 --- a/tests/unit/test_dbapi.py +++ b/tests/unit/test_dbapi.py @@ -328,10 +328,10 @@ class DBAPITestCase(StacktachBaseTestCase): def test_list_usage_exists_no_custom_filters_for_nova(self): fake_request = self.mox.CreateMockAnything() - fake_request.GET = {'service': 'glance'} + fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') objects = self.mox.CreateMockAnything() - dbapi.get_db_objects(models.ImageExists, fake_request, 'id', + dbapi.get_db_objects(models.InstanceExists, fake_request, 'id', custom_filters={}).AndReturn(objects) self.mox.StubOutWithMock(dbapi, '_convert_model_list') dbapi._convert_model_list(objects, dbapi._exists_extra_values) @@ -342,7 +342,7 @@ class DBAPITestCase(StacktachBaseTestCase): def test_list_usage_exists_no_custom_filters_for_glance(self): fake_request = self.mox.CreateMockAnything() - fake_request.GET = {'service': 'glance'} + fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') objects = self.mox.CreateMockAnything() dbapi.get_db_objects(models.ImageExists, fake_request, 'id', @@ -350,7 +350,7 @@ class DBAPITestCase(StacktachBaseTestCase): self.mox.StubOutWithMock(dbapi, '_convert_model_list') dbapi._convert_model_list(objects, dbapi._exists_extra_values) self.mox.ReplayAll() - resp = dbapi.list_usage_exists(fake_request) + resp = dbapi.list_usage_exists_glance(fake_request) self.assertEqual(resp.status_code, 200) self.mox.VerifyAll() @@ -770,7 +770,7 @@ class DBAPITestCase(StacktachBaseTestCase): def test_list_usage_launches_for_glance(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'glance'} + fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') mock_objects = self.mox.CreateMockAnything() launches = {'a': 1} @@ -779,15 +779,15 @@ class DBAPITestCase(StacktachBaseTestCase): dbapi.get_db_objects(models.ImageUsage, fake_request, 'created_at').AndReturn(mock_objects) self.mox.ReplayAll() - resp = dbapi.list_usage_launches(fake_request) + resp = dbapi.list_usage_images(fake_request) self.assertEqual(resp.status_code, 200) - self.assertEqual(json.loads(resp.content), {'launches': launches}) + self.assertEqual(json.loads(resp.content), {'images': launches}) self.mox.VerifyAll() def test_list_usage_launches_for_nova(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'nova'} + fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') mock_objects = self.mox.CreateMockAnything() launches = {'a': 1} @@ -818,7 +818,7 @@ class DBAPITestCase(StacktachBaseTestCase): def test_get_usage_launch_for_nova(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'nova'} + fake_request.GET = {} launch = {'a': 1} self.mox.StubOutWithMock(dbapi, '_get_model_by_id') dbapi._get_model_by_id(models.InstanceUsage, 1).AndReturn(launch) @@ -832,13 +832,13 @@ class DBAPITestCase(StacktachBaseTestCase): def test_get_usage_launch_for_glance(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'glance'} + fake_request.GET = {} launch = {'a': 1} self.mox.StubOutWithMock(dbapi, '_get_model_by_id') dbapi._get_model_by_id(models.ImageUsage, 1).AndReturn(launch) self.mox.ReplayAll() - resp = dbapi.get_usage_launch(fake_request, 1) + resp = dbapi.get_usage_image(fake_request, 1) self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.content), {'launch': {'a': 1}}) self.mox.VerifyAll() @@ -846,7 +846,7 @@ class DBAPITestCase(StacktachBaseTestCase): def test_get_usage_delete_for_nova(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'nova'} + fake_request.GET = {} delete = {'a': 1} self.mox.StubOutWithMock(dbapi, '_get_model_by_id') dbapi._get_model_by_id(models.InstanceDeletes, 1).AndReturn(delete) @@ -860,13 +860,13 @@ class DBAPITestCase(StacktachBaseTestCase): def test_get_usage_delete_for_glance(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'glance'} + fake_request.GET = {} delete = {'a': 1} self.mox.StubOutWithMock(dbapi, '_get_model_by_id') dbapi._get_model_by_id(models.ImageDeletes, 1).AndReturn(delete) self.mox.ReplayAll() - resp = dbapi.get_usage_delete(fake_request, 1) + resp = dbapi.get_usage_delete_glance(fake_request, 1) self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.content), {'delete': {'a': 1}}) self.mox.VerifyAll() @@ -891,7 +891,7 @@ class DBAPITestCase(StacktachBaseTestCase): def test_list_usage_deletes_for_nova(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'nova'} + fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') mock_objects = self.mox.CreateMockAnything() deletes = {'a': 1} @@ -908,7 +908,7 @@ class DBAPITestCase(StacktachBaseTestCase): def test_list_usage_deletes_for_glance(self): fake_request = self.mox.CreateMockAnything() fake_request.method = 'GET' - fake_request.GET = {'service': 'glance'} + fake_request.GET = {} self.mox.StubOutWithMock(dbapi, 'get_db_objects') mock_objects = self.mox.CreateMockAnything() deletes = {'a': 1} @@ -917,7 +917,7 @@ class DBAPITestCase(StacktachBaseTestCase): dbapi.get_db_objects(models.ImageDeletes, fake_request, 'deleted_at').AndReturn(mock_objects) self.mox.ReplayAll() - resp = dbapi.list_usage_deletes(fake_request) + resp = dbapi.list_usage_deletes_glance(fake_request) self.assertEqual(resp.status_code, 200) self.assertEqual(json.loads(resp.content), {'deletes': deletes}) self.mox.VerifyAll()