]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
Allow templates specified from local filesystem
authorTomas Sedovic <tomas@sedovic.cz>
Mon, 2 Apr 2012 16:00:00 +0000 (18:00 +0200)
committerTomas Sedovic <tomas@sedovic.cz>
Mon, 2 Apr 2012 16:01:18 +0000 (18:01 +0200)
Fixes #47

Passing a template file (as opposed to a URL) to the CLI:

      ./heat -d create wordpress \
        --template-file=../templates/WordPress_Single_Instance.template

caused a 'URI too long' error. The reason was that we were passing the entire
template contents to the Heat API server inside the URL params.

This makes sure that templates are passed around inside HTTP body rather than
the URL.

heat/api/v1/__init__.py
heat/api/v1/stacks.py
heat/client.py
heat/common/client.py
heat/engine/client.py

index 8ce18894d6fea11225a9f2c15fc0cc3d6efad9e8..550e3a66befaa7058e50d9e84d73068943131b4e 100644 (file)
@@ -49,6 +49,6 @@ class API(wsgi.Router):
         mapper.connect("/DescribeStackEvents", controller=stacks_resource,
                        action="events_list", conditions=dict(method=["GET"]))
         mapper.connect("/ValidateTemplate", controller=stacks_resource,
-                       action="validate_template", conditions=dict(method=["GET"]))
+                       action="validate_template", conditions=dict(method=["POST"]))
 
         super(API, self).__init__(mapper)
index d1b9bd4aa60c5af9eb05c12a8f180d29318dcfab..6943a578609622107b412c3d25de7bf90427ef45 100644 (file)
@@ -75,13 +75,13 @@ class StackController(object):
 
         return res
 
-    def _get_template(self, req):
-        if req.params.has_key('TemplateBody'):
+    def _get_template(self, body):
+        if body.has_key('TemplateBody'):
             logger.info('TemplateBody ...')
-            return req.params['TemplateBody']
-        elif req.params.has_key('TemplateUrl'):
-            logger.info('TemplateUrl %s' % req.params['TemplateUrl'])
-            url = urlparse.urlparse(req.params['TemplateUrl'])
+            return body['TemplateBody']
+        elif body.has_key('TemplateUrl'):
+            logger.info('TemplateUrl %s' % body['TemplateUrl'])
+            url = urlparse.urlparse(body['TemplateUrl'])
             if url.scheme == 'https':
                 conn = httplib.HTTPSConnection(url.netloc)
             else:
@@ -99,14 +99,14 @@ class StackController(object):
         return None
 
 
-    def create(self, req):
+    def create(self, req, body):
         """
         Returns the following information for all stacks:
         """
         c = engine.get_engine_client(req.context)
 
         try:
-            templ = self._get_template(req)
+            templ = self._get_template(body)
         except socket.gaierror:
             msg = _('Invalid Template URL')
             return webob.exc.HTTPBadRequest(explanation=msg)
@@ -119,16 +119,16 @@ class StackController(object):
         except ValueError:
             msg = _("The Template must be a JSON document.")
             return webob.exc.HTTPBadRequest(explanation=msg)
-        stack['StackName'] = req.params['StackName']
+        stack['StackName'] = body['StackName']
 
         return c.create_stack(stack, **req.params)
 
-    def validate_template(self, req):
+    def validate_template(self, req, body):
 
         client = engine.get_engine_client(req.context)
 
         try:
-            templ = self._get_template(req)
+            templ = self._get_template(body)
         except socket.gaierror:
             msg = _('Invalid Template URL')
             return webob.exc.HTTPBadRequest(explanation=msg)
index ed73b7c382c7a1789d6c6ccc409629d5d99ba80a..a830a697174451391d004116fe5997d00f3aead1 100644 (file)
@@ -58,25 +58,27 @@ class V1Client(base_client.BaseClient):
         return data
 
     def create_stack(self, **kwargs):
