summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosh Kearney <josh@jk0.org>2011-09-21 18:27:23 +0000
committerTarmac <>2011-09-21 18:27:23 +0000
commit14068bc8f748ee3bee3c5b86ac19cd21b0ff8b67 (patch)
treec0d87fd5a575ef80227ccd35e75e73f9c74827ba
parent7e3bebbe8e911851a7398b8d5ad81afb421dfd62 (diff)
parent10589faa5fbde09689641d5e64ddd41a341eaade (diff)
downloadnova-14068bc8f748ee3bee3c5b86ac19cd21b0ff8b67.tar.gz
nova-14068bc8f748ee3bee3c5b86ac19cd21b0ff8b67.tar.xz
nova-14068bc8f748ee3bee3c5b86ac19cd21b0ff8b67.zip
Adds an 'alternate' link to image views per 3.10 and 3.11 of http://docs.openstack.org/cactus/openstack-compute/developer/openstack-compute-api-1.1/content/LinksReferences.html
-rw-r--r--nova/api/openstack/images.py2
-rw-r--r--nova/api/openstack/views/images.py16
-rw-r--r--nova/flags.py4
-rw-r--r--nova/tests/api/openstack/test_images.py102
-rw-r--r--nova/tests/test_utils.py9
-rw-r--r--nova/utils.py7
6 files changed, 138 insertions, 2 deletions
diff --git a/nova/api/openstack/images.py b/nova/api/openstack/images.py
index 4340cbe3e..d579ae716 100644
--- a/nova/api/openstack/images.py
+++ b/nova/api/openstack/images.py
@@ -251,6 +251,8 @@ class ImageXMLSerializer(wsgi.XMLDictSerializer):
elem = etree.SubElement(image_elem,
'{%s}link' % xmlutil.XMLNS_ATOM)
elem.set('rel', link['rel'])
+ if 'type' in link:
+ elem.set('type', link['type'])
elem.set('href', link['href'])
return image_elem
diff --git a/nova/api/openstack/views/images.py b/nova/api/openstack/views/images.py
index 86e8d7f3a..659bfd463 100644
--- a/nova/api/openstack/views/images.py
+++ b/nova/api/openstack/views/images.py
@@ -18,6 +18,7 @@
import os.path
from nova.api.openstack import common
+from nova import utils
class ViewBuilder(object):
@@ -139,6 +140,7 @@ class ViewBuilderV11(ViewBuilder):
image = ViewBuilder.build(self, image_obj, detail)
href = self.generate_href(image_obj["id"])
bookmark = self.generate_bookmark(image_obj["id"])
+ alternate = self.generate_alternate(image_obj["id"])
image["links"] = [
{
@@ -149,6 +151,11 @@ class ViewBuilderV11(ViewBuilder):
"rel": "bookmark",
"href": bookmark,
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": alternate,
+ },
]
@@ -158,6 +165,13 @@ class ViewBuilderV11(ViewBuilder):
return image
def generate_bookmark(self, image_id):
- """Create an url that refers to a specific flavor id."""
+ """Create a URL that refers to a specific flavor id."""
return os.path.join(common.remove_version_from_href(self.base_url),
self.project_id, "images", str(image_id))
+
+ def generate_alternate(self, image_id):
+ """Create an alternate link for a specific flavor id."""
+ glance_url = utils.generate_glance_url()
+
+ return "%s/%s/images/%s" % (glance_url, self.project_id,
+ str(image_id))
diff --git a/nova/flags.py b/nova/flags.py
index 971e78807..ffb313cec 100644
--- a/nova/flags.py
+++ b/nova/flags.py
@@ -271,8 +271,10 @@ DEFINE_string('connection_type', 'libvirt', 'libvirt, xenapi or fake')
DEFINE_string('aws_access_key_id', 'admin', 'AWS Access ID')
DEFINE_string('aws_secret_access_key', 'admin', 'AWS Access Key')
# NOTE(sirp): my_ip interpolation doesn't work within nested structures
+DEFINE_string('glance_host', _get_my_ip(), 'default glance host')
+DEFINE_integer('glance_port', 9292, 'default glance port')
DEFINE_list('glance_api_servers',
- ['%s:9292' % _get_my_ip()],
+ ['%s:%d' % (FLAGS.glance_host, FLAGS.glance_port)],
'list of glance api servers available to nova (host:port)')
DEFINE_integer('s3_port', 3333, 's3 port')
DEFINE_string('s3_host', '$my_ip', 's3 host (for infrastructure)')
diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py
index e5fd4764a..886efb6ac 100644
--- a/nova/tests/api/openstack/test_images.py
+++ b/nova/tests/api/openstack/test_images.py
@@ -33,7 +33,9 @@ from nova import context
import nova.api.openstack
from nova.api.openstack import images
from nova.api.openstack import xmlutil
+from nova.api.openstack.views import images as images_view
from nova import test
+from nova import utils
from nova.tests.api.openstack import fakes
@@ -119,6 +121,7 @@ class ImagesTest(test.TestCase):
href = "http://localhost/v1.1/fake/images/124"
bookmark = "http://localhost/fake/images/124"
+ alternate = "%s/fake/images/124" % utils.generate_glance_url()
server_href = "http://localhost/v1.1/servers/42"
server_bookmark = "http://localhost/servers/42"
@@ -152,6 +155,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": bookmark,
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": alternate
}],
},
}
@@ -289,6 +297,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/123",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/123" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -303,6 +317,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/124",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/124" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -317,6 +337,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/125",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/125" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -331,6 +357,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/126",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/126" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -345,6 +377,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/127",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/127" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -359,6 +397,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/128",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/128" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -373,6 +417,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/129",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/129" %
+ utils.generate_glance_url()
+ },
],
},
{
@@ -387,6 +437,12 @@ class ImagesTest(test.TestCase):
"rel": "bookmark",
"href": "http://localhost/fake/images/130",
},
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/130" %
+ utils.generate_glance_url()
+ },
],
},
]
@@ -493,6 +549,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/123",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/123" % utils.generate_glance_url()
}],
},
{
@@ -524,6 +585,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/124",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/124" % utils.generate_glance_url()
}],
},
{
@@ -555,6 +621,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/125",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/125" % utils.generate_glance_url()
}],
},
{
@@ -586,6 +657,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/126",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/126" % utils.generate_glance_url()
}],
},
{
@@ -617,6 +693,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/127",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/127" % utils.generate_glance_url()
}],
},
{
@@ -648,6 +729,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/128",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/128" % utils.generate_glance_url()
}],
},
{
@@ -679,6 +765,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/129",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/129" % utils.generate_glance_url()
}],
},
{
@@ -696,6 +787,11 @@ class ImagesTest(test.TestCase):
{
"rel": "bookmark",
"href": "http://localhost/fake/images/130",
+ },
+ {
+ "rel": "alternate",
+ "type": "application/vnd.openstack.image",
+ "href": "%s/fake/images/130" % utils.generate_glance_url()
}],
},
]
@@ -963,6 +1059,12 @@ class ImagesTest(test.TestCase):
response = req.get_response(fakes.wsgi_app())
self.assertEqual(400, response.status_int)
+ def test_generate_alternate(self):
+ view = images_view.ViewBuilderV11(1)
+ generated_url = view.generate_alternate(1)
+ actual_url = "%s//images/1" % utils.generate_glance_url()
+ self.assertEqual(generated_url, actual_url)
+
class ImageXMLSerializationTest(test.TestCase):
diff --git a/nova/tests/test_utils.py b/nova/tests/test_utils.py
index 1ba794a1a..19a15332d 100644
--- a/nova/tests/test_utils.py
+++ b/nova/tests/test_utils.py
@@ -20,10 +20,14 @@ import tempfile
import nova
from nova import exception
+from nova import flags
from nova import test
from nova import utils
+FLAGS = flags.FLAGS
+
+
class ExecuteTestCase(test.TestCase):
def test_retry_on_failure(self):
fd, tmpfilename = tempfile.mkstemp()
@@ -291,6 +295,11 @@ class GenericUtilsTestCase(test.TestCase):
self.assertFalse(utils.bool_from_str(None))
self.assertFalse(utils.bool_from_str('junk'))
+ def test_generate_glance_url(self):
+ generated_url = utils.generate_glance_url()
+ actual_url = "http://%s:%d" % (FLAGS.glance_host, FLAGS.glance_port)
+ self.assertEqual(generated_url, actual_url)
+
class IsUUIDLikeTestCase(test.TestCase):
def assertUUIDLike(self, val, expected):
diff --git a/nova/utils.py b/nova/utils.py
index 81157a4cd..57c93d9d0 100644
--- a/nova/utils.py
+++ b/nova/utils.py
@@ -910,3 +910,10 @@ def convert_to_list_dict(lst, label):
if not isinstance(lst, list):
lst = [lst]
return [{label: x} for x in lst]
+
+
+def generate_glance_url():
+ """Generate the URL to glance."""
+ # TODO(jk0): This will eventually need to take SSL into consideration
+ # when supported in glance.
+ return "http://%s:%d" % (FLAGS.glance_host, FLAGS.glance_port)