def show(self, request, image): """Return a dictionary with image details.""" image_dict = { "id": image.get("id"), "name": image.get("name"), "minRam": int(image.get("min_ram") or 0), "minDisk": int(image.get("min_disk") or 0), "metadata": image.get("properties", {}), "created": self._format_date(image.get("created_at")), "updated": self._format_date(image.get("updated_at")), "status": self._get_status(image), "progress": self._get_progress(image), "links": self._get_links(request, image["id"]), } server_ref = image.get("properties", {}).get("instance_ref") if server_ref is not None: image_dict["server"] = { "id": common.get_id_from_href(server_ref), "links": [{ "rel": "self", "href": server_ref, }, { "rel": "bookmark", "href": common.remove_version_from_href(server_ref), }], } return dict(image=image_dict)
def _action_rebuild(self, info, request, instance_id): context = request.environ['nova.context'] instance_id = int(instance_id) try: image_ref = info["rebuild"]["imageRef"] except (KeyError, TypeError): msg = _("Could not parse imageRef from request.") LOG.debug(msg) return faults.Fault(exc.HTTPBadRequest(explanation=msg)) image_id = common.get_id_from_href(image_ref) personalities = info["rebuild"].get("personality", []) metadata = info["rebuild"].get("metadata", {}) self._validate_metadata(metadata) self._decode_personalities(personalities) try: self.compute_api.rebuild(context, instance_id, image_id, metadata, personalities) except exception.BuildInProgress: msg = _("Instance %d is currently being rebuilt.") % instance_id LOG.debug(msg) return faults.Fault(exc.HTTPConflict(explanation=msg)) response = exc.HTTPAccepted() response.empty_body = True return response
def _get_image(self, request, instance): image_ref = instance["image_ref"] if image_ref: image_id = str(common.get_id_from_href(image_ref)) bookmark = self._image_builder._get_bookmark_link(request, image_id, "images") return {"id": image_id, "links": [{"rel": "bookmark", "href": bookmark}]} else: return ""
def _flavor_id_from_req_data(self, data): try: flavor_ref = data['server']['flavor_ref'] except (TypeError, KeyError): msg = _("Missing flavor_ref attribute") raise exc.HTTPBadRequest(explanation=msg) return common.get_id_from_href(flavor_ref)
def _build_flavor(self, response, inst): if inst.get("instance_type", None): flavor_id = inst["instance_type"]["flavorid"] flavor_ref = self.flavor_builder.generate_href(flavor_id) flavor_bookmark = self.flavor_builder.generate_bookmark(flavor_id) response["flavor"] = { "id": str(common.get_id_from_href(flavor_ref)), "links": [{"rel": "bookmark", "href": flavor_bookmark}], }
def _get_flavor(self, request, instance): flavor_id = instance["instance_type"]["flavorid"] flavor_ref = self._flavor_builder._get_href_link(request, flavor_id) flavor_bookmark = self._flavor_builder._get_bookmark_link(request, flavor_id) return { "id": str(common.get_id_from_href(flavor_ref)), "links": [{ "rel": "bookmark", "href": flavor_bookmark, }], }
def _build_server(self, image, image_obj): try: serverRef = image_obj["properties"]["instance_ref"] image["server"] = { "id": common.get_id_from_href(serverRef), "links": [ {"rel": "self", "href": serverRef}, {"rel": "bookmark", "href": common.remove_version_from_href(serverRef)}, ], } except KeyError: return
def _action_resize(self, input_dict, req, id): """ Resizes a given instance to the flavor size requested """ try: if 'resize' in input_dict and 'flavorRef' in input_dict['resize']: flavor_ref = input_dict['resize']['flavorRef'] flavor_id = common.get_id_from_href(flavor_ref) self.compute_api.resize(req.environ['nova.context'], id, flavor_id) else: LOG.exception(_("Missing 'flavorRef' argument for resize")) return faults.Fault(exc.HTTPUnprocessableEntity()) except Exception, e: LOG.exception(_("Error in resize %s"), e) return faults.Fault(exc.HTTPBadRequest())
def _build_image(self, response, inst): if 'image_ref' in dict(inst): image_href = inst['image_ref'] image_id = str(common.get_id_from_href(image_href)) _bookmark = self.image_builder.generate_bookmark(image_id) response['image'] = { "id": image_id, "links": [ { "rel": "bookmark", "href": _bookmark, }, ] }
def _build_server(self, image, image_obj): try: serverRef = image_obj['properties']['instance_ref'] image['server'] = { "id": common.get_id_from_href(serverRef), "links": [ { "rel": "self", "href": serverRef, }, { "rel": "bookmark", "href": common.remove_version_from_href(serverRef), }, ] } except KeyError: return
def test_get_id_from_href(self): fixture = 'http://www.testsite.com/dir/45' actual = common.get_id_from_href(fixture) expected = 45 self.assertEqual(actual, expected)
def test_get_id_from_href_int(self): fixture = 1 self.assertEqual(fixture, common.get_id_from_href(fixture))
def _image_id_from_req_data(self, data): href = data['server']['imageRef'] return common.get_id_from_href(href)
def test_get_id_from_href_with_uuid_url_query(self): fixture = 'http://www.testsite.com/dir/abc123?asdf=jkl' actual = common.get_id_from_href(fixture) expected = "abc123" self.assertEqual(actual, expected)
def test_get_id_from_href_with_uuid(self): fixture = 'abc123' actual = common.get_id_from_href(fixture) expected = 'abc123' self.assertEqual(actual, expected)
def test_get_id_from_href_with_int(self): fixture = '45' actual = common.get_id_from_href(fixture) expected = '45' self.assertEqual(actual, expected)
def test_get_id_from_href_with_int_url_query(self): fixture = 'http://www.testsite.com/dir/45?asdf=jkl' actual = common.get_id_from_href(fixture) expected = '45' self.assertEqual(actual, expected)
def test_get_id_from_href(self): fixture = 'http://www.testsite.com/dir/45' actual = common.get_id_from_href(fixture) expected = 45 self.assertEqual(actual, expected)
def test_get_id_from_href_with_uuid(self): fixture = 'abc123' actual = common.get_id_from_href(fixture) expected = 'abc123' self.assertEqual(actual, expected)
def test_get_id_from_href_with_int_url_query(self): fixture = 'http://www.testsite.com/dir/45?asdf=jkl' actual = common.get_id_from_href(fixture) expected = '45' self.assertEqual(actual, expected)
def _flavor_id_from_req_data(self, data): flavor_ref = data['server']['flavorRef'] return common.get_id_from_href(flavor_ref)
def _build_image(self, response, inst): if inst.get("image_ref", None): image_href = inst["image_ref"] image_id = str(common.get_id_from_href(image_href)) _bookmark = self.image_builder.generate_bookmark(image_id) response["image"] = {"id": image_id, "links": [{"rel": "bookmark", "href": _bookmark}]}
def test_get_id_from_href_with_int(self): fixture = '45' actual = common.get_id_from_href(fixture) expected = '45' self.assertEqual(actual, expected)
def _flavor_id_from_req_data(self, data): flavor_ref = data["server"]["flavorRef"] return common.get_id_from_href(flavor_ref)
def test_get_id_from_href_with_uuid_url_query(self): fixture = 'http://www.testsite.com/dir/abc123?asdf=jkl' actual = common.get_id_from_href(fixture) expected = "abc123" self.assertEqual(actual, expected)
def _flavor_id_from_req_data(self, data): flavor_ref = data['server']['flavorRef'] return common.get_id_from_href(flavor_ref)
def test_get_id_from_href_with_int_url(self): fixture = "http://www.testsite.com/dir/45" actual = common.get_id_from_href(fixture) expected = "45" self.assertEqual(actual, expected)
def _image_id_from_req_data(self, data): href = data['server']['imageRef'] return common.get_id_from_href(href)