]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
ReST API: Separate out code to fetch template from a URL
authorZane Bitter <zbitter@redhat.com>
Fri, 12 Oct 2012 20:32:54 +0000 (22:32 +0200)
committerZane Bitter <zbitter@redhat.com>
Fri, 12 Oct 2012 20:33:25 +0000 (22:33 +0200)
This will make testing a bit easier.

Change-Id: I8f9d63e3ff68174bb9f1284c21f52696ec5ac006
Signed-off-by: Zane Bitter <zbitter@redhat.com>
heat/api/openstack/v1/stacks.py

index b7d0b1bb71085413b1a44e264ae9975d35167deb..682285a866c09eb6ed58f134a33d29695910477b 100644 (file)
@@ -86,6 +86,33 @@ class InstantiationData(object):
             raise exc.HTTPBadRequest(explanation=_("No stack name specified"))
         return self.data[self.PARAM_STACK_NAME]
 
+    def _load_template(self, template_url):
+        """
+        Retrieve a template from a URL, in JSON format.
+        """
+        logger.debug('Template URL %s' % template_url)
+        url = urlparse.urlparse(template_url)
+        err_reason = _("Could not retrieve template")
+
+        try:
+            ConnType = (url.scheme == 'https' and httplib.HTTPSConnection
+                                               or httplib.HTTPConnection)
+            conn = ConnType(url.netloc)
+
+            try:
+                conn.request("GET", url.path)
+                resp = conn.getresponse()
+                logger.info('status %d' % r1.status)
+
+                if resp.status != 200:
+                    raise exc.HTTPBadRequest(explanation=err_reason)
+
+                return self.json_parse(resp.read(), 'Template')
+            finally:
+                conn.close()
+        except socket.gaierror:
+            raise exc.HTTPBadRequest(explanation=err_reason)
+
     def template(self):
         """
         Get template file contents, either inline or from a URL, in JSON
@@ -94,29 +121,7 @@ class InstantiationData(object):
         if self.PARAM_TEMPLATE in self.data:
             return self.data[self.PARAM_TEMPLATE]
         elif self.PARAM_TEMPLATE_URL in self.data:
-            template_url = self.data[self.PARAM_TEMPLATE_URL]
-            logger.debug('Template URL %s' % template_url)
-            url = urlparse.urlparse(template_url)
-            err_reason = _("Could not retrieve template")
-
-            try:
-                ConnType = (url.scheme == 'https' and httplib.HTTPSConnection
-                                                   or httplib.HTTPConnection)
-                conn = ConnType(url.netloc)
-
-                try:
-                    conn.request("GET", url.path)
-                    resp = conn.getresponse()
-                    logger.info('status %d' % r1.status)
-
-                    if resp.status != 200:
-                        raise exc.HTTPBadRequest(explanation=err_reason)
-
-                    return self.json_parse(resp.read(), 'Template')
-                finally:
-                    conn.close()
-            except socket.gaierror:
-                raise exc.HTTPBadRequest(explanation=err_reason)
+            return self._load_template(self.data[self.PARAM_TEMPLATE_URL])
 
         raise exc.HTTPBadRequest(explanation=_("No template specified"))