Add support for Python 3
[yardstick.git] / yardstick / vTC / apexlake / experimental_framework / heat_manager.py
1 # Copyright (c) 2015 Intel Research and Development Ireland Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from __future__ import absolute_import
16 from keystoneclient.v2_0 import client as keystoneClient
17 from heatclient import client as heatClient
18 from heatclient.common import template_utils
19
20 import experimental_framework.common as common
21
22 __author__ = 'vmriccox'
23
24
25 class HeatManager:
26
27     def __init__(self, credentials):
28         self.ip_controller = credentials['ip_controller']
29         self.heat_url = credentials['heat_url']
30         self.user = credentials['user']
31         self.password = credentials['password']
32         self.auth_uri = credentials['auth_uri']
33         self.project_id = credentials['project']
34         self.heat = None
35
36     def init_heat(self):
37         keystone = keystoneClient.Client(username=self.user,
38                                          password=self.password,
39                                          tenant_name=self.project_id,
40                                          auth_url=self.auth_uri)
41         auth_token = keystone.auth_token
42         self.heat_url = keystone.service_catalog.url_for(
43             service_type='orchestration')
44         self.heat = heatClient.Client('1', endpoint=self.heat_url,
45                                       token=auth_token)
46
47     def print_stacks(self, name=None):
48         for stack in self.heat.stacks.list():
49             if (name and stack.stack_name == name) or not name:
50                 common.LOG.info("Stack Name: " + stack.stack_name)
51                 common.LOG.info("Stack Status: " + stack.stack_status)
52
53     def create_stack(self, template_file, stack_name, parameters):
54         self.init_heat()
55         # self.print_stacks()
56         tpl_files, template = \
57             template_utils.get_template_contents(template_file)
58
59         fields = {
60             'template': template,
61             'files': dict(list(tpl_files.items()))
62         }
63         self.heat.stacks.create(stack_name=stack_name, files=fields['files'],
64                                 template=template, parameters=parameters)
65         self.print_stacks(stack_name)
66
67     def is_stack_deployed(self, stack_name):
68         self.init_heat()
69         if stack_name in self.heat.stacks.list():
70             return True
71         return False
72
73     def check_stack_status(self, stack_name):
74         """
75         Returns a string representing the status of a stack from Heat
76         perspective
77         :param stack_name: Name of the stack to be checked (type: str)
78         :return: (type: str)
79         """
80         if self.heat:
81             for stack in self.heat.stacks.list():
82                 if stack.stack_name == stack_name:
83                     return stack.stack_status
84         return 'NOT_FOUND'
85
86     def validate_heat_template(self, heat_template_file):
87         self.init_heat()
88         if not self.heat.stacks.validate(template=open(heat_template_file,
89                                                        'r').read()):
90             raise ValueError('The provided heat template "' +
91                              heat_template_file +
92                              '" is not in the correct format')
93
94     def delete_stack(self, stack_name):
95         self.init_heat()
96         try:
97             for stack in self.heat.stacks.list():
98                 if stack.stack_name == stack_name:
99                     self.heat.stacks.delete(stack.id)
100                     return True
101         except Exception:
102             common.LOG.debug("destroy_heat_template", exc_info=True)
103         return False