Merge "Enlighten server tags API about cells"

This commit is contained in:
Jenkins
2017-03-08 03:04:16 +00:00
committed by Gerrit Code Review
2 changed files with 58 additions and 13 deletions

View File

@@ -22,6 +22,7 @@ from nova.api import validation
from nova.api.validation import parameter_types from nova.api.validation import parameter_types
from nova import compute from nova import compute
from nova.compute import vm_states from nova.compute import vm_states
from nova import context as nova_context
from nova import exception from nova import exception
from nova.i18n import _ from nova.i18n import _
from nova import objects from nova import objects
@@ -35,6 +36,14 @@ def _get_tags_names(tags):
return [t.tag for t in 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): class ServerTagsController(wsgi.Controller):
_view_builder_class = server_tags.ViewBuilder _view_builder_class = server_tags.ViewBuilder
@@ -61,8 +70,12 @@ class ServerTagsController(wsgi.Controller):
context.can(st_policies.POLICY_ROOT % 'show') context.can(st_policies.POLICY_ROOT % 'show')
try: try:
exists = objects.Tag.exists(context, server_id, id) im = objects.InstanceMapping.get_by_instance_uuid(context,
except exception.InstanceNotFound as e: 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()) raise webob.exc.HTTPNotFound(explanation=e.format_message())
if not exists: if not exists:
@@ -77,8 +90,12 @@ class ServerTagsController(wsgi.Controller):
context.can(st_policies.POLICY_ROOT % 'index') context.can(st_policies.POLICY_ROOT % 'index')
try: try:
tags = objects.TagList.get_by_resource_id(context, server_id) im = objects.InstanceMapping.get_by_instance_uuid(context,
except exception.InstanceNotFound as e: 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()) raise webob.exc.HTTPNotFound(explanation=e.format_message())
return {'tags': _get_tags_names(tags)} return {'tags': _get_tags_names(tags)}
@@ -89,7 +106,11 @@ class ServerTagsController(wsgi.Controller):
def update(self, req, server_id, id, body): def update(self, req, server_id, id, body):
context = req.environ["nova.context"] context = req.environ["nova.context"]
context.can(st_policies.POLICY_ROOT % 'update') 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: try:
jsonschema.validate(id, parameter_types.tag) jsonschema.validate(id, parameter_types.tag)
@@ -100,7 +121,8 @@ class ServerTagsController(wsgi.Controller):
raise webob.exc.HTTPBadRequest(explanation=msg) raise webob.exc.HTTPBadRequest(explanation=msg)
try: 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: except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message()) 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) tag = objects.Tag(context=context, resource_id=server_id, tag=id)
try: try:
tag.create() with nova_context.target_cell(context, im.cell_mapping):
tag.create()
except exception.InstanceNotFound as e: except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message()) raise webob.exc.HTTPNotFound(explanation=e.format_message())
@@ -131,10 +154,15 @@ class ServerTagsController(wsgi.Controller):
def update_all(self, req, server_id, body): def update_all(self, req, server_id, body):
context = req.environ["nova.context"] context = req.environ["nova.context"]
context.can(st_policies.POLICY_ROOT % 'update_all') 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: 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: except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message()) raise webob.exc.HTTPNotFound(explanation=e.format_message())
@@ -146,10 +174,15 @@ class ServerTagsController(wsgi.Controller):
def delete(self, req, server_id, id): def delete(self, req, server_id, id):
context = req.environ["nova.context"] context = req.environ["nova.context"]
context.can(st_policies.POLICY_ROOT % 'delete') 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: 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: except exception.InstanceTagNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message()) raise webob.exc.HTTPNotFound(explanation=e.format_message())
except exception.InstanceNotFound as e: except exception.InstanceNotFound as e:
@@ -161,10 +194,15 @@ class ServerTagsController(wsgi.Controller):
def delete_all(self, req, server_id): def delete_all(self, req, server_id):
context = req.environ["nova.context"] context = req.environ["nova.context"]
context.can(st_policies.POLICY_ROOT % 'delete_all') 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: 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: except exception.InstanceNotFound as e:
raise webob.exc.HTTPNotFound(explanation=e.format_message()) raise webob.exc.HTTPNotFound(explanation=e.format_message())

View File

@@ -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 server_tags
from nova.api.openstack.compute import servers from nova.api.openstack.compute import servers
from nova.compute import vm_states from nova.compute import vm_states
from nova import context
from nova.db.sqlalchemy import models from nova.db.sqlalchemy import models
from nova import exception from nova import exception
from nova import objects
from nova.objects import instance from nova.objects import instance
from nova.objects import tag as tag_obj from nova.objects import tag as tag_obj
from nova import test from nova import test
@@ -49,6 +51,11 @@ class ServerTagsTest(test.TestCase):
def setUp(self): def setUp(self):
super(ServerTagsTest, self).setUp() super(ServerTagsTest, self).setUp()
self.controller = server_tags.ServerTagsController() 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): def _get_tag(self, tag_name):
tag = models.Tag() tag = models.Tag()