1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 from heatclient.client import Client
19 from heatclient.common.template_format import yaml_loader
20 from oslo_serialization import jsonutils
22 from snaps import file_utils
23 from snaps.domain.stack import Stack
25 from snaps.openstack.utils import keystone_utils
27 __author__ = 'spisarski'
29 logger = logging.getLogger('heat_utils')
32 def heat_client(os_creds):
34 Retrieves the Heat client
35 :param os_creds: the OpenStack credentials
38 logger.debug('Retrieving Nova Client')
39 return Client(1, session=keystone_utils.keystone_session(os_creds))
42 def get_stack_by_name(heat_cli, stack_name):
44 Returns a domain Stack object
45 :param heat_cli: the OpenStack heat client
46 :param stack_name: the name of the heat stack
47 :return: the Stack domain object else None
49 stacks = heat_cli.stacks.list(**{'name': stack_name})
51 return Stack(name=stack.identifier, stack_id=stack.id)
56 def get_stack_by_id(heat_cli, stack_id):
58 Returns a domain Stack object for a given ID
59 :param heat_cli: the OpenStack heat client
60 :param stack_id: the ID of the heat stack to retrieve
61 :return: the Stack domain object else None
63 stack = heat_cli.stacks.get(stack_id)
64 return Stack(name=stack.identifier, stack_id=stack.id)
67 def get_stack_status(heat_cli, stack_id):
69 Returns the current status of the Heat stack
70 :param heat_cli: the OpenStack heat client
71 :param stack_id: the ID of the heat stack to retrieve
74 return heat_cli.stacks.get(stack_id).stack_status
77 def get_stack_outputs(heat_cli, stack_id):
79 Returns a domain Stack object for a given ID
80 :param heat_cli: the OpenStack heat client
81 :param stack_id: the ID of the heat stack to retrieve
82 :return: the Stack domain object else None
84 stack = heat_cli.stacks.get(stack_id)
88 def create_stack(heat_cli, stack_settings):
90 Executes an Ansible playbook to the given host
91 :param heat_cli: the OpenStack heat client object
92 :param stack_settings: the stack configuration
93 :return: the Stack domain object
97 if stack_settings.template:
98 args['template'] = stack_settings.template
100 args['template'] = parse_heat_template_str(file_utils.read_file(stack_settings.template_path))
101 args['stack_name'] = stack_settings.name
103 if stack_settings.env_values:
104 args['parameters'] = stack_settings.env_values
106 stack = heat_cli.stacks.create(**args)
108 return get_stack_by_id(heat_cli, stack_id=stack['stack']['id'])
111 def delete_stack(heat_cli, stack):
113 Deletes the Heat stack
114 :param heat_cli: the OpenStack heat client object
115 :param stack: the OpenStack Heat stack object
117 heat_cli.stacks.delete(stack.id)
120 def parse_heat_template_str(tmpl_str):
121 """Takes a heat template string, performs some simple validation and returns a dict containing the parsed structure.
122 This function supports both JSON and YAML Heat template formats.
124 if tmpl_str.startswith('{'):
125 tpl = jsonutils.loads(tmpl_str)
128 tpl = yaml.load(tmpl_str, Loader=yaml_loader)
129 except yaml.YAMLError as yea:
130 raise ValueError(yea)
134 # Looking for supported version keys in the loaded template
135 if not ('HeatTemplateFormatVersion' in tpl or
136 'heat_template_version' in tpl or
137 'AWSTemplateFormatVersion' in tpl):
138 raise ValueError("Template format version not found.")