From 241a926ed682cb6154ff8f37c4940e7b5885b6fe Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 26 Jul 2011 16:13:09 -0400 Subject: moved v1.1 image creation from /images to /servers//action --- nova/api/openstack/images.py | 37 ++++++++--------- nova/api/openstack/servers.py | 93 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 24 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/images.py b/nova/api/openstack/images.py index 30e4fd389..517a51662 100644 --- a/nova/api/openstack/images.py +++ b/nova/api/openstack/images.py @@ -98,6 +98,20 @@ class Controller(object): self._image_service.delete(context, id) return webob.exc.HTTPNoContent() + def get_builder(self, request): + """Indicates that you must use a Controller subclass.""" + raise NotImplementedError() + + def _server_id_from_req(self, req, data): + raise NotImplementedError() + + def _get_extra_properties(self, req, data): + return {} + + +class ControllerV10(Controller): + """Version 1.0 specific controller logic.""" + def create(self, req, body): """Snapshot or backup a server instance and save the image. @@ -157,20 +171,6 @@ class Controller(object): return dict(image=self.get_builder(req).build(image, detail=True)) - def get_builder(self, request): - """Indicates that you must use a Controller subclass.""" - raise NotImplementedError() - - def _server_id_from_req(self, req, data): - raise NotImplementedError() - - def _get_extra_properties(self, req, data): - return {} - - -class ControllerV10(Controller): - """Version 1.0 specific controller logic.""" - def get_builder(self, request): """Property to get the ViewBuilder class we need to use.""" base_url = request.application_url @@ -278,6 +278,9 @@ class ControllerV11(Controller): server_ref) return {'instance_ref': server_ref} + def create(self, *args, **kwargs): + raise webob.exc.HTTPMethodNotAllowed() + class ImageXMLSerializer(wsgi.XMLDictSerializer): @@ -369,12 +372,6 @@ class ImageXMLSerializer(wsgi.XMLDictSerializer): image_dict['image']) return self.to_xml_string(node, True) - def create(self, image_dict): - xml_doc = minidom.Document() - node = self._image_to_xml_detailed(xml_doc, - image_dict['image']) - return self.to_xml_string(node, True) - def create_resource(version='1.0'): controller = { diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index d7cabb067..5371e62c9 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -14,6 +14,7 @@ # under the License. import base64 +import os import traceback from webob import exc @@ -155,20 +156,26 @@ class Controller(object): """Multi-purpose method used to reboot, rebuild, or resize a server""" - actions = { + self.actions = { 'changePassword': self._action_change_password, 'reboot': self._action_reboot, 'resize': self._action_resize, 'confirmResize': self._action_confirm_resize, 'revertResize': self._action_revert_resize, 'rebuild': self._action_rebuild, - 'migrate': self._action_migrate} + 'migrate': self._action_migrate, + 'createImage': self._action_create_image, + } - for key in actions.keys(): + + for key in self.actions.keys(): if key in body: - return actions[key](body, req, id) + return self.actions[key](body, req, id) raise exc.HTTPNotImplemented() + def _action_create_image(self, input_dict, req, id): + return exc.HTTPNotImplemented() + def _action_change_password(self, input_dict, req, id): return exc.HTTPNotImplemented() @@ -585,6 +592,80 @@ class ControllerV11(Controller): return webob.Response(status_int=202) + def _action_create_image(self, input_dict, req, instance_id): + """Snapshot or backup a server instance and save the image. + + 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. + + """ + entity = input_dict.get('createImage', {}) + + def get_param(param): + try: + return entity[param] + except KeyError: + msg = _("Missing required param: %s") % param + raise webob.exc.HTTPBadRequest(explanation=msg) + + context = req.environ['nova.context'] + + image_name = get_param("name") + image_type = entity.get("image_type", "snapshot") + + # preserve link to server in image properties + server_ref = os.path.join(req.application_url, + 'servers', + str(instance_id)) + props = {'instance_ref': server_ref} + + metadata = entity.get('metadata', {}) + try: + props.update(metadata) + except ValueError: + msg = _("Invalid metadata") + raise webob.exc.HTTPBadRequest(explanation=msg) + + if image_type == "snapshot": + image = self.compute_api.snapshot(context, + instance_id, + image_name, + extra_properties=props) + + elif image_type == "backup": + # NOTE(sirp): Unlike snapshot, backup is not a customer facing + # API call; rather, it's used by the internal backup scheduler + if not FLAGS.allow_admin_api: + msg = _("Admin API Required") + raise webob.exc.HTTPBadRequest(explanation=msg) + + backup_type = get_param("backup_type") + rotation = int(get_param("rotation")) + + image = self.compute_api.backup(context, + instance_id, + image_name, + backup_type, + rotation, + extra_properties=props) + else: + msg = _("Invalid image_type '%s'") % image_type + raise webob.exc.HTTPBadRequest(explanation=msg) + + + # build location of newly-created image entity + image_ref = os.path.join(req.application_url, + 'images', + str(image['id'])) + + resp = webob.Response(status_int=202) + resp.headers['Location'] = image_ref + return resp + def get_default_xmlns(self, req): return common.XML_NS_V11 @@ -593,6 +674,10 @@ class ControllerV11(Controller): return self.helper._get_server_admin_password_new_style(server) + + + + class HeadersSerializer(wsgi.ResponseHeadersSerializer): def delete(self, response, data): -- cgit From c8b1a357c9bd5fe4bc54e6472f9667123d91c02a Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 26 Jul 2011 16:53:00 -0400 Subject: adding xml deserialization for createImage action --- nova/api/openstack/create_instance_helper.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'nova/api') diff --git a/nova/api/openstack/create_instance_helper.py b/nova/api/openstack/create_instance_helper.py index f8317565e..eb556ae8b 100644 --- a/nova/api/openstack/create_instance_helper.py +++ b/nova/api/openstack/create_instance_helper.py @@ -293,6 +293,30 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): and personality attributes """ + def action(self, string): + dom = minidom.parseString(string) + action_node = dom.childNodes[0] + action_name = action_node.tagName + + action_deserializer = { + 'createImage': self._action_create_image, + }.get(action_name, self.default) + + action_data = action_deserializer(action_node) + + return {'body': {action_name: action_data}} + + def _action_create_image(self, node): + data = {} + attributes = ['name', 'image_type', 'backup_type', 'rotation'] + for attribute in attributes: + value = node.getAttribute(attribute) + if value: + data[attribute] = value + metadata_node = self.find_first_child_named(node, 'metadata') + data['metadata'] = self.extract_metadata(metadata_node) + return data + def create(self, string): """Deserialize an xml-formatted server create request""" dom = minidom.parseString(string) -- cgit From 84909f4a7733dde453afcc5cc540854ac1bc458c Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 26 Jul 2011 17:00:10 -0400 Subject: pep8 --- nova/api/openstack/servers.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 7a900cb54..0cc81009b 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -153,8 +153,7 @@ class Controller(object): @scheduler_api.redirect_handler def action(self, req, id, body): - """Multi-purpose method used to reboot, rebuild, or - resize a server""" + """Multi-purpose method used to take actions on a server""" self.actions = { 'changePassword': self._action_change_password, @@ -167,7 +166,6 @@ class Controller(object): 'createImage': self._action_create_image, } - for key in self.actions.keys(): if key in body: return self.actions[key](body, req, id) @@ -665,7 +663,6 @@ class ControllerV11(Controller): msg = _("Invalid image_type '%s'") % image_type raise webob.exc.HTTPBadRequest(explanation=msg) - # build location of newly-created image entity image_ref = os.path.join(req.application_url, 'images', @@ -683,10 +680,6 @@ class ControllerV11(Controller): return self.helper._get_server_admin_password_new_style(server) - - - - class HeadersSerializer(wsgi.ResponseHeadersSerializer): def delete(self, response, data): -- cgit From 8141ef4139fbf8512150ce970cea4dc4bee22e1a Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 28 Jul 2011 16:21:12 -0400 Subject: moving server backup to /servers//action instead of POST /images --- nova/api/openstack/create_instance_helper.py | 12 ++- nova/api/openstack/images.py | 112 +++------------------- nova/api/openstack/servers.py | 133 +++++++++++++++++---------- 3 files changed, 111 insertions(+), 146 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/create_instance_helper.py b/nova/api/openstack/create_instance_helper.py index 73ff191e8..e165899e7 100644 --- a/nova/api/openstack/create_instance_helper.py +++ b/nova/api/openstack/create_instance_helper.py @@ -300,6 +300,7 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): action_deserializer = { 'createImage': self._action_create_image, + 'createBackup': self._action_create_image, }.get(action_name, self.default) action_data = action_deserializer(action_node) @@ -308,7 +309,16 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): def _action_create_image(self, node): data = {} - attributes = ['name', 'image_type', 'backup_type', 'rotation'] + value = node.getAttribute('name') + if value: + data['name'] = value + metadata_node = self.find_first_child_named(node, 'metadata') + data['metadata'] = self.extract_metadata(metadata_node) + return data + + def _action_create_image(self, node): + data = {} + attributes = ['name', 'backup_type', 'rotation'] for attribute in attributes: value = node.getAttribute(attribute) if value: diff --git a/nova/api/openstack/images.py b/nova/api/openstack/images.py index 517a51662..b3e16c997 100644 --- a/nova/api/openstack/images.py +++ b/nova/api/openstack/images.py @@ -102,72 +102,27 @@ class Controller(object): """Indicates that you must use a Controller subclass.""" raise NotImplementedError() - def _server_id_from_req(self, req, data): - raise NotImplementedError() - - def _get_extra_properties(self, req, data): - return {} - class ControllerV10(Controller): """Version 1.0 specific controller logic.""" def create(self, req, body): - """Snapshot or backup a server instance and save the image. - - 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. - - :param req: `wsgi.Request` object - """ - def get_param(param): - try: - return body["image"][param] - except KeyError: - raise webob.exc.HTTPBadRequest(explanation="Missing required " - "param: %s" % param) - - context = req.environ['nova.context'] - content_type = req.get_content_type() - - if not body: - raise webob.exc.HTTPBadRequest() - - image_type = body["image"].get("image_type", "snapshot") + """Snapshot a server instance and save the image.""" + try: + image = body["image"] + except (KeyError, TypeError): + msg = _("Invalid image entity") + raise webob.exc.HTTPBadRequest(explanation=msg) try: - server_id = self._server_id_from_req(req, body) - except KeyError: - raise webob.exc.HTTPBadRequest() - - image_name = get_param("name") - props = self._get_extra_properties(req, body) - - if image_type == "snapshot": - image = self._compute_service.snapshot( - context, server_id, image_name, - extra_properties=props) - elif image_type == "backup": - # NOTE(sirp): Unlike snapshot, backup is not a customer facing - # API call; rather, it's used by the internal backup scheduler - if not FLAGS.allow_admin_api: - raise webob.exc.HTTPBadRequest( - explanation="Admin API Required") - - backup_type = get_param("backup_type") - rotation = int(get_param("rotation")) - - image = self._compute_service.backup( - context, server_id, image_name, - backup_type, rotation, extra_properties=props) - else: - LOG.error(_("Invalid image_type '%s' passed") % image_type) - raise webob.exc.HTTPBadRequest(explanation="Invalue image_type: " - "%s" % image_type) + image_name = image["name"] + server_id = image["serverId"] + except KeyError as missing_key: + msg = _("Image entity requires %s") % missing_key + raise webob.exc.HTTPBadRequest(explanation=msg) + + context = req.environ["nova.context"] + image = self._compute_service.snapshot(context, server_id, image_name) return dict(image=self.get_builder(req).build(image, detail=True)) @@ -202,13 +157,6 @@ class ControllerV10(Controller): builder = self.get_builder(req).build return dict(images=[builder(image, detail=True) for image in images]) - def _server_id_from_req(self, req, data): - try: - return data['image']['serverId'] - except KeyError: - msg = _("Expected serverId attribute on server entity.") - raise webob.exc.HTTPBadRequest(explanation=msg) - class ControllerV11(Controller): """Version 1.1 specific controller logic.""" @@ -246,38 +194,6 @@ class ControllerV11(Controller): builder = self.get_builder(req).build return dict(images=[builder(image, detail=True) for image in images]) - def _server_id_from_req(self, req, data): - try: - server_ref = data['image']['serverRef'] - except KeyError: - msg = _("Expected serverRef attribute on server entity.") - raise webob.exc.HTTPBadRequest(explanation=msg) - - if not server_ref.startswith('http'): - return server_ref - - passed = urlparse.urlparse(server_ref) - expected = urlparse.urlparse(req.application_url) - version = expected.path.split('/')[1] - expected_prefix = "/%s/servers/" % version - _empty, _sep, server_id = passed.path.partition(expected_prefix) - scheme_ok = passed.scheme == expected.scheme - host_ok = passed.hostname == expected.hostname - port_ok = (passed.port == expected.port or - passed.port == FLAGS.osapi_port) - if not (scheme_ok and port_ok and host_ok and server_id): - msg = _("serverRef must match request url") - raise webob.exc.HTTPBadRequest(explanation=msg) - - return server_id - - def _get_extra_properties(self, req, data): - server_ref = data['image']['serverRef'] - if not server_ref.startswith('http'): - server_ref = os.path.join(req.application_url, 'servers', - server_ref) - return {'instance_ref': server_ref} - def create(self, *args, **kwargs): raise webob.exc.HTTPMethodNotAllowed() diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index 0cc81009b..fafde1c15 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -166,11 +166,79 @@ class Controller(object): 'createImage': self._action_create_image, } + if FLAGS.allow_admin_api: + admin_actions = { + 'createBackup': self._action_create_backup, + } + self.actions.update(admin_actions) + for key in self.actions.keys(): if key in body: return self.actions[key](body, req, id) + raise exc.HTTPNotImplemented() + def _action_create_backup(self, input_dict, req, instance_id): + """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. + + """ + entity = input_dict["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 webob.exc.HTTPBadRequest(explanation=msg) + + except TypeError: + msg = _("Malformed createBackup entity") + raise webob.exc.HTTPBadRequest(explanation=msg) + + try: + rotation = int(rotation) + except ValueError: + msg = _("createBackup attribute 'rotation' must be an integer") + raise webob.exc.HTTPBadRequest(explanation=msg) + + # preserve link to server in image properties + server_ref = os.path.join(req.application_url, + 'servers', + str(instance_id)) + props = {'instance_ref': server_ref} + + metadata = entity.get('metadata', {}) + try: + props.update(metadata) + except ValueError: + msg = _("Invalid metadata") + raise webob.exc.HTTPBadRequest(explanation=msg) + + context = req.environ["nova.context"] + image = self.compute_api.backup(context, + instance_id, + image_name, + backup_type, + rotation, + extra_properties=props) + + # build location of newly-created image entity + image_id = str(image['id']) + image_ref = os.path.join(req.application_url, 'images', image_id) + + resp = webob.Response(status_int=202) + resp.headers['Location'] = image_ref + return resp + def _action_create_image(self, input_dict, req, id): return exc.HTTPNotImplemented() @@ -599,30 +667,22 @@ class ControllerV11(Controller): return webob.Response(status_int=202) - def _action_create_image(self, input_dict, req, instance_id): - """Snapshot or backup a server instance and save the image. - - 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. - """ - entity = input_dict.get('createImage', {}) + def _action_create_image(self, input_dict, req, instance_id): + """Snapshot a server instance.""" + entity = input_dict.get("createImage", {}) - def get_param(param): - try: - return entity[param] - except KeyError: - msg = _("Missing required param: %s") % param - raise webob.exc.HTTPBadRequest(explanation=msg) + try: + image_name = entity["name"] - context = req.environ['nova.context'] + except KeyError: + msg = _("createImage entity requires name attribute") + raise webob.exc.HTTPBadRequest(explanation=msg) - image_name = get_param("name") - image_type = entity.get("image_type", "snapshot") + except TypeError: + msg = _("Malformed createImage entity") + raise webob.exc.HTTPBadRequest(explanation=msg) # preserve link to server in image properties server_ref = os.path.join(req.application_url, @@ -637,36 +697,15 @@ class ControllerV11(Controller): msg = _("Invalid metadata") raise webob.exc.HTTPBadRequest(explanation=msg) - if image_type == "snapshot": - image = self.compute_api.snapshot(context, - instance_id, - image_name, - extra_properties=props) - - elif image_type == "backup": - # NOTE(sirp): Unlike snapshot, backup is not a customer facing - # API call; rather, it's used by the internal backup scheduler - if not FLAGS.allow_admin_api: - msg = _("Admin API Required") - raise webob.exc.HTTPBadRequest(explanation=msg) - - backup_type = get_param("backup_type") - rotation = int(get_param("rotation")) - - image = self.compute_api.backup(context, - instance_id, - image_name, - backup_type, - rotation, - extra_properties=props) - else: - msg = _("Invalid image_type '%s'") % image_type - raise webob.exc.HTTPBadRequest(explanation=msg) + context = req.environ['nova.context'] + image = self.compute_api.snapshot(context, + instance_id, + image_name, + extra_properties=props) # build location of newly-created image entity - image_ref = os.path.join(req.application_url, - 'images', - str(image['id'])) + image_id = str(image['id']) + image_ref = os.path.join(req.application_url, 'images', image_id) resp = webob.Response(status_int=202) resp.headers['Location'] = image_ref -- cgit From 0a7e19481849f451f04063d3d2fc45b8f3328119 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Thu, 28 Jul 2011 16:33:14 -0400 Subject: pep8 --- nova/api/openstack/servers.py | 2 -- 1 file changed, 2 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/servers.py b/nova/api/openstack/servers.py index fafde1c15..636d79b66 100644 --- a/nova/api/openstack/servers.py +++ b/nova/api/openstack/servers.py @@ -667,8 +667,6 @@ class ControllerV11(Controller): return webob.Response(status_int=202) - - def _action_create_image(self, input_dict, req, instance_id): """Snapshot a server instance.""" entity = input_dict.get("createImage", {}) -- cgit From 804bbc7656080597880e9705532ac161d3124aa4 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 2 Aug 2011 10:21:36 -0400 Subject: fixing method naming problem --- nova/api/openstack/create_instance_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/create_instance_helper.py b/nova/api/openstack/create_instance_helper.py index 5a745ded3..ea5d4af73 100644 --- a/nova/api/openstack/create_instance_helper.py +++ b/nova/api/openstack/create_instance_helper.py @@ -303,7 +303,7 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): action_deserializer = { 'createImage': self._action_create_image, - 'createBackup': self._action_create_image, + 'createBackup': self._action_create_backup, }.get(action_name, self.default) action_data = action_deserializer(action_node) @@ -319,7 +319,7 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): data['metadata'] = self.extract_metadata(metadata_node) return data - def _action_create_image(self, node): + def _action_create_backup(self, node): data = {} attributes = ['name', 'backup_type', 'rotation'] for attribute in attributes: -- cgit From 1fc4c4cb9bdfca2cf6a931cec44fa25ee76c502d Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 2 Aug 2011 10:24:23 -0400 Subject: abstraction of xml deserialization --- nova/api/openstack/create_instance_helper.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'nova/api') diff --git a/nova/api/openstack/create_instance_helper.py b/nova/api/openstack/create_instance_helper.py index ea5d4af73..b0f422655 100644 --- a/nova/api/openstack/create_instance_helper.py +++ b/nova/api/openstack/create_instance_helper.py @@ -311,18 +311,15 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): return {'body': {action_name: action_data}} def _action_create_image(self, node): - data = {} - value = node.getAttribute('name') - if value: - data['name'] = value - metadata_node = self.find_first_child_named(node, 'metadata') - data['metadata'] = self.extract_metadata(metadata_node) - return data + return self._deserialize_image_action(node, ('name',)) def _action_create_backup(self, node): + attributes = ('name', 'backup_type', 'rotation') + return self._deserialize_image_action(node, attributes) + + def _deserialize_image_action(self, node, allowed_attribtues): data = {} - attributes = ['name', 'backup_type', 'rotation'] - for attribute in attributes: + for attribute in allowed_attributes: value = node.getAttribute(attribute) if value: data[attribute] = value -- cgit From b65c7e2378d8344d1948fe4cf0dde66ef34b7204 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 2 Aug 2011 13:32:14 -0400 Subject: fixing typo --- nova/api/openstack/create_instance_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/api') diff --git a/nova/api/openstack/create_instance_helper.py b/nova/api/openstack/create_instance_helper.py index b0f422655..53e814cd5 100644 --- a/nova/api/openstack/create_instance_helper.py +++ b/nova/api/openstack/create_instance_helper.py @@ -317,7 +317,7 @@ class ServerXMLDeserializer(wsgi.MetadataXMLDeserializer): attributes = ('name', 'backup_type', 'rotation') return self._deserialize_image_action(node, attributes) - def _deserialize_image_action(self, node, allowed_attribtues): + def _deserialize_image_action(self, node, allowed_attributes): data = {} for attribute in allowed_attributes: value = node.getAttribute(attribute) -- cgit