def test_create_backup_rotation_is_string_number(self): body = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': '1', }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, 'Backup 1', 'daily', 1, extra_properties={}).AndReturn(image) self.mox.ReplayAll() res = self.controller._create_backup(self.req, instance['uuid'], body=body) self.assertEqual(202, res.status_int) self.assertIn('fake-image-id', res.headers['Location'])
def test_create_backup_rotation_is_zero(self): # The happy path for creating backups if rotation is zero. body = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 0, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, 'Backup 1', 'daily', 0, extra_properties={}).AndReturn(image) self.mox.ReplayAll() res = self.controller._create_backup(self.req, instance.uuid, body=body) self.assertEqual(202, res.status_int) self.assertNotIn('Location', res.headers)
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] entity = body.get("createImage", {}) image_name = entity.get("name") if not image_name: msg = _("createImage entity requires name attribute") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): policy.enforce(context, 'compute:snapshot_volume_backed', {'project_id': context.project_id, 'user_id': context.user_id}) image = self.compute_api.snapshot_volume_backed( context, instance, image_name, extra_properties=props) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage', id) except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image['id']) url_prefix = self._view_builder._update_glance_link_prefix( req.application_url) image_ref = common.url_join(url_prefix, context.project_id, 'images', image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def test_backup_volume_backed_instance(self): body = { 'createBackup': { 'name': 'BackupMe', 'backup_type': 'daily', 'rotation': 3 }, } common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() instance.image_ref = None self.compute_api.backup(self.context, instance, 'BackupMe', 'daily', 3, extra_properties={}).AndRaise( exception.InvalidRequest()) self.mox.ReplayAll() self.assertRaises(webob.exc.HTTPBadRequest, self.controller._create_backup, self.req, instance['uuid'], body=body)
def update(self, req, image_id, id, body): context = req.environ['nova.context'] try: meta = body['meta'] except KeyError: expl = _('Incorrect request body format') raise exc.HTTPBadRequest(explanation=expl) if id not in meta: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) if len(meta) > 1: expl = _('Request body contains too many items') raise exc.HTTPBadRequest(explanation=expl) image = self._get_image(context, image_id) image['properties'][id] = meta[id] common.check_img_metadata_properties_quota(context, image['properties']) try: self.image_service.update(context, image_id, image, None) except exception.ImageNotAuthorized as e: raise exc.HTTPForbidden(explanation=str(e)) return dict(meta=meta)
def test_create_backup_rotation_is_positive(self): # The happy path for creating backups if rotation is positive. body = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 1, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get(objects=False) self.compute_api.backup(self.context, instance, 'Backup 1', 'daily', 1, extra_properties={}).AndReturn(image) self.mox.ReplayAll() res = self._make_request(self._make_url(instance['uuid']), body) self.assertEqual(202, res.status_int) self.assertIn('fake-image-id', res.headers['Location'])
def _create_backup(self, req, id, body): """Backup a server instance. Images now have an `image_type` associated with them, which can be 'snapshot' or the backup type, like 'daily' or 'weekly'. If the image_type is backup-like, then the rotation factor can be included and that will cause the oldest backups that exceed the rotation factor to be deleted. """ context = req.environ["nova.context"] context.can(cb_policies.BASE_POLICY_NAME) entity = body["createBackup"] image_name = common.normalize_name(entity["name"]) backup_type = entity["backup_type"] rotation = int(entity["rotation"]) props = {} metadata = entity.get('metadata', {}) # Starting from microversion 2.39 we don't check quotas on createBackup if api_version_request.is_supported(req, max_version=api_version_request. MAX_IMAGE_META_PROXY_API_VERSION): common.check_img_metadata_properties_quota(context, metadata) props.update(metadata) instance = common.get_instance(self.compute_api, context, id) try: image = self.compute_api.backup(context, instance, image_name, backup_type, rotation, extra_properties=props) except exception.InstanceUnknownCell as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state( state_error, 'createBackup', id) except exception.InvalidRequest as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) # Starting with microversion 2.45 we return a response body containing # the snapshot image id without the Location header. if api_version_request.is_supported(req, '2.45'): return {'image_id': image['id']} resp = webob.Response(status_int=202) # build location of newly-created image entity if rotation is not zero if rotation > 0: image_id = str(image['id']) image_ref = common.url_join(req.application_url, 'images', image_id) resp.headers['Location'] = image_ref return resp
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ["nova.context"] authorize(context, action="create_image") entity = body["createImage"] image_name = entity["name"] metadata = entity.get("metadata", {}) common.check_img_metadata_properties_quota(context, metadata) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(context, instance.uuid) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): authorize(context, action="create_image:allow_volume_backed") image = self.compute_api.snapshot_volume_backed( context, instance, image_name, extra_properties=metadata ) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=metadata) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, "createImage", id) except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image["id"]) image_ref = glance.generate_image_url(image_id) resp = webob.Response(status_int=202) resp.headers["Location"] = image_ref return resp
def test_backup_volume_backed_instance(self): body = { 'createBackup': { 'name': 'BackupMe', 'backup_type': 'daily', 'rotation': 3 }, } common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() instance.image_ref = None self.compute_api.backup(self.context, instance, 'BackupMe', 'daily', 3, extra_properties={}).AndRaise( exception.InvalidRequest()) self.mox.ReplayAll() res = self._make_request(self._make_url(instance['uuid']), body) self.assertEqual(400, res.status_int)
def update(self, req, id): """update the image by id with properties in req """ context = req.environ['nova.context'] using_check = self._image_is_using(req, context, id) image = {} try: image = self._image_service.show(context, id) except exception.NotFound: msg = _("Image not found.") raise webob.exc.HTTPNotFound(explanation=msg) try: meta = image["properties"] except KeyError: expl = _('no properties found for image') raise webob.exc.HTTPNotFound(explanation=expl) if 'image_type' in meta and meta['image_type'] == 'snapshot': raise webob.exc.HTTPForbidden( explanation="Cannot update a snapshot image") for key, value in meta.iteritems(): image['properties'][key] = value common.check_img_metadata_properties_quota(context, image['properties']) self._image_service.update(context, id, image, None, features={"x-glance-image-update-using-check": using_check}) return dict(metadata=image['properties'])
def test_create_backup_with_metadata(self): metadata = {'123': 'asdf'} body = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 1, 'metadata': metadata, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties=metadata) common.check_img_metadata_properties_quota(self.context, metadata) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, 'Backup 1', 'daily', 1, extra_properties=metadata).AndReturn(image) self.mox.ReplayAll() res = self._make_request(self._make_url(instance['uuid']), body) self.assertEqual(202, res.status_int) self.assertIn('fake-image-id', res.headers['Location'])
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] entity = body.get("createImage", {}) image_name = entity.get("name") if not image_name: msg = _("createImage entity requires name attribute") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): policy.enforce(context, 'compute:snapshot_volume_backed', {'project_id': context.project_id, 'user_id': context.user_id}) image = self.compute_api.snapshot_volume_backed( context, instance, image_name, extra_properties=props) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage', id) except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image['id']) url_prefix = self._view_builder._update_glance_link_prefix( req.application_url) image_ref = os.path.join(url_prefix, context.project_id, 'images', image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def test_create_backup_with_metadata(self): metadata = {'123': 'asdf'} body = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 1, 'metadata': metadata, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties=metadata) common.check_img_metadata_properties_quota(self.context, metadata) instance = self._stub_instance_get(objects=False) self.compute_api.backup(self.context, instance, 'Backup 1', 'daily', 1, extra_properties=metadata).AndReturn(image) self.mox.ReplayAll() res = self._make_request(self._make_url(instance['uuid']), body) self.assertEqual(202, res.status_int) self.assertIn('fake-image-id', res.headers['Location'])
def update(self, req, image_id, id, body): context = req.environ['nova.context'] try: meta = body['meta'] except KeyError: expl = _('Incorrect request body format') raise exc.HTTPBadRequest(explanation=expl) if id not in meta: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) if len(meta) > 1: expl = _('Request body contains too many items') raise exc.HTTPBadRequest(explanation=expl) image = self._get_image(context, image_id) image['properties'][id] = meta[id] common.check_img_metadata_properties_quota(context, image['properties']) try: self.image_api.update(context, image_id, image, data=None, purge_props=True) except exception.ImageNotAuthorized as e: raise exc.HTTPForbidden(explanation=e.format_message()) return dict(meta=meta)
def update_all(self, req, image_id, body): context = req.environ['nova.context'] image = self._get_image(context, image_id) metadata = body.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) image['properties'] = metadata self.image_service.update(context, image_id, image, None) return dict(metadata=metadata)
def create(self, req, image_id, body): context = req.environ["nova.context"] image = self._get_image(context, image_id) if "metadata" in body: for key, value in body["metadata"].iteritems(): image["properties"][key] = value common.check_img_metadata_properties_quota(context, image["properties"]) image = self.image_service.update(context, image_id, image, None) return dict(metadata=image["properties"])
def _create_backup(self, req, id, body): """Backup a server instance. Images now have an `image_type` associated with them, which can be 'snapshot' or the backup type, like 'daily' or 'weekly'. If the image_type is backup-like, then the rotation factor can be included and that will cause the oldest backups that exceed the rotation factor to be deleted. """ context = req.environ["nova.context"] context.can(cb_policies.BASE_POLICY_NAME) entity = body["createBackup"] image_name = common.normalize_name(entity["name"]) backup_type = entity["backup_type"] rotation = int(entity["rotation"]) props = {} metadata = entity.get('metadata', {}) # Starting from microversion 2.39 we don't check quotas on createBackup if api_version_request.is_supported( req, max_version= api_version_request.MAX_IMAGE_META_PROXY_API_VERSION): common.check_img_metadata_properties_quota(context, metadata) props.update(metadata) instance = common.get_instance(self.compute_api, context, id) try: image = self.compute_api.backup(context, instance, image_name, backup_type, rotation, extra_properties=props) except exception.InstanceUnknownCell as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createBackup', id) except exception.InvalidRequest as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) # Starting with microversion 2.45 we return a response body containing # the snapshot image id without the Location header. if api_version_request.is_supported(req, '2.45'): return {'image_id': image['id']} resp = webob.Response(status_int=202) # build location of newly-created image entity if rotation is not zero if rotation > 0: image_id = str(image['id']) image_ref = common.url_join(req.application_url, 'images', image_id) resp.headers['Location'] = image_ref return resp
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] entity = body.get("createImage", {}) image_name = entity.get("name") if not image_name: msg = _("createImage entity requires name attribute") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) instance = self._get_server(context, req, id) bdms = self.compute_api.get_instance_bdms(context, instance) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): img = instance['image_ref'] src_image = self.compute_api.image_service.show(context, img) image_meta = dict(src_image) image = self.compute_api.snapshot_volume_backed( context, instance, image_meta, image_name, extra_properties=props) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage') # build location of newly-created image entity image_id = str(image['id']) image_ref = os.path.join(req.application_url, context.project_id, 'images', image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def test_create_backup_with_non_existed_instance(self): body_map = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 1, }, } common.check_img_metadata_properties_quota(self.context, {}) self._test_non_existing_instance('createBackup', body_map=body_map)
def create(self, req, image_id, body): context = req.environ['nova.context'] image = self._get_image(context, image_id) if 'metadata' in body: for key, value in body['metadata'].iteritems(): image['properties'][key] = value common.check_img_metadata_properties_quota(context, image['properties']) image = self.image_service.update(context, image_id, image, None) return dict(metadata=image['properties'])
def create(self, req, image_id, body): context = req.environ['nova.context'] image = self._get_image(context, image_id) if 'metadata' in body: for key, value in body['metadata'].iteritems(): image['properties'][key] = value common.check_img_metadata_properties_quota(context, image['properties']) self.image_service.update(context, image_id, image, None) return dict(metadata=image['properties'])
def update_all(self, req, image_id, body): context = req.environ['nova.context'] image = self._get_image(context, image_id) metadata = body.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) image['properties'] = metadata try: self.image_service.update(context, image_id, image, None) except exception.ImageNotAuthorized as e: raise exc.HTTPForbidden(explanation=e.format_message()) return dict(metadata=metadata)
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] authorize(context, action='create_image') entity = body["createImage"] image_name = entity["name"] metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): authorize(context, action="create_image:allow_volume_backed") img = instance.image_ref if not img: properties = bdms.root_metadata( context, self.compute_api.image_api, self.compute_api.volume_api) image_meta = {'properties': properties} else: image_meta = self.compute_api.image_api.get(context, img) image = self.compute_api.snapshot_volume_backed( context, instance, image_meta, image_name, extra_properties= metadata) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=metadata) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage', id) except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image['id']) image_ref = glance.generate_image_url(image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] entity = body["createImage"] image_name = entity["name"] metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): img = instance['image_ref'] if not img: properties = bdms.root_metadata( context, self.compute_api.image_api, self.compute_api.volume_api) image_meta = {'properties': properties} else: image_meta = self.compute_api.image_api.get(context, img) image = self.compute_api.snapshot_volume_backed( context, instance, image_meta, image_name, extra_properties= metadata) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=metadata) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage', id) except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image['id']) image_ref = glance.generate_image_url(image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def test_check_img_metadata_properties_quota_valid_metadata(self): ctxt = utils.get_test_admin_context() metadata1 = {"key": "value"} actual = common.check_img_metadata_properties_quota(ctxt, metadata1) self.assertIsNone(actual) metadata2 = {"key": "v" * 260} actual = common.check_img_metadata_properties_quota(ctxt, metadata2) self.assertIsNone(actual) metadata3 = {"key": ""} actual = common.check_img_metadata_properties_quota(ctxt, metadata3) self.assertIsNone(actual)
def create(self, req, image_id, body): context = req.environ['nova.context'] image = self._get_image(context, image_id) for key, value in six.iteritems(body['metadata']): image['properties'][key] = value common.check_img_metadata_properties_quota(context, image['properties']) try: image = self.image_api.update(context, image_id, image, data=None, purge_props=True) except exception.ImageNotAuthorized as e: raise exc.HTTPForbidden(explanation=e.format_message()) return dict(metadata=image['properties'])
def test_check_img_metadata_properties_quota_valid_metadata(self): ctxt = utils.get_test_admin_context() metadata1 = {"key": "value"} actual = common.check_img_metadata_properties_quota(ctxt, metadata1) self.assertEqual(actual, None) metadata2 = {"key": "v" * 260} actual = common.check_img_metadata_properties_quota(ctxt, metadata2) self.assertEqual(actual, None) metadata3 = {"key": ""} actual = common.check_img_metadata_properties_quota(ctxt, metadata3) self.assertEqual(actual, None)
def _create_backup(self, req, id, body): """Backup a server instance. Images now have an `image_type` associated with them, which can be 'snapshot' or the backup type, like 'daily' or 'weekly'. If the image_type is backup-like, then the rotation factor can be included and that will cause the oldest backups that exceed the rotation factor to be deleted. """ context = req.environ["nova.context"] authorize(context) entity = body["createBackup"] image_name = common.normalize_name(entity["name"]) backup_type = entity["backup_type"] rotation = int(entity["rotation"]) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) props.update(metadata) instance = common.get_instance(self.compute_api, context, id) try: image = self.compute_api.backup(context, instance, image_name, backup_type, rotation, extra_properties=props) except exception.InstanceUnknownCell as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state( state_error, 'createBackup', id) except exception.InvalidRequest as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) resp = webob.Response(status_int=202) # build location of newly-created image entity if rotation is not zero if rotation > 0: image_id = str(image['id']) image_ref = common.url_join(req.application_url, 'images', image_id) resp.headers['Location'] = image_ref return resp
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ["nova.context"] entity = body.get("create_image", {}) image_name = entity.get("name") if not image_name: msg = _("create_image entity requires name attribute") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get("metadata", {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid(context, instance.uuid) try: if self.compute_api.is_volume_backed_instance(context, instance, bdms): img = instance["image_ref"] if not img: properties = bdms.root_metadata(context, self.compute_api.image_api, self.compute_api.volume_api) image_meta = {"properties": properties} else: image_meta = self.compute_api.image_api.get(context, img) image = self.compute_api.snapshot_volume_backed( context, instance, image_meta, image_name, extra_properties=props ) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, "create_image") except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image["id"]) image_ref = glance.generate_image_url(image_id) resp = webob.Response(status_int=202) resp.headers["Location"] = image_ref return resp
def test_create_backup_rotation_is_positive(self): # The happy path for creating backups if rotation is positive. body = {"createBackup": {"name": "Backup 1", "backup_type": "daily", "rotation": 1}} image = dict(id="fake-image-id", status="ACTIVE", name="Backup 1", properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, "Backup 1", "daily", 1, extra_properties={}).AndReturn(image) self.mox.ReplayAll() res = self._make_request(self._make_url(instance["uuid"]), body) self.assertEqual(202, res.status_int) self.assertIn("fake-image-id", res.headers["Location"])
def test_check_img_metadata_properties_quota_inv_metadata(self): ctxt = utils.get_test_admin_context() metadata1 = {"a" * 260: "value"} self.assertRaises(webob.exc.HTTPBadRequest, common.check_img_metadata_properties_quota, ctxt, metadata1) metadata2 = {"": "value"} self.assertRaises(webob.exc.HTTPBadRequest, common.check_img_metadata_properties_quota, ctxt, metadata2) metadata3 = "invalid metadata" self.assertRaises(webob.exc.HTTPBadRequest, common.check_img_metadata_properties_quota, ctxt, metadata3) metadata4 = None self.assertIsNone(common.check_img_metadata_properties_quota(ctxt, metadata4)) metadata5 = {} self.assertIsNone(common.check_img_metadata_properties_quota(ctxt, metadata5))
def _create_backup(self, req, id, body): """Backup a server instance. Images now have an `image_type` associated with them, which can be 'snapshot' or the backup type, like 'daily' or 'weekly'. If the image_type is backup-like, then the rotation factor can be included and that will cause the oldest backups that exceed the rotation factor to be deleted. """ context = req.environ["nova.context"] authorize(context) entity = body["createBackup"] image_name = common.normalize_name(entity["name"]) backup_type = entity["backup_type"] rotation = int(entity["rotation"]) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) props.update(metadata) instance = common.get_instance(self.compute_api, context, id) try: image = self.compute_api.backup(context, instance, image_name, backup_type, rotation, extra_properties=props) except exception.InstanceUnknownCell as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createBackup', id) except exception.InvalidRequest as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) resp = webob.Response(status_int=202) # build location of newly-created image entity if rotation is not zero if rotation > 0: image_id = str(image['id']) image_ref = common.url_join(req.application_url, 'images', image_id) resp.headers['Location'] = image_ref return resp
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] context.can(server_policies.SERVERS % 'create_image') entity = body["createImage"] image_name = common.normalize_name(entity["name"]) metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) instance = self._get_server(context, req, id) bdms = objects.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid) try: if compute_utils.is_volume_backed_instance(context, instance, bdms): context.can(server_policies.SERVERS % 'create_image:allow_volume_backed') image = self.compute_api.snapshot_volume_backed( context, instance, image_name, extra_properties= metadata) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=metadata) except exception.InstanceUnknownCell as e: raise exc.HTTPNotFound(explanation=e.format_message()) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage', id) except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image['id']) image_ref = glance.generate_image_url(image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def test_create_backup_raises_conflict_on_invalid_state(self): body_map = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 1, }, } args_map = { 'createBackup': ( ('Backup 1', 'daily', 1), {'extra_properties': {}} ), } common.check_img_metadata_properties_quota(self.context, {}) self._test_invalid_state('createBackup', method='backup', body_map=body_map, compute_api_args_map=args_map)
def test_create_backup_with_metadata(self): metadata = {"123": "asdf"} body = {"createBackup": {"name": "Backup 1", "backup_type": "daily", "rotation": 1, "metadata": metadata}} image = dict(id="fake-image-id", status="ACTIVE", name="Backup 1", properties=metadata) common.check_img_metadata_properties_quota(self.context, metadata) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, "Backup 1", "daily", 1, extra_properties=metadata).AndReturn( image ) self.mox.ReplayAll() res = self._make_request(self._make_url(instance["uuid"]), body) self.assertEqual(202, res.status_int) self.assertIn("fake-image-id", res.headers["Location"])
def test_create_backup_name_with_leading_trailing_spaces(self): body = { 'createBackup': { 'name': ' test ', 'backup_type': 'daily', 'rotation': 1, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, ' test ', 'daily', 1, extra_properties={}).AndReturn(image) self.mox.ReplayAll() self.controller._create_backup(self.req, instance.uuid, body=body)
def update(self, req, image_id, id, body): context = req.environ['nova.context'] meta = body['meta'] if id not in meta: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) image = self._get_image(context, image_id) image['properties'][id] = meta[id] common.check_img_metadata_properties_quota(context, image['properties']) try: self.image_api.update(context, image_id, image, data=None, purge_props=True) except exception.ImageNotAuthorized as e: raise exc.HTTPForbidden(explanation=e.format_message()) return dict(meta=meta)
def test_backup_volume_backed_instance(self): body = { 'createBackup': { 'name': 'BackupMe', 'backup_type': 'daily', 'rotation': 3 }, } common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() instance.image_ref = None self.compute_api.backup(self.context, instance, 'BackupMe', 'daily', 3, extra_properties={}).AndRaise(exception.InvalidRequest()) self.mox.ReplayAll() res = self._make_request(self._make_url(instance['uuid']), body) self.assertEqual(400, res.status_int)
def test_create_backup_name_with_leading_trailing_spaces_compat_mode( self): body = { 'createBackup': { 'name': ' test ', 'backup_type': 'daily', 'rotation': 1, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, 'test', 'daily', 1, extra_properties={}).AndReturn(image) self.mox.ReplayAll() self.req.set_legacy_v2() self.controller._create_backup(self.req, instance.uuid, body=body)
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] entity = body.get("createImage", {}) image_name = entity.get("name") if not image_name: msg = _("createImage entity requires name attribute") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) instance = self._get_server(context, req, id) try: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'createImage') # build location of newly-created image entity image_id = str(image['id']) image_ref = os.path.join(req.application_url, context.project_id, 'images', image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def update(self, req, image_id, id, body): context = req.environ["nova.context"] try: meta = body["meta"] except KeyError: expl = _("Incorrect request body format") raise exc.HTTPBadRequest(explanation=expl) if not id in meta: expl = _("Request body and URI mismatch") raise exc.HTTPBadRequest(explanation=expl) if len(meta) > 1: expl = _("Request body contains too many items") raise exc.HTTPBadRequest(explanation=expl) image = self._get_image(context, image_id) image["properties"][id] = meta[id] common.check_img_metadata_properties_quota(context, image["properties"]) self.image_service.update(context, image_id, image, None) return dict(meta=meta)
def test_backup_volume_backed_instance(self): body = { 'createBackup': { 'name': 'BackupMe', 'backup_type': 'daily', 'rotation': 3 }, } common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() instance.image_ref = None self.compute_api.backup(self.context, instance, 'BackupMe', 'daily', 3, extra_properties={}).AndRaise(exception.InvalidRequest()) self.mox.ReplayAll() self.assertRaises(webob.exc.HTTPBadRequest, self.controller._create_backup, self.req, instance['uuid'], body=body)
def update(self, req, image_id, id, body): context = req.environ['nova.context'] try: meta = body['meta'] except KeyError: expl = _('Incorrect request body format') raise exc.HTTPBadRequest(explanation=expl) if not id in meta: expl = _('Request body and URI mismatch') raise exc.HTTPBadRequest(explanation=expl) if len(meta) > 1: expl = _('Request body contains too many items') raise exc.HTTPBadRequest(explanation=expl) image = self._get_image(context, image_id) image['properties'][id] = meta[id] common.check_img_metadata_properties_quota(context, image['properties']) self.image_service.update(context, image_id, image, None) return dict(meta=meta)
def test_create_backup_rotation_is_positive(self): # The happy path for creating backups if rotation is positive. body = { 'createBackup': { 'name': 'Backup 1', 'backup_type': 'daily', 'rotation': 1, }, } image = dict(id='fake-image-id', status='ACTIVE', name='Backup 1', properties={}) common.check_img_metadata_properties_quota(self.context, {}) instance = self._stub_instance_get() self.compute_api.backup(self.context, instance, 'Backup 1', 'daily', 1, extra_properties={}).AndReturn(image) self.mox.ReplayAll() res = self._make_request(self._make_url(instance['uuid']), body) self.assertEqual(202, res.status_int) self.assertIn('fake-image-id', res.headers['Location'])
def _action_create_image(self, req, id, body): """Snapshot a server instance.""" context = req.environ['nova.context'] entity = body.get("create_image", {}) image_name = entity.get("name") if not image_name: msg = _("create_image entity requires name attribute") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) instance = self._get_server(context, req, id) bdms = block_device_obj.BlockDeviceMappingList.get_by_instance_uuid( context, instance.uuid) try: if self.compute_api.is_volume_backed_instance( context, instance, bdms): img = instance['image_ref'] if not img: props = bdms.root_metadata(context, self.compute_api.image_service, self.compute_api.volume_api) image_meta = {'properties': props} else: src_image = self.compute_api.\ image_service.show(context, img) image_meta = dict(src_image) image = self.compute_api.snapshot_volume_backed( context, instance, image_meta, image_name, extra_properties=props) else: image = self.compute_api.snapshot(context, instance, image_name, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state( state_error, 'create_image') except exception.Invalid as err: raise exc.HTTPBadRequest(explanation=err.format_message()) # build location of newly-created image entity image_id = str(image['id']) image_ref = glance.generate_image_url(image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref return resp
def _create_backup(self, req, id, body): """Backup a server instance. Images now have an `image_type` associated with them, which can be 'snapshot' or the backup type, like 'daily' or 'weekly'. If the image_type is backup-like, then the rotation factor can be included and that will cause the oldest backups that exceed the rotation factor to be deleted. """ context = req.environ["nova.context"] authorize(context, 'createBackup') entity = body["createBackup"] try: image_name = entity["name"] backup_type = entity["backup_type"] rotation = entity["rotation"] except KeyError as missing_key: msg = _("createBackup entity requires %s attribute") % missing_key raise exc.HTTPBadRequest(explanation=msg) except TypeError: msg = _("Malformed createBackup entity") raise exc.HTTPBadRequest(explanation=msg) try: rotation = int(rotation) except ValueError: msg = _("createBackup attribute 'rotation' must be an integer") raise exc.HTTPBadRequest(explanation=msg) if rotation < 0: msg = _("createBackup attribute 'rotation' must be greater " "than or equal to zero") raise exc.HTTPBadRequest(explanation=msg) props = {} metadata = entity.get('metadata', {}) common.check_img_metadata_properties_quota(context, metadata) try: props.update(metadata) except ValueError: msg = _("Invalid metadata") raise exc.HTTPBadRequest(explanation=msg) try: instance = self.compute_api.get(context, id, want_objects=True) except exception.NotFound: msg = _("Instance not found") raise exc.HTTPNotFound(explanation=msg) try: image = self.compute_api.backup(context, instance, image_name, backup_type, rotation, extra_properties=props) except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state( state_error, 'createBackup') resp = webob.Response(status_int=202) # build location of newly-created image entity if rotation is not zero if rotation > 0: image_id = str(image['id']) image_ref = os.path.join(req.application_url, 'images', image_id) resp.headers['Location'] = image_ref return resp