From 74390d4920e692e7c85462232a6256774c6eabae Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 22 Dec 2011 00:46:09 -0500 Subject: Move 'actions' subresource into extension Related to blueprint separate-nova-adminapi Change-Id: I6929c5c467f339bc91ac8185d6217a55a5369a58 --- nova/api/openstack/v2/__init__.py | 7 +-- .../api/openstack/v2/contrib/server_action_list.py | 61 ++++++++++++++++++++ nova/api/openstack/v2/servers.py | 15 ----- .../v2/contrib/test_server_action_list.py | 65 ++++++++++++++++++++++ nova/tests/api/openstack/v2/test_extensions.py | 1 + nova/tests/api/openstack/v2/test_servers.py | 36 ------------ 6 files changed, 128 insertions(+), 57 deletions(-) create mode 100644 nova/api/openstack/v2/contrib/server_action_list.py create mode 100644 nova/tests/api/openstack/v2/contrib/test_server_action_list.py diff --git a/nova/api/openstack/v2/__init__.py b/nova/api/openstack/v2/__init__.py index 8a574ffa5..82a8764a0 100644 --- a/nova/api/openstack/v2/__init__.py +++ b/nova/api/openstack/v2/__init__.py @@ -105,7 +105,6 @@ class APIRouter(base_wsgi.Router): if ext_mgr is None: ext_mgr = extensions.ExtensionManager() - self.server_members = {} mapper = ProjectMapper() self._setup_routes(mapper) self._setup_ext_routes(mapper, ext_mgr) @@ -133,13 +132,9 @@ class APIRouter(base_wsgi.Router): mapper.resource(resource.collection, resource.collection, **kargs) def _setup_routes(self, mapper): - server_members = self.server_members - server_members['action'] = 'POST' if FLAGS.allow_admin_api: LOG.debug(_("Including admin operations in API.")) - server_members['actions'] = 'GET' - mapper.resource("user", "users", controller=users.create_resource(), collection={'detail': 'GET'}) @@ -168,7 +163,7 @@ class APIRouter(base_wsgi.Router): mapper.resource("server", "servers", controller=servers.create_resource(), collection={'detail': 'GET'}, - member=self.server_members) + member={'action': 'POST'}) mapper.resource("ip", "ips", controller=ips.create_resource(), parent_resource=dict(member_name='server', diff --git a/nova/api/openstack/v2/contrib/server_action_list.py b/nova/api/openstack/v2/contrib/server_action_list.py new file mode 100644 index 000000000..62f21dbbf --- /dev/null +++ b/nova/api/openstack/v2/contrib/server_action_list.py @@ -0,0 +1,61 @@ +# Copyright 2011 OpenStack LLC. +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import webob.exc + +from nova.api.openstack.v2 import extensions +from nova import compute +from nova import exception + + +class ServerActionListController(object): + + def index(self, req, server_id): + context = req.environ["nova.context"] + compute_api = compute.API() + + try: + instance = compute_api.get(context, server_id) + except exception.NotFound: + raise webob.exc.HTTPNotFound(_("Instance not found")) + + items = compute_api.get_actions(context, instance) + + def _format_item(item): + return { + 'created_at': str(item['created_at']), + 'action': item['action'], + 'error': item['error'], + } + + return {'actions': [_format_item(item) for item in items]} + + +class Server_action_list(extensions.ExtensionDescriptor): + """Allow Admins to view pending server actions""" + + name = "ServerActionList" + alias = "os-server-action-list" + namespace = "http://docs.openstack.org/ext/server-actions-list/api/v1.1" + updated = "2011-12-21T00:00:00+00:00" + admin_only = True + + def get_resources(self): + parent_def = {'member_name': 'server', 'collection_name': 'servers'} + #NOTE(bcwaldon): This should be prefixed with 'os-' + ext = extensions.ResourceExtension('actions', + ServerActionListController(), + parent=parent_def) + return [ext] diff --git a/nova/api/openstack/v2/servers.py b/nova/api/openstack/v2/servers.py index 51ccaadde..dda6e6e96 100644 --- a/nova/api/openstack/v2/servers.py +++ b/nova/api/openstack/v2/servers.py @@ -627,21 +627,6 @@ class Controller(wsgi.Controller): raise exc.HTTPUnprocessableEntity() return webob.Response(status_int=202) - def actions(self, req, id): - """Permit Admins to retrieve server actions.""" - ctxt = req.environ["nova.context"] - instance = self._get_server(ctxt, id) - items = self.compute_api.get_actions(ctxt, instance) - actions = [] - # TODO(jk0): Do not do pre-serialization here once the default - # serializer is updated - for item in items: - actions.append(dict( - created_at=str(item.created_at), - action=item.action, - error=item.error)) - return dict(actions=actions) - def _resize(self, req, instance_id, flavor_id): """Begin the resize process with given instance/flavor.""" context = req.environ["nova.context"] diff --git a/nova/tests/api/openstack/v2/contrib/test_server_action_list.py b/nova/tests/api/openstack/v2/contrib/test_server_action_list.py new file mode 100644 index 000000000..c6a185e56 --- /dev/null +++ b/nova/tests/api/openstack/v2/contrib/test_server_action_list.py @@ -0,0 +1,65 @@ +# Copyright 2011 Eldar Nugaev +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import datetime +import json + +from nova.api.openstack import v2 +from nova.api.openstack.v2 import extensions +from nova.api.openstack import wsgi +import nova.compute +from nova import test +from nova.tests.api.openstack import fakes +import nova.utils + + +dt = datetime.datetime.utcnow() + + +def fake_get_actions(self, _context, instance_uuid): + return [ + {'action': 'rebuild', 'error': None, 'created_at': dt}, + {'action': 'reboot', 'error': 'Failed!', 'created_at': dt}, + ] + + +def fake_instance_get(self, _context, instance_uuid): + return {'uuid': instance_uuid} + + +class ServerDiagnosticsTest(test.TestCase): + + def setUp(self): + super(ServerDiagnosticsTest, self).setUp() + self.flags(allow_admin_api=True) + self.flags(verbose=True) + self.stubs.Set(nova.compute.API, 'get_actions', fake_get_actions) + self.stubs.Set(nova.compute.API, 'get', fake_instance_get) + self.compute_api = nova.compute.API() + + self.router = v2.APIRouter() + ext_middleware = extensions.ExtensionMiddleware(self.router) + self.app = wsgi.LazySerializationMiddleware(ext_middleware) + + def test_get_actions(self): + uuid = nova.utils.gen_uuid() + req = fakes.HTTPRequest.blank('/fake/servers/%s/actions' % uuid) + res = req.get_response(self.app) + output = json.loads(res.body) + expected = {'actions': [ + {'action': 'rebuild', 'error': None, 'created_at': str(dt)}, + {'action': 'reboot', 'error': 'Failed!', 'created_at': str(dt)}, + ]} + self.assertEqual(output, expected) diff --git a/nova/tests/api/openstack/v2/test_extensions.py b/nova/tests/api/openstack/v2/test_extensions.py index 5c5bf1adf..2063e6e2d 100644 --- a/nova/tests/api/openstack/v2/test_extensions.py +++ b/nova/tests/api/openstack/v2/test_extensions.py @@ -114,6 +114,7 @@ class ExtensionControllerTest(ExtensionTestCase): "Quotas", "Rescue", "SecurityGroups", + "ServerActionList", "ServerDiagnostics", "SimpleTenantUsage", "VSAs", diff --git a/nova/tests/api/openstack/v2/test_servers.py b/nova/tests/api/openstack/v2/test_servers.py index e29049ef7..03627c833 100644 --- a/nova/tests/api/openstack/v2/test_servers.py +++ b/nova/tests/api/openstack/v2/test_servers.py @@ -154,19 +154,6 @@ def fake_compute_api(cls, req, id): return True -_fake_compute_actions = [ - dict( - created_at=str(datetime.datetime(2010, 11, 11, 11, 0, 0)), - action='Fake Action', - error='Fake Error', - ) - ] - - -def fake_compute_actions(_1, _2, _3): - return [InstanceActions(**a) for a in _fake_compute_actions] - - def find_host(self, context, instance_id): return "nova" @@ -205,7 +192,6 @@ class ServersControllerTest(test.TestCase): instance_addresses) self.stubs.Set(nova.db, 'instance_get_floating_address', instance_addresses) - self.stubs.Set(nova.compute.API, "get_actions", fake_compute_actions) self.config_drive = None @@ -1167,28 +1153,6 @@ class ServersControllerTest(test.TestCase): self.assertEqual(s['status'], 'BUILD') self.assertEqual(s['metadata']['seq'], str(i)) - def test_server_actions(self): - req = fakes.HTTPRequest.blank( - "/v2/fake/servers/%s/actions" % FAKE_UUID) - res_dict = self.controller.actions(req, FAKE_UUID) - self.assertEqual(res_dict, {'actions': _fake_compute_actions}) - - def test_server_actions_after_reboot(self): - """ - Bug #897091 was this failure mode -- the /actions call failed if - /action had been called first. - """ - body = dict(reboot=dict(type="HARD")) - req = fakes.HTTPRequest.blank( - '/v2/fake/servers/%s/action' % FAKE_UUID) - # Assume the instance is in ACTIVE state before calling reboot - self.stubs.Set(nova.db, 'instance_get', - return_server_with_state(vm_states.ACTIVE)) - self.stubs.Set(nova.db, 'instance_get_by_uuid', - return_server_with_state(vm_states.ACTIVE)) - self.controller.action(req, FAKE_UUID, body) - self.test_server_actions() - def test_get_all_server_details_with_host(self): ''' We want to make sure that if two instances are on the same host, then -- cgit