summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/heat_utils.py
diff options
context:
space:
mode:
authorspisarski <s.pisarski@cablelabs.com>2017-06-02 15:31:53 -0600
committerspisarski <s.pisarski@cablelabs.com>2017-06-05 13:22:49 -0600
commit48da17bfedb683b624faf08d2e0b7552d56cff21 (patch)
tree9219ed4ab9872b26f7ff685c4d3378212a641d08 /snaps/openstack/utils/heat_utils.py
parentc01f193cad22895f86f726f588a46e44ed4ab68a (diff)
Added support for applying Heat Templates
Second patch expanded support to both files and dict() objects. Third patch exposes new accessor for status and outputs. JIRA: SNAPS-86 Change-Id: Ie7e8d883b4cc1a08dbe851fc9cbf663396334909 Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/openstack/utils/heat_utils.py')
-rw-r--r--snaps/openstack/utils/heat_utils.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/snaps/openstack/utils/heat_utils.py b/snaps/openstack/utils/heat_utils.py
new file mode 100644
index 0000000..d40e3b9
--- /dev/null
+++ b/snaps/openstack/utils/heat_utils.py
@@ -0,0 +1,139 @@
+# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
+# and others. All rights reserved.
+#
+# 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.
+import logging
+
+import yaml
+from heatclient.client import Client
+from heatclient.common.template_format import yaml_loader
+from oslo_serialization import jsonutils
+
+from snaps import file_utils
+from snaps.domain.stack import Stack
+
+from snaps.openstack.utils import keystone_utils
+
+__author__ = 'spisarski'
+
+logger = logging.getLogger('heat_utils')
+
+
+def heat_client(os_creds):
+ """
+ Retrieves the Heat client
+ :param os_creds: the OpenStack credentials
+ :return: the client
+ """
+ logger.debug('Retrieving Nova Client')
+ return Client(1, session=keystone_utils.keystone_session(os_creds))
+
+
+def get_stack_by_name(heat_cli, stack_name):
+ """
+ Returns a domain Stack object
+ :param heat_cli: the OpenStack heat client
+ :param stack_name: the name of the heat stack
+ :return: the Stack domain object else None
+ """
+ stacks = heat_cli.stacks.list(**{'name': stack_name})
+ for stack in stacks:
+ return Stack(name=stack.identifier, stack_id=stack.id)
+
+ return None
+
+
+def get_stack_by_id(heat_cli, stack_id):
+ """
+ Returns a domain Stack object for a given ID
+ :param heat_cli: the OpenStack heat client
+ :param stack_id: the ID of the heat stack to retrieve
+ :return: the Stack domain object else None
+ """
+ stack = heat_cli.stacks.get(stack_id)
+ return Stack(name=stack.identifier, stack_id=stack.id)
+
+
+def get_stack_status(heat_cli, stack_id):
+ """
+ Returns the current status of the Heat stack
+ :param heat_cli: the OpenStack heat client
+ :param stack_id: the ID of the heat stack to retrieve
+ :return:
+ """
+ return heat_cli.stacks.get(stack_id).stack_status
+
+
+def get_stack_outputs(heat_cli, stack_id):
+ """
+ Returns a domain Stack object for a given ID
+ :param heat_cli: the OpenStack heat client
+ :param stack_id: the ID of the heat stack to retrieve
+ :return: the Stack domain object else None
+ """
+ stack = heat_cli.stacks.get(stack_id)
+ return stack.outputs
+
+
+def create_stack(heat_cli, stack_settings):
+ """
+ Executes an Ansible playbook to the given host
+ :param heat_cli: the OpenStack heat client object
+ :param stack_settings: the stack configuration
+ :return: the Stack domain object
+ """
+ args = dict()
+
+ if stack_settings.template:
+ args['template'] = stack_settings.template
+ else:
+ args['template'] = parse_heat_template_str(file_utils.read_file(stack_settings.template_path))
+ args['stack_name'] = stack_settings.name
+
+ if stack_settings.env_values:
+ args['parameters'] = stack_settings.env_values
+
+ stack = heat_cli.stacks.create(**args)
+
+ return get_stack_by_id(heat_cli, stack_id=stack['stack']['id'])
+
+
+def delete_stack(heat_cli, stack):
+ """
+ Deletes the Heat stack
+ :param heat_cli: the OpenStack heat client object
+ :param stack: the OpenStack Heat stack object
+ """
+ heat_cli.stacks.delete(stack.id)
+
+
+def parse_heat_template_str(tmpl_str):
+ """Takes a heat template string, performs some simple validation and returns a dict containing the parsed structure.
+ This function supports both JSON and YAML Heat template formats.
+ """
+ if tmpl_str.startswith('{'):
+ tpl = jsonutils.loads(tmpl_str)
+ else:
+ try:
+ tpl = yaml.load(tmpl_str, Loader=yaml_loader)
+ except yaml.YAMLError as yea:
+ raise ValueError(yea)
+ else:
+ if tpl is None:
+ tpl = {}
+ # Looking for supported version keys in the loaded template
+ if not ('HeatTemplateFormatVersion' in tpl or
+ 'heat_template_version' in tpl or
+ 'AWSTemplateFormatVersion' in tpl):
+ raise ValueError("Template format version not found.")
+ return tpl