summaryrefslogtreecommitdiffstats
path: root/tosca2heat/heat-translator-0.3.0/translator/osc
diff options
context:
space:
mode:
Diffstat (limited to 'tosca2heat/heat-translator-0.3.0/translator/osc')
-rw-r--r--tosca2heat/heat-translator-0.3.0/translator/osc/__init__.py0
-rw-r--r--tosca2heat/heat-translator-0.3.0/translator/osc/osc_plugin.py41
-rw-r--r--tosca2heat/heat-translator-0.3.0/translator/osc/utils.py45
-rw-r--r--tosca2heat/heat-translator-0.3.0/translator/osc/v1/__init__.py0
-rw-r--r--tosca2heat/heat-translator-0.3.0/translator/osc/v1/translate.py94
5 files changed, 180 insertions, 0 deletions
diff --git a/tosca2heat/heat-translator-0.3.0/translator/osc/__init__.py b/tosca2heat/heat-translator-0.3.0/translator/osc/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tosca2heat/heat-translator-0.3.0/translator/osc/__init__.py
diff --git a/tosca2heat/heat-translator-0.3.0/translator/osc/osc_plugin.py b/tosca2heat/heat-translator-0.3.0/translator/osc/osc_plugin.py
new file mode 100644
index 0000000..6d3d25a
--- /dev/null
+++ b/tosca2heat/heat-translator-0.3.0/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='<translator-api-version>',
+ 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/tosca2heat/heat-translator-0.3.0/translator/osc/utils.py b/tosca2heat/heat-translator-0.3.0/translator/osc/utils.py
new file mode 100644
index 0000000..e8a6814
--- /dev/null
+++ b/tosca2heat/heat-translator-0.3.0/translator/osc/utils.py
@@ -0,0 +1,45 @@
+# 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 argparse
+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', '')
+
+
+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)
diff --git a/tosca2heat/heat-translator-0.3.0/translator/osc/v1/__init__.py b/tosca2heat/heat-translator-0.3.0/translator/osc/v1/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tosca2heat/heat-translator-0.3.0/translator/osc/v1/__init__.py
diff --git a/tosca2heat/heat-translator-0.3.0/translator/osc/v1/translate.py b/tosca2heat/heat-translator-0.3.0/translator/osc/v1/translate.py
new file mode 100644
index 0000000..c356042
--- /dev/null
+++ b/tosca2heat/heat-translator-0.3.0/translator/osc/v1/translate.py
@@ -0,0 +1,94 @@
+# 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.config
+import os
+import sys
+
+from cliff import command
+
+from toscaparser.tosca_template import ToscaTemplate
+from translator.common.utils import UrlUtils
+from translator.hot.tosca_translator import TOSCATranslator
+from translator.osc import utils
+
+
+class TranslateTemplate(command.Command):
+ """Translate a template"""
+
+ log = logging.getLogger('heat-translator' + '.TranslateTemplate')
+ auth_required = False
+
+ def get_parser(self, prog_name):
+ parser = super(TranslateTemplate, self).get_parser(prog_name)
+ parser.add_argument(
+ '--template-file',
+ metavar='<template-file>',
+ required=True,
+ help='Path to the file that needs to be translated.')
+ parser.add_argument(
+ '--template-type',
+ metavar='<template-type>',
+ required=True,
+ choices=['tosca'],
+ help='Format of the template file.')
+ parser.add_argument(
+ '--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)',
+ )
+ parser.add_argument(
+ '--validate-only',
+ metavar='<true or false>',
+ help='Set to true to only validate a template file.',
+ default='false')
+ return parser
+
+ def take_action(self, parsed_args):
+ self.log.debug('take_action(%s)', parsed_args)
+ output = None
+
+ if parsed_args.parameter:
+ parsed_params = parsed_args.parameter
+ else:
+ parsed_params = {}
+
+ if parsed_args.template_type == "tosca":
+ path = parsed_args.template_file
+ a_file = os.path.isfile(path)
+ a_url = UrlUtils.validate_url(path) if not a_file else False
+ if a_file or a_url:
+ validate = parsed_args.validate_only
+ if validate and validate.lower() == "true":
+ ToscaTemplate(path, parsed_params, a_file)
+ else:
+ tosca = ToscaTemplate(path, parsed_params, a_file)
+ translator = TOSCATranslator(tosca, parsed_params)
+ output = translator.translate()
+ else:
+ sys.stdout.write('Could not find template file.')
+ raise SystemExit
+
+ if output:
+ if parsed_args.output_file:
+ with open(parsed_args.output_file, 'w+') as f:
+ f.write(output)
+ else:
+ print(output)