]> review.fuel-infra Code Review - openstack-build/heat-build.git/commitdiff
heat cli: encapsulate template arg-parsing
authorSteven Hardy <shardy@redhat.com>
Mon, 15 Oct 2012 15:42:52 +0000 (16:42 +0100)
committerSteven Hardy <shardy@redhat.com>
Mon, 15 Oct 2012 17:57:48 +0000 (18:57 +0100)
Encapsulate template argument parsing in a helper function
cleans up some duplication and provides a single place to add
swift-template-url logic

Ref #216

Change-Id: I36515f318ed79899a2710899ffca83e4452c61f4
Signed-off-by: Steven Hardy <shardy@redhat.com>
bin/heat

index 7f615b7078e15075d7f060a050d897518f464584..34473538fb812f69b57e182c144813352f2de457 100755 (executable)
--- a/bin/heat
+++ b/bin/heat
@@ -52,6 +52,18 @@ from heat.common import exception
 from heat import utils
 
 
+def get_template_param(options):
+    '''
+    Helper function to extract the template in whatever
+    format has been specified by the cli options
+    '''
+    param={}
+    if options.template_file:
+        param['TemplateBody'] = open(options.template_file).read()
+    elif options.template_url:
+        param['TemplateUrl'] = options.template_url
+    return param
+
 @utils.catch_error('validate')
 def template_validate(options, arguments):
     '''
@@ -66,10 +78,9 @@ def template_validate(options, arguments):
     --template-url:  Specify a URL pointing to a stack description template.
     '''
     parameters = {}
-    if options.template_file:
-        parameters['TemplateBody'] = open(options.template_file).read()
-    elif options.template_url:
-        parameters['TemplateUrl'] = options.template_url
+    templ_param = get_template_param(options)
+    if templ_param:
+        parameters.update(templ_param)
     else:
         logging.error('Please specify a template file or url')
         return utils.FAILURE
@@ -90,10 +101,9 @@ def estimate_template_cost(options, arguments):
         logging.error("as the first argument")
         return utils.FAILURE
 
-    if options.template_file:
-        parameters['TemplateBody'] = open(options.template_file).read()
-    elif options.template_url:
-        parameters['TemplateUrl'] = options.template_url
+    templ_param = get_template_param(options)
+    if templ_param:
+        parameters.update(templ_param)
     else:
         logging.error('Please specify a template file or url')
         return utils.FAILURE
@@ -149,10 +159,9 @@ def stack_create(options, arguments):
 
     parameters['TimeoutInMinutes'] = options.timeout
 
-    if options.template_file:
-        parameters['TemplateBody'] = open(options.template_file).read()
-    elif options.template_url:
-        parameters['TemplateUrl'] = options.template_url
+    templ_param = get_template_param(options)
+    if templ_param:
+        parameters.update(templ_param)
     else:
         logging.error('Please specify a template file or url')
         return utils.FAILURE
@@ -191,10 +200,12 @@ def stack_update(options, arguments):
         logging.error("as the first argument")
         return utils.FAILURE
 
-    if options.template_file:
-        parameters['TemplateBody'] = open(options.template_file).read()
-    elif options.template_url:
-        parameters['TemplateUrl'] = options.template_url
+    templ_param = get_template_param(options)
+    if templ_param:
+        parameters.update(templ_param)
+    else:
+        logging.error('Please specify a template file or url')
+        return utils.FAILURE
 
     c = get_client(options)
     parameters.update(c.format_parameters(options))