diff options
author | Andrew Plunk <andrew.plunk@rackspace.com> | 2013-08-05 10:28:47 -0500 |
---|---|---|
committer | Andrew Plunk <andrew.plunk@rackspace.com> | 2013-08-07 09:47:40 -0500 |
commit | 68999ba81fc622ab9274d26725fddda83a631f53 (patch) | |
tree | cd6e2aad5eac1516ef1b590a2c526c656dbe39ec /heatclient | |
parent | 2c05c73a6b7137a428111bb52b97087aac1c43a0 (diff) | |
download | python-heatclient-68999ba81fc622ab9274d26725fddda83a631f53.tar.gz |
Generate a template from a resource
Setup the command line options and calls for generating
a template from an installed resource.
blueprint resource-template
Change-Id: If533bd90b1ec73bbe2603b55a0a7621879d355ec
Diffstat (limited to 'heatclient')
-rw-r--r-- | heatclient/common/utils.py | 17 | ||||
-rw-r--r-- | heatclient/exc.py | 4 | ||||
-rw-r--r-- | heatclient/v1/resources.py | 6 | ||||
-rw-r--r-- | heatclient/v1/shell.py | 18 |
4 files changed, 44 insertions, 1 deletions
diff --git a/heatclient/common/utils.py b/heatclient/common/utils.py index 7a94598..0a264ff 100644 --- a/heatclient/common/utils.py +++ b/heatclient/common/utils.py @@ -19,11 +19,16 @@ import prettytable import sys import textwrap import uuid - +import yaml from heatclient import exc from heatclient.openstack.common import importutils +supported_formats = { + "json": lambda x: json.dumps(x, indent=2), + "yaml": yaml.safe_dump +} + # Decorator for cli-args def arg(*args, **kwargs): @@ -151,3 +156,13 @@ def format_parameters(params): parameters[n] = v return parameters + + +def format_output(output, format='yaml'): + """Format the supplied dict as specified.""" + output_format = format.lower() + try: + return supported_formats[output_format](output) + except KeyError: + raise exc.HTTPUnsupported("The format(%s) is unsupported." + % output_format) diff --git a/heatclient/exc.py b/heatclient/exc.py index 6996bf2..60be0fd 100644 --- a/heatclient/exc.py +++ b/heatclient/exc.py @@ -138,6 +138,10 @@ class HTTPOverLimit(OverLimit): pass +class HTTPUnsupported(HTTPException): + code = 415 + + class HTTPInternalServerError(HTTPException): code = 500 diff --git a/heatclient/v1/resources.py b/heatclient/v1/resources.py index 9ee4564..de88f50 100644 --- a/heatclient/v1/resources.py +++ b/heatclient/v1/resources.py @@ -67,3 +67,9 @@ class ResourceManager(stacks.StackChildManager): '/stacks/%s/resources/%s/metadata' % (stack_id, resource_name)) return body['metadata'] + + def generate_template(self, resource_name): + resp, body = self.api.json_request('GET', + '/resource_types/%s/template' % + resource_name) + return body diff --git a/heatclient/v1/shell.py b/heatclient/v1/shell.py index 93ef0ea..f7101f7 100644 --- a/heatclient/v1/shell.py +++ b/heatclient/v1/shell.py @@ -368,6 +368,24 @@ def do_resource_show(hc, args): utils.print_dict(resource.to_dict(), formatters=formatters) +@utils.arg('resource', metavar='<RESOURCE>', + help='Name of the resource to generate a template for.') +@utils.arg('-F', '--format', metavar='<FORMAT>', + help="The template output format. %s" % utils.supported_formats) +def do_resource_template(hc, args): + '''Generate a template based on a resource.''' + fields = {'resource_name': args.resource} + try: + template = hc.resources.generate_template(**fields) + except exc.HTTPNotFound: + raise exc.CommandError('Resource %s not found.' % args.resource) + else: + if args.format: + print utils.format_output(template, format=args.format) + else: + print utils.format_output(template) + + @utils.arg('id', metavar='<NAME or ID>', help='Name or ID of stack to show the resource metadata for.') @utils.arg('resource', metavar='<RESOURCE>', |