1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
5 # http://www.apache.org/licenses/LICENSE-2.0
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
13 """Translate action implementations"""
20 from cliff import command
22 from toscaparser.tosca_template import ToscaTemplate
23 from toscaparser.utils.gettextutils import _
24 from translator.common.utils import UrlUtils
25 from translator.hot.tosca_translator import TOSCATranslator
26 from translator.osc import utils
29 logging.config.fileConfig('heat_translator_logging.conf')
30 log = logging.getLogger('heat-translator')
33 class TranslateTemplate(command.Command):
35 """Translate a template"""
39 def get_parser(self, prog_name):
40 parser = super(TranslateTemplate, self).get_parser(prog_name)
43 metavar='<template-file>',
45 help='Path to the file that needs to be translated.')
48 metavar='<template-type>',
51 help='Format of the template file.')
54 metavar='<output-file>',
55 help='Path to place the translated content.')
58 metavar='<key=value>',
59 action=utils.KeyValueAction,
60 help='Set a property for this template '
61 '(repeat option to set multiple properties)',
65 metavar='<true or false>',
66 help='Set to true to only validate a template file.',
70 def take_action(self, parsed_args):
71 log.debug(_('Translating the template with input parameters'
72 '(%s).'), parsed_args)
75 if parsed_args.parameter:
76 parsed_params = parsed_args.parameter
80 if parsed_args.template_type == "tosca":
81 path = parsed_args.template_file
82 a_file = os.path.isfile(path)
83 a_url = UrlUtils.validate_url(path) if not a_file else False
85 validate = parsed_args.validate_only
86 if validate and validate.lower() == "true":
87 ToscaTemplate(path, parsed_params, a_file)
88 msg = (_('The input "%(path)s" successfully passed '
89 'validation.') % {'path': path})
92 tosca = ToscaTemplate(path, parsed_params, a_file)
93 translator = TOSCATranslator(tosca, parsed_params)
94 output = translator.translate()
96 msg = _('Could not find template file.')
102 if parsed_args.output_file:
103 with open(parsed_args.output_file, 'w+') as f: