Add support for parameters to translate template

Some templates can have substitutions for values used throughout
the template. We should expose this feature via the command
line.

partially implements bp openstack-client-heat-translator

Change-Id: Iee4c21285afa77e7e5b36437928b3d62c10e465e
This commit is contained in:
Steve Martinelli 2015-06-15 15:31:34 -04:00
parent ebc7ea79d7
commit 528b3665c2
2 changed files with 29 additions and 2 deletions

View File

@ -13,6 +13,7 @@
"""Common client utilities"""
import argparse
import os
@ -27,3 +28,18 @@ def env(*vars, **kwargs):
if value:
return value
return kwargs.get('default', '')
class KeyValueAction(argparse.Action):
"""A custom action to parse arguments as key=value pairs. """
def __call__(self, parser, namespace, values, option_string=None):
# Make sure we have an empty dict rather than None
if getattr(namespace, self.dest, None) is None:
setattr(namespace, self.dest, {})
# Add value if an assignment else remove it
if '=' in values:
getattr(namespace, self.dest, {}).update([values.split('=', 1)])
else:
getattr(namespace, self.dest, {}).pop(values, None)

View File

@ -19,6 +19,7 @@ import sys
from cliff import command
from translator.hot.tosca_translator import TOSCATranslator
from translator.osc import utils
from translator.toscalib.tosca_template import ToscaTemplate
@ -45,6 +46,13 @@ class TranslateTemplate(command.Command):
'--output-file',
metavar='<output-file>',
help='Path to place the translated content.')
parser.add_argument(
'--parameter',
metavar='<key=value>',
action=utils.KeyValueAction,
help='Set a property for this template '
'(repeat option to set multiple properties)',
)
return parser
def take_action(self, parsed_args):
@ -54,8 +62,11 @@ class TranslateTemplate(command.Command):
sys.stdout.write('Could not find template file.')
raise SystemExit
# TODO(stevemar): parsed_params doesn't default nicely
parsed_params = {}
if parsed_args.parameter:
parsed_params = parsed_args.parameter
else:
parsed_params = {}
if parsed_args.template_type == "tosca":
tosca = ToscaTemplate(parsed_args.template_file)
translator = TOSCATranslator(tosca, parsed_params)