Synchronise the openstack bugs
[parser.git] / tosca2heat / heat-translator / translator / osc / v1 / translate.py
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
4 #
5 #        http://www.apache.org/licenses/LICENSE-2.0
6 #
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
11 #   under the License.
12
13 """Translate action implementations"""
14
15 import logging
16 import logging.config
17 import os
18 import sys
19
20 from cliff import command
21
22 from toscaparser.tosca_template import ToscaTemplate
23 from toscaparser.utils.gettextutils import _
24 from translator.common.utils import UrlUtils
25 from translator.conf.config import ConfigProvider
26 from translator.hot.tosca_translator import TOSCATranslator
27 from translator.osc import utils
28
29 conf_file = ConfigProvider.get_translator_logging_file()
30 logging.config.fileConfig(conf_file)
31 log = logging.getLogger('heat-translator')
32
33
34 class TranslateTemplate(command.Command):
35
36     """Translate a template"""
37
38     auth_required = False
39
40     def get_parser(self, prog_name):
41         parser = super(TranslateTemplate, self).get_parser(prog_name)
42         parser.add_argument(
43             '--template-file',
44             metavar='<template-file>',
45             required=True,
46             help='Path to the file that needs to be translated.')
47         parser.add_argument(
48             '--template-type',
49             metavar='<template-type>',
50             required=True,
51             choices=['tosca'],
52             help='Format of the template file.')
53         parser.add_argument(
54             '--output-file',
55             metavar='<output-file>',
56             help='Path to place the translated content.')
57         parser.add_argument(
58             '--parameter',
59             metavar='<key=value>',
60             action=utils.KeyValueAction,
61             help='Set a property for this template '
62                  '(repeat option to set multiple properties)',
63         )
64         parser.add_argument(
65             '--validate-only',
66             metavar='<true or false>',
67             help='Set to true to only validate a template file.',
68             default='false')
69         return parser
70
71     def take_action(self, parsed_args):
72         log.debug(_('Translating the template with input parameters'
73                     '(%s).'), parsed_args)
74         output = None
75
76         if parsed_args.parameter:
77             parsed_params = parsed_args.parameter
78         else:
79             parsed_params = {}
80
81         if parsed_args.template_type == "tosca":
82             path = parsed_args.template_file
83             a_file = os.path.isfile(path)
84             a_url = UrlUtils.validate_url(path) if not a_file else False
85             if a_file or a_url:
86                 validate = parsed_args.validate_only
87                 if validate and validate.lower() == "true":
88                     ToscaTemplate(path, parsed_params, a_file)
89                     msg = (_('The input "%(path)s" successfully passed '
90                              'validation.') % {'path': path})
91                     print(msg)
92                 else:
93                     tosca = ToscaTemplate(path, parsed_params, a_file)
94                     translator = TOSCATranslator(tosca, parsed_params)
95                     output = translator.translate()
96             else:
97                 msg = _('Could not find template file.')
98                 log.error(msg)
99                 sys.stdout.write(msg)
100                 raise SystemExit
101
102         if output:
103             if parsed_args.output_file:
104                 with open(parsed_args.output_file, 'w+') as f:
105                     f.write(output)
106             else:
107                 print(output)