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
This commit is contained in:
Andrew Plunk 2013-08-05 10:28:47 -05:00
parent 2c05c73a6b
commit 68999ba81f
4 changed files with 44 additions and 1 deletions

View File

@ -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)

View File

@ -138,6 +138,10 @@ class HTTPOverLimit(OverLimit):
pass
class HTTPUnsupported(HTTPException):
code = 415
class HTTPInternalServerError(HTTPException):
code = 500

View File

@ -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

View File

@ -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>',