From 1515caaba8b96c330ca6372eb812bbd93e5f0349 Mon Sep 17 00:00:00 2001 From: Justin Pomeroy Date: Thu, 10 Dec 2015 14:17:01 -0600 Subject: [PATCH] Add getServers function to nova rest API This adds the getServers function to the nova rest API module so that new angular code can use it. Partially-Implements: blueprint angularize-instances-table Change-Id: I2be7e37f0065bda66fbacbb1f9898551819f45c8 --- openstack_dashboard/api/rest/nova.py | 13 +++++++++++++ .../core/openstack-service-api/nova.service.js | 16 ++++++++++++++++ .../openstack-service-api/nova.service.spec.js | 6 ++++++ .../test/api_tests/nova_rest_tests.py | 14 ++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/openstack_dashboard/api/rest/nova.py b/openstack_dashboard/api/rest/nova.py index 66946ba04..855a08bbd 100644 --- a/openstack_dashboard/api/rest/nova.py +++ b/openstack_dashboard/api/rest/nova.py @@ -124,6 +124,19 @@ class Servers(generic.View): 'config_drive' ] + @rest_utils.ajax() + def get(self, request): + """Get a list of servers. + + The listing result is an object with property "items". Each item is + a server. + + Example GET: + http://localhost/api/nova/servers + """ + servers = api.nova.server_list(request)[0] + return {'items': [s.to_dict() for s in servers]} + @rest_utils.ajax(data_required=True) def post(self, request): """Create a server. diff --git a/openstack_dashboard/static/app/core/openstack-service-api/nova.service.js b/openstack_dashboard/static/app/core/openstack-service-api/nova.service.js index 671a5e148..34ae3a022 100644 --- a/openstack_dashboard/static/app/core/openstack-service-api/nova.service.js +++ b/openstack_dashboard/static/app/core/openstack-service-api/nova.service.js @@ -39,6 +39,7 @@ getLimits: getLimits, createServer: createServer, getServer: getServer, + getServers: getServers, getExtensions: getExtensions, getFlavors: getFlavors, getFlavor: getFlavor, @@ -191,6 +192,21 @@ }); } + /** + * @name horizon.app.core.openstack-service-api.nova.getServers + * @description + * Get a list of servers. + * + * The listing result is an object with property "items". Each item is + * a server. + */ + function getServers() { + return apiService.get('/api/nova/servers/') + .error(function () { + toastService.add('error', gettext('Unable to retrieve instances.')); + }); + } + /** * @name horizon.app.core.openstack-service-api.nova.getExtensions * @description diff --git a/openstack_dashboard/static/app/core/openstack-service-api/nova.service.spec.js b/openstack_dashboard/static/app/core/openstack-service-api/nova.service.spec.js index bab3d46b2..3ee933d26 100644 --- a/openstack_dashboard/static/app/core/openstack-service-api/nova.service.spec.js +++ b/openstack_dashboard/static/app/core/openstack-service-api/nova.service.spec.js @@ -101,6 +101,12 @@ 42 ] }, + { + "func": "getServers", + "method": "get", + "path": "/api/nova/servers/", + "error": "Unable to retrieve instances." + }, { "func": "getExtensions", "method": "get", diff --git a/openstack_dashboard/test/api_tests/nova_rest_tests.py b/openstack_dashboard/test/api_tests/nova_rest_tests.py index 47d0b5e2a..fec6b098d 100644 --- a/openstack_dashboard/test/api_tests/nova_rest_tests.py +++ b/openstack_dashboard/test/api_tests/nova_rest_tests.py @@ -143,6 +143,20 @@ class NovaRestTestCase(test.TestCase): [{'name': 'root'}] ) + @mock.patch.object(nova.api, 'nova') + def test_server_list(self, nc): + request = self.mock_rest_request() + nc.server_list.return_value = ([ + mock.Mock(**{'to_dict.return_value': {'id': 'one'}}), + mock.Mock(**{'to_dict.return_value': {'id': 'two'}}), + ], False) + + response = nova.Servers().get(request) + self.assertStatusCode(response, 200) + self.assertEqual(response.json, + {'items': [{'id': 'one'}, {'id': 'two'}]}) + nc.server_list.assert_called_once_with(request) + @mock.patch.object(nova.api, 'nova') def test_server_get_single(self, nc): request = self.mock_rest_request()