diff options
author | Steve Baker <sbaker@redhat.com> | 2014-01-11 10:25:20 +1300 |
---|---|---|
committer | Steve Baker <sbaker@redhat.com> | 2014-01-13 10:30:01 +1300 |
commit | 68a3fa076e7d9fd06a1c344bed5722a2a54f5449 (patch) | |
tree | 6b82f740cd10e1f60083f93e351e525dbdcb129d /heatclient/common/template_utils.py | |
parent | df14b6560082fac60fa7ca947f67355e11f44f92 (diff) | |
download | python-heatclient-68a3fa076e7d9fd06a1c344bed5722a2a54f5449.tar.gz |
Improve and unit-test template contents fetching
This includes some important changes in the way template
content is fetched:
* for template_file, the value is converted to a file:// URL and the
actual fetching is performed with the URL fetching code path.
As a consequence it is now possible to specify a file path *or*
a URL for the --template-file command option. This is a change in
behaviour, and it does make --template-url somewhat redundant.
This makes the behaviour of --template-file consistent with the
behaviour of --environment-file.
* for template_url, the content is fetched in heatclient so that
the server does not need to fetch the template at all. This means
that in the API request the 'template' field is populated instead
of 'template_url'. We can consider deprecating 'template_url' from
the REST API.
Change-Id: Id8de05973381dba7ac052c45dd074071fd3035b2
Diffstat (limited to 'heatclient/common/template_utils.py')
-rw-r--r-- | heatclient/common/template_utils.py | 59 |
1 files changed, 29 insertions, 30 deletions
diff --git a/heatclient/common/template_utils.py b/heatclient/common/template_utils.py index e956d9c..6eecb8a 100644 --- a/heatclient/common/template_utils.py +++ b/heatclient/common/template_utils.py @@ -22,36 +22,35 @@ from heatclient import exc from heatclient.openstack.common.py3kcompat import urlutils -def set_template_fields(hc, args, fields): - if args.template_file: - tpl = open(args.template_file).read() - try: - fields['template'] = template_format.parse(tpl) - except ValueError as e: - raise exc.CommandError( - "Cannot parse template file: %s" % e) - return args.template_file - - if args.template_url: - fields['template_url'] = args.template_url - return args.template_url - - if args.template_object: - template_body = hc.http_client.raw_request('GET', args.template_object) - if template_body: - try: - fields['template'] = template_format.parse(template_body) - except ValueError as e: - raise exc.CommandError( - "Cannot parse template file: %s" % e) - else: - raise exc.CommandError('Could not fetch template from %s' - % args.template_object) - return args.template_object - - raise exc.CommandError('Need to specify exactly one of ' - '--template-file, --template-url ' - 'or --template-object') +def get_template_contents(template_file=None, template_url=None, + template_object=None, object_request=None): + + # Transform a bare file path to a file:// URL. + if template_file: + template_url = urlutils.urljoin( + 'file:', urllib.pathname2url(template_file)) + + if template_url: + tpl = urlutils.urlopen(template_url).read() + + elif template_object: + template_url = template_object + tpl = object_request and object_request('GET', + template_object) + else: + raise exc.CommandError('Need to specify exactly one of ' + '--template-file, --template-url ' + 'or --template-object') + + if not tpl: + raise exc.CommandError('Could not fetch template from %s' + % template_url) + + try: + return template_format.parse(tpl) + except ValueError as e: + raise exc.CommandError( + 'Error parsing template %s %s' % (template_url, e)) def get_file_contents(resource_registry, fields, base_url='', |