From 2e240d70d3598a63f473edd8f1e6da268090f359 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Thu, 2 Mar 2017 13:29:18 -0800 Subject: [PATCH] Enlighten server tags API about cells Related to blueprint cells-aware-api Change-Id: Iff71a1bc1762f864d93bace974ad36136347dfdf --- nova/api/openstack/compute/server_tags.py | 64 +++++++++++++++---- .../api/openstack/compute/test_server_tags.py | 7 ++ 2 files changed, 58 insertions(+), 13 deletions(-) diff --git a/nova/api/openstack/compute/server_tags.py b/nova/api/openstack/compute/server_tags.py index 521c2affa83d..f2572ac02a39 100644 --- a/nova/api/openstack/compute/server_tags.py +++ b/nova/api/openstack/compute/server_tags.py @@ -22,6 +22,7 @@ from nova.api import validation from nova.api.validation import parameter_types from nova import compute from nova.compute import vm_states +from nova import context as nova_context from nova import exception from nova.i18n import _ from nova import objects @@ -35,6 +36,14 @@ def _get_tags_names(tags): return [t.tag for t in tags] +def _get_instance_mapping(context, server_id): + try: + return objects.InstanceMapping.get_by_instance_uuid(context, + server_id) + except exception.InstanceMappingNotFound as e: + raise webob.exc.HTTPNotFound(explanation=e.format_message()) + + class ServerTagsController(wsgi.Controller): _view_builder_class = server_tags.ViewBuilder @@ -61,8 +70,12 @@ class ServerTagsController(wsgi.Controller): context.can(st_policies.POLICY_ROOT % 'show') try: - exists = objects.Tag.exists(context, server_id, id) - except exception.InstanceNotFound as e: + im = objects.InstanceMapping.get_by_instance_uuid(context, + server_id) + with nova_context.target_cell(context, im.cell_mapping): + exists = objects.Tag.exists(context, server_id, id) + except (exception.InstanceNotFound, + exception.InstanceMappingNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) if not exists: @@ -77,8 +90,12 @@ class ServerTagsController(wsgi.Controller): context.can(st_policies.POLICY_ROOT % 'index') try: - tags = objects.TagList.get_by_resource_id(context, server_id) - except exception.InstanceNotFound as e: + im = objects.InstanceMapping.get_by_instance_uuid(context, + server_id) + with nova_context.target_cell(context, im.cell_mapping): + tags = objects.TagList.get_by_resource_id(context, server_id) + except (exception.InstanceNotFound, + exception.InstanceMappingNotFound) as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) return {'tags': _get_tags_names(tags)} @@ -89,7 +106,11 @@ class ServerTagsController(wsgi.Controller): def update(self, req, server_id, id, body): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'update') - self._check_instance_in_valid_state(context, server_id, 'update tag') + im = _get_instance_mapping(context, server_id) + + with nova_context.target_cell(context, im.cell_mapping): + self._check_instance_in_valid_state(context, server_id, + 'update tag') try: jsonschema.validate(id, parameter_types.tag) @@ -100,7 +121,8 @@ class ServerTagsController(wsgi.Controller): raise webob.exc.HTTPBadRequest(explanation=msg) try: - tags = objects.TagList.get_by_resource_id(context, server_id) + with nova_context.target_cell(context, im.cell_mapping): + tags = objects.TagList.get_by_resource_id(context, server_id) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) @@ -116,7 +138,8 @@ class ServerTagsController(wsgi.Controller): tag = objects.Tag(context=context, resource_id=server_id, tag=id) try: - tag.create() + with nova_context.target_cell(context, im.cell_mapping): + tag.create() except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) @@ -131,10 +154,15 @@ class ServerTagsController(wsgi.Controller): def update_all(self, req, server_id, body): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'update_all') - self._check_instance_in_valid_state(context, server_id, 'update tags') + im = _get_instance_mapping(context, server_id) + + with nova_context.target_cell(context, im.cell_mapping): + self._check_instance_in_valid_state(context, server_id, + 'update tags') try: - tags = objects.TagList.create(context, server_id, body['tags']) + with nova_context.target_cell(context, im.cell_mapping): + tags = objects.TagList.create(context, server_id, body['tags']) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) @@ -146,10 +174,15 @@ class ServerTagsController(wsgi.Controller): def delete(self, req, server_id, id): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'delete') - self._check_instance_in_valid_state(context, server_id, 'delete tag') + im = _get_instance_mapping(context, server_id) + + with nova_context.target_cell(context, im.cell_mapping): + self._check_instance_in_valid_state(context, server_id, + 'delete tag') try: - objects.Tag.destroy(context, server_id, id) + with nova_context.target_cell(context, im.cell_mapping): + objects.Tag.destroy(context, server_id, id) except exception.InstanceTagNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceNotFound as e: @@ -161,10 +194,15 @@ class ServerTagsController(wsgi.Controller): def delete_all(self, req, server_id): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'delete_all') - self._check_instance_in_valid_state(context, server_id, 'delete tags') + im = _get_instance_mapping(context, server_id) + + with nova_context.target_cell(context, im.cell_mapping): + self._check_instance_in_valid_state(context, server_id, + 'delete tags') try: - objects.TagList.destroy(context, server_id) + with nova_context.target_cell(context, im.cell_mapping): + objects.TagList.destroy(context, server_id) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) diff --git a/nova/tests/unit/api/openstack/compute/test_server_tags.py b/nova/tests/unit/api/openstack/compute/test_server_tags.py index 28d3fab54cf3..893224074b3d 100644 --- a/nova/tests/unit/api/openstack/compute/test_server_tags.py +++ b/nova/tests/unit/api/openstack/compute/test_server_tags.py @@ -17,8 +17,10 @@ from nova.api.openstack.compute import extension_info from nova.api.openstack.compute import server_tags from nova.api.openstack.compute import servers from nova.compute import vm_states +from nova import context from nova.db.sqlalchemy import models from nova import exception +from nova import objects from nova.objects import instance from nova.objects import tag as tag_obj from nova import test @@ -49,6 +51,11 @@ class ServerTagsTest(test.TestCase): def setUp(self): super(ServerTagsTest, self).setUp() self.controller = server_tags.ServerTagsController() + inst_map = objects.InstanceMapping( + cell_mapping=objects.CellMappingList.get_all( + context.get_admin_context())[1]) + self.stub_out('nova.objects.InstanceMapping.get_by_instance_uuid', + lambda s, c, u: inst_map) def _get_tag(self, tag_name): tag = models.Tag()