-
-        params = self._extract_params(kwargs, SUPPORTED_PARAMS)
+        body = self._extract_params(kwargs, SUPPORTED_PARAMS)
+        params = {}
         self._insert_common_parameters(params)
-        res = self.do_request("POST", "/CreateStack", params=params)
 
+        res = self.do_request("POST", "/CreateStack", params=params, body=body)
         data = json.loads(res.read())
         return data
 
     def update_stack(self, **kwargs):
-        params = self._extract_params(kwargs, SUPPORTED_PARAMS)
+        body = self._extract_params(kwargs, SUPPORTED_PARAMS)
+        params = {}
         self._insert_common_parameters(params)
-        res = self.do_request("PUT", "/UpdateStack", params=params)
 
+        res = self.do_request("PUT", "/UpdateStack", params=params, body=body)
         data = json.loads(res.read())
         return data
 
     def delete_stack(self, **kwargs):
         params = self._extract_params(kwargs, SUPPORTED_PARAMS)
         self._insert_common_parameters(params)
+
         res = self.do_request("DELETE", "/DeleteStack", params=params)
         data = json.loads(res.read())
         return data
@@ -90,10 +92,12 @@ class V1Client(base_client.BaseClient):
         return data
 
     def validate_template(self, **kwargs):
-        params = self._extract_params(kwargs, SUPPORTED_PARAMS)
+        body = self._extract_params(kwargs, SUPPORTED_PARAMS)
+        params = {}
         self._insert_common_parameters(params)
-        
-        res = self.do_request("GET", "/ValidateTemplate", params=params)
+
+        res = self.do_request("POST", "/ValidateTemplate", params=params,
+                              body=body)
         data = json.loads(res.read())
         return data
 
index a3a49d347fc940d563588478d6a4d45c79e1cb7f..b9a1d3ba9e414601ee64c256e3dd627e573a443d 100644 (file)
@@ -21,6 +21,7 @@ import collections
 import errno
 import functools
 import httplib
+import json
 import logging
 import os
 import urllib
@@ -414,6 +415,10 @@ class BaseClient(object):
             self._authenticate()
 
         url = self._construct_url(action, params)
+        headers = headers or {}
+        if body and not isinstance(body, basestring):
+            body = json.dumps(body)
+            headers['Content-Type'] = 'application/json'
         return self._do_request(method=method, url=url, body=body,
                                 headers=headers)
 
index 2bae31dbfd125aff535e89e37e0e964309c59464..d82245e0c8c4ca0f286831768ee1102697b67062 100644 (file)
@@ -91,13 +91,9 @@ class EngineClient(BaseClient):
         """
         Validate the template
         """
-        headers = {
-            'Content-Type': 'application/json',
-        }
-
         logger.info(template)
-        res = self.do_request("POST", "/validate_template", body=json.dumps(template),
-                              headers=headers, params=kwargs)
+        res = self.do_request("POST", "/validate_template", template,
+                              params=kwargs)
         data = json.loads(res.read())
         logger.info(data)
         return data
@@ -106,12 +102,7 @@ class EngineClient(BaseClient):
         """
         Tells engine about an stack's metadata
         """
-        headers = {
-            'Content-Type': 'application/json',
-        }
-
-        res = self.do_request("POST", "/stacks", json.dumps(template),
-                              headers=headers, params=kwargs)
+        res = self.do_request("POST", "/stacks", template, params=kwargs)
         data = json.loads(res.read())
         return data
 
@@ -119,11 +110,7 @@ class EngineClient(BaseClient):
         """
         Updates Engine's information about an stack
         """
-        headers = {
-            'Content-Type': 'application/json',
-        }
-
-        res = self.do_request("PUT", "/stacks/%s" % (stack_id), json.dumps(template), headers)
+        res = self.do_request("PUT", "/stacks/%s" % (stack_id), template)
         data = json.loads(res.read())
         stack = data['stack']
         return stack