From eb913cd991f65d40dd6fd4d1669980f0250ac45f Mon Sep 17 00:00:00 2001 From: Steve Martinelli Date: Mon, 15 Jun 2015 13:09:43 -0400 Subject: [PATCH] Create an OSC plugin for heat-translator Creating the framework for an OSC plugin for heat-translator. This will make adding new commands easier, incorporates them into OSC, and provides the capabilities of authentication should it be required in the future (using heatclient after translating a template). partially implements bp openstack-client-heat-translator Change-Id: Ie477eac9acd9ed26f00069947af0d25b3c7e67b1 --- requirements.txt | 1 + setup.cfg | 7 ++++ translator/osc/__init__.py | 0 translator/osc/osc_plugin.py | 41 +++++++++++++++++++++++ translator/osc/utils.py | 29 +++++++++++++++++ translator/osc/v1/__init__.py | 0 translator/osc/v1/translate.py | 59 ++++++++++++++++++++++++++++++++++ 7 files changed, 137 insertions(+) create mode 100644 translator/osc/__init__.py create mode 100644 translator/osc/osc_plugin.py create mode 100644 translator/osc/utils.py create mode 100644 translator/osc/v1/__init__.py create mode 100644 translator/osc/v1/translate.py diff --git a/requirements.txt b/requirements.txt index 31c29fa7..fc588e2d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ pbr>=0.6,!=0.7,<1.0 Babel>=1.3 +cliff>=1.10.0 # Apache-2.0 PyYAML>=3.1.0 python_dateutil>=2.4.0 six>=1.9.0 diff --git a/setup.cfg b/setup.cfg index b84b4fde..27be7fda 100644 --- a/setup.cfg +++ b/setup.cfg @@ -23,6 +23,13 @@ classifier = packages = translator +[entry_points] +openstack.cli.extension = + translator = translator.osc.osc_plugin + +openstack.translator.v1 = + translate_template = translator.osc.v1.translate:TranslateTemplate + [build_sphinx] source-dir = doc/source build-dir = doc/build diff --git a/translator/osc/__init__.py b/translator/osc/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/translator/osc/osc_plugin.py b/translator/osc/osc_plugin.py new file mode 100644 index 00000000..6d3d25a7 --- /dev/null +++ b/translator/osc/osc_plugin.py @@ -0,0 +1,41 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from translator.osc import utils + +DEFAULT_TRANSLATOR_API_VERSION = '1' +API_VERSION_OPTION = 'os_translator_api_version' +API_NAME = 'translator' +API_VERSIONS = { + '1': 'translator.v1.client.Client', +} + + +def make_client(instance): + # NOTE(stevemar): We don't need a client because + # heat-translator itself is a command line tool + pass + + +def build_option_parser(parser): + """Hook to add global options.""" + + parser.add_argument( + '--os-translator-api-version', + metavar='', + default=utils.env( + 'OS_TRANSLATOR_API_VERSION', + default=DEFAULT_TRANSLATOR_API_VERSION), + help='Translator API version, default=' + + DEFAULT_TRANSLATOR_API_VERSION + + ' (Env: OS_TRANSLATOR_API_VERSION)') + return parser diff --git a/translator/osc/utils.py b/translator/osc/utils.py new file mode 100644 index 00000000..5ca1e0fa --- /dev/null +++ b/translator/osc/utils.py @@ -0,0 +1,29 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +"""Common client utilities""" + +import os + + +def env(*vars, **kwargs): + """Search for the first defined of possibly many env vars + + Returns the first environment variable defined in vars, or + returns the default defined in kwargs. + """ + for v in vars: + value = os.environ.get(v, None) + if value: + return value + return kwargs.get('default', '') diff --git a/translator/osc/v1/__init__.py b/translator/osc/v1/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/translator/osc/v1/translate.py b/translator/osc/v1/translate.py new file mode 100644 index 00000000..ca730c5c --- /dev/null +++ b/translator/osc/v1/translate.py @@ -0,0 +1,59 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +"""Translate action implementations""" + +import logging +import os +import sys + +from cliff import command + +from translator.hot.tosca_translator import TOSCATranslator +from translator.toscalib.tosca_template import ToscaTemplate + + +class TranslateTemplate(command.Command): + """Translate a template""" + + log = logging.getLogger(__name__ + '.TranslateTemplate') + auth_required = False + + def get_parser(self, prog_name): + parser = super(TranslateTemplate, self).get_parser(prog_name) + parser.add_argument( + '--template-file', + metavar='', + required=True, + help='Path to the file that needs to be translated.') + parser.add_argument( + '--template-type', + metavar='', + required=True, + choices=['tosca'], + help='Format of the template file.') + return parser + + def take_action(self, parsed_args): + self.log.debug('take_action(%s)', parsed_args) + + if not os.path.isfile(parsed_args.template_file): + sys.stdout.write('Could not find template file.') + raise SystemExit + + # TODO(stevemar): parsed_params doesn't default nicely + parsed_params = {} + if parsed_args.template_type == "tosca": + tosca = ToscaTemplate(parsed_args.template_file) + translator = TOSCATranslator(tosca, parsed_params) + output = translator.translate() + print(output)