summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNaveed Massjouni <naveedm9@gmail.com>2011-05-02 21:37:59 +0000
committerTarmac <>2011-05-02 21:37:59 +0000
commitc069337734e99d5833b5a1814e33ffb1fbb5fff6 (patch)
tree9532691439aa3459b4819899bae6565102d884bb
parente4538af1a6e4b9796da3f4d7c3484084d3b72463 (diff)
parent2463fc66e363e13bf955e1ebb9b370f8e901d328 (diff)
Added support in the nova openstack api for requests with local hrefs, e.g., "imageRef":"2"
Previously, it only supported "imageRef":"http://foo.com/images/2". The 1.1 api spec defines both approaches.
-rw-r--r--nova/api/openstack/common.py9
-rw-r--r--nova/tests/api/openstack/test_servers.py27
2 files changed, 35 insertions, 1 deletions
diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py
index 65ed1e143..32cd689ca 100644
--- a/nova/api/openstack/common.py
+++ b/nova/api/openstack/common.py
@@ -15,6 +15,7 @@
# License for the specific language governing permissions and limitations
# under the License.
+import re
from urlparse import urlparse
import webob
@@ -130,10 +131,16 @@ def get_image_id_from_image_hash(image_service, context, image_hash):
def get_id_from_href(href):
"""Return the id portion of a url as an int.
- Given: http://www.foo.com/bar/123?q=4
+ Given: 'http://www.foo.com/bar/123?q=4'
+ Returns: 123
+
+ In order to support local hrefs, the href argument can be just an id:
+ Given: '123'
Returns: 123
"""
+ if re.match(r'\d+$', str(href)):
+ return int(href)
try:
return int(urlparse(href).path.split('/')[-1])
except:
diff --git a/nova/tests/api/openstack/test_servers.py b/nova/tests/api/openstack/test_servers.py
index f294b3b56..bbce77bf0 100644
--- a/nova/tests/api/openstack/test_servers.py
+++ b/nova/tests/api/openstack/test_servers.py
@@ -620,6 +620,33 @@ class ServersTest(test.TestCase):
res = req.get_response(fakes.wsgi_app())
self.assertEqual(res.status_int, 400)
+ def test_create_instance_v11_local_href(self):
+ self._setup_for_create_instance()
+
+ imageRef = 'http://localhost/v1.1/images/2'
+ imageRefLocal = '2'
+ flavorRef = 'http://localhost/v1.1/flavors/3'
+ body = {
+ 'server': {
+ 'name': 'server_test',
+ 'imageRef': imageRefLocal,
+ 'flavorRef': flavorRef,
+ },
+ }
+
+ req = webob.Request.blank('/v1.1/servers')
+ req.method = 'POST'
+ req.body = json.dumps(body)
+ req.headers["content-type"] = "application/json"
+
+ res = req.get_response(fakes.wsgi_app())
+
+ server = json.loads(res.body)['server']
+ self.assertEqual(1, server['id'])
+ self.assertEqual(flavorRef, server['flavorRef'])
+ self.assertEqual(imageRef, server['imageRef'])
+ self.assertEqual(res.status_int, 200)
+
def test_create_instance_with_admin_pass_v10(self):
self._setup_for_create_instance()