def test_send_instance_update_notification_disabled(self, mock_enabled, mock_info): """Tests the case that notifications are disabled which makes send_instance_update_notification a noop. """ base.send_instance_update_notification(mock.sentinel.ctxt, mock.sentinel.instance)
def test_send_instance_update_notification_disabled(self, mock_enabled, mock_info): """Tests the case that notifications are disabled which makes send_instance_update_notification a noop. """ base.send_instance_update_notification(mock.sentinel.ctxt, mock.sentinel.instance)
def update(self, req, server_id, id, body): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'update') im = _get_instance_mapping(context, server_id) with nova_context.target_cell(context, im.cell_mapping) as cctxt: instance = self._check_instance_in_valid_state( cctxt, server_id, 'update tag') try: jsonschema.validate(id, parameter_types.tag) except jsonschema.ValidationError as e: msg = (_("Tag '%(tag)s' is invalid. It must be a non empty string " "without characters '/' and ','. Validation error " "message: %(err)s") % { 'tag': id, 'err': e.message }) raise webob.exc.HTTPBadRequest(explanation=msg) try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: tags = objects.TagList.get_by_resource_id(cctxt, server_id) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) if len(tags) >= objects.instance.MAX_TAG_COUNT: msg = (_("The number of tags exceeded the per-server limit %d") % objects.instance.MAX_TAG_COUNT) raise webob.exc.HTTPBadRequest(explanation=msg) if id in _get_tags_names(tags): # NOTE(snikitin): server already has specified tag return webob.Response(status_int=204) try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: tag = objects.Tag(context=cctxt, resource_id=server_id, tag=id) tag.create() instance.tags = objects.TagList.get_by_resource_id( cctxt, server_id) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) notifications_base.send_instance_update_notification( context, instance, service="nova-api") response = webob.Response(status_int=201) response.headers['Location'] = self._view_builder.get_location( req, server_id, id) return response
def test_send_legacy_instance_update_notification(self, mock_info, mock_get_notifier, mock_states, mock_bw): """Tests the case that versioned notifications are disabled and assert that this does not prevent sending the unversioned instance.update notification. """ self.flags(notification_format='unversioned', group='notifications') base.send_instance_update_notification(mock.sentinel.ctxt, mock.sentinel.instance) mock_get_notifier.return_value.info.assert_called_once_with( mock.sentinel.ctxt, 'compute.instance.update', mock.ANY)
def test_send_legacy_instance_update_notification(self, mock_info, mock_get_notifier, mock_states, mock_bw): """Tests the case that versioned notifications are disabled and assert that this does not prevent sending the unversioned instance.update notification. """ self.flags(notification_format='unversioned', group='notifications') base.send_instance_update_notification(mock.sentinel.ctxt, mock.sentinel.instance) mock_get_notifier.return_value.info.assert_called_once_with( mock.sentinel.ctxt, 'compute.instance.update', mock.ANY)
def update(self, req, server_id, id, body): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'update') im = _get_instance_mapping(context, server_id) with nova_context.target_cell(context, im.cell_mapping) as cctxt: instance = self._check_instance_in_valid_state( cctxt, server_id, 'update tag') try: jsonschema.validate(id, parameter_types.tag) except jsonschema.ValidationError as e: msg = (_("Tag '%(tag)s' is invalid. It must be a non empty string " "without characters '/' and ','. Validation error " "message: %(err)s") % {'tag': id, 'err': e.message}) raise webob.exc.HTTPBadRequest(explanation=msg) try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: tags = objects.TagList.get_by_resource_id(cctxt, server_id) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) if len(tags) >= objects.instance.MAX_TAG_COUNT: msg = (_("The number of tags exceeded the per-server limit %d") % objects.instance.MAX_TAG_COUNT) raise webob.exc.HTTPBadRequest(explanation=msg) if id in _get_tags_names(tags): # NOTE(snikitin): server already has specified tag return webob.Response(status_int=204) try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: tag = objects.Tag(context=cctxt, resource_id=server_id, tag=id) tag.create() instance.tags = objects.TagList.get_by_resource_id(cctxt, server_id) except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) notifications_base.send_instance_update_notification( context, instance, service="nova-api") response = webob.Response(status_int=201) response.headers['Location'] = self._view_builder.get_location( req, server_id, id) return response
def delete_all(self, req, server_id): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'delete_all') im = _get_instance_mapping(context, server_id) with nova_context.target_cell(context, im.cell_mapping) as cctxt: instance = self._check_instance_in_valid_state( cctxt, server_id, 'delete tags') try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: objects.TagList.destroy(cctxt, server_id) instance.tags = objects.TagList() except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) notifications_base.send_instance_update_notification( context, instance, service="nova-api")
def delete_all(self, req, server_id): context = req.environ["nova.context"] context.can(st_policies.POLICY_ROOT % 'delete_all') im = _get_instance_mapping(context, server_id) with nova_context.target_cell(context, im.cell_mapping) as cctxt: instance = self._check_instance_in_valid_state( cctxt, server_id, 'delete tags') try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: objects.TagList.destroy(cctxt, server_id) instance.tags = objects.TagList() except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) notifications_base.send_instance_update_notification( context, instance, service="nova-api")
def update_all(self, req, server_id, body): context = req.environ["nova.context"] im = _get_instance_mapping(context, server_id) context.can(st_policies.POLICY_ROOT % 'update_all', target={'project_id': im.project_id}) with nova_context.target_cell(context, im.cell_mapping) as cctxt: instance = self._check_instance_in_valid_state( cctxt, server_id, 'update tags') try: with nova_context.target_cell(context, im.cell_mapping) as cctxt: tags = objects.TagList.create(cctxt, server_id, body['tags']) instance.tags = tags except exception.InstanceNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) notifications_base.send_instance_update_notification( context, instance, service="nova-api") return {'tags': _get_tags_names(tags)}