]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
ReST API: Add a convenience redirect for resources
authorZane Bitter <zbitter@redhat.com>
Wed, 14 Nov 2012 16:51:26 +0000 (17:51 +0100)
committerZane Bitter <zbitter@redhat.com>
Fri, 16 Nov 2012 11:19:47 +0000 (12:19 +0100)
Since the path stacks/{stack_name}/{stack_id}/resources exists for each
valid stack we can safely redirect to it from stacks/{stack_name}/resources
without having to perform any further checks in the engine, so we may as
well allow it.

Change-Id: I0169493479f8c6840de3edad271cf98e8fd5d1da
Signed-off-by: Zane Bitter <zbitter@redhat.com>
docs/api.md
heat/api/openstack/v1/__init__.py
heat/api/openstack/v1/stacks.py
heat/tests/test_api_openstack_v1.py

index dc4262e715309dfab09cb7023f717dec02d623f7..385a6d1cfcda4853d5737a2e7d4229bb36ffc9fa 100644 (file)
@@ -176,6 +176,26 @@ Parameters:
 * `stack_name` The name of the stack to look up
 * `stack_id` The unique identifier of the stack to look up
 
+Find Stack Resources by Name
+----------------------------
+
+```
+GET /v1/{tenant_id}/stacks/{stack_name}/resources
+```
+
+Parameters:
+
+* `stack_name` The name of the stack to look up
+
+Result:
+
+```
+HTTP/1.1 302 Found
+Location: http://heat.example.com:8004/v1/{tenant_id}/stacks/{stack_name}/{stack_id}/resources
+```
+
+This is a shortcut to go directly to the list of stack resources when only the stack name is known.
+
 Get Resource
 ------------
 
index 4a02ee63b5f7ecfa025ce287ae4e4d0404b7f238..446ac7fb357f81ec025168486e44b579be71b7ee 100644 (file)
@@ -69,6 +69,10 @@ class API(wsgi.Router):
             stack_mapper.connect("stack_lookup",
                                  "/stacks/{stack_name}",
                                  action="lookup")
+            stack_mapper.connect("stack_lookup_subpath",
+                                 "/stacks/{stack_name}/{path:resources}",
+                                 action="lookup",
+                                 conditions={'method': 'GET'})
             stack_mapper.connect("stack_show",
                                  "/stacks/{stack_name}/{stack_id}",
                                  action="show",
index fc4b0e5a11eb2d077699698d3d7f90de5fad8b92..ff4e1bf73d0af93be277849fdb2c6aff364cc2f9 100644 (file)
@@ -210,7 +210,7 @@ class StackController(object):
         raise exc.HTTPCreated(location=util.make_url(req, result))
 
     @util.tenant_local
-    def lookup(self, req, stack_name, body=None):
+    def lookup(self, req, stack_name, path='', body=None):
         """
         Redirect to the canonical URL for a stack
         """
@@ -221,7 +221,11 @@ class StackController(object):
         except rpc_common.RemoteError as ex:
             return util.remote_error(ex)
 
-        raise exc.HTTPFound(location=util.make_url(req, identity))
+        location = util.make_url(req, identity)
+        if path:
+            location = '/'.join([location, path])
+
+        raise exc.HTTPFound(location=location)
 
     @util.identified_stack
     def show(self, req, identity):
index ae5a2c1585f415497e583bae595f95207b8c7bf3..3ce7a842a67b75264c071fcf890099f8b4ee4940 100644 (file)
@@ -433,6 +433,49 @@ class StackControllerTest(ControllerTest, unittest.TestCase):
                           req, tenant_id=self.tenant, stack_name=stack_name)
         self.m.VerifyAll()
 
+    def test_lookup_resource(self):
+        identity = identifier.HeatIdentifier(self.tenant, 'wordpress', '1')
+
+        req = self._get('/stacks/%(stack_name)s/resources' % identity)
+
+        self.m.StubOutWithMock(rpc, 'call')
+        rpc.call(req.context, self.topic,
+                 {'method': 'identify_stack',
+                  'args': {'stack_name': identity.stack_name},
+                  'version': self.api_version},
+                 None).AndReturn(identity)
+
+        self.m.ReplayAll()
+
+        try:
+            result = self.controller.lookup(req, tenant_id=identity.tenant,
+                                            stack_name=identity.stack_name,
+                                            path='resources')
+        except webob.exc.HTTPFound as found:
+            self.assertEqual(found.location, self._url(identity) +
+                                             '/resources')
+        else:
+            self.fail('No redirect generated')
+        self.m.VerifyAll()
+
+    def test_lookup_resource_nonexistant(self):
+        stack_name = 'wibble'
+
+        req = self._get('/stacks/%(stack_name)s/resources' % locals())
+
+        self.m.StubOutWithMock(rpc, 'call')
+        rpc.call(req.context, self.topic,
+                 {'method': 'identify_stack',
+                  'args': {'stack_name': stack_name},
+                  'version': self.api_version},
+                 None).AndRaise(rpc_common.RemoteError("AttributeError"))
+        self.m.ReplayAll()
+
+        self.assertRaises(webob.exc.HTTPNotFound, self.controller.lookup,
+                          req, tenant_id=self.tenant, stack_name=stack_name,
+                          path='resources')
+        self.m.VerifyAll()
+
     def test_show(self):
         identity = identifier.HeatIdentifier(self.tenant, 'wordpress', '6')