Fix flake8 errors
[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 keystoneclient.v2_0 import client as keystoneClient
16 from heatclient import client as heatClient
17 from heatclient.common import template_utils
18
19 import experimental_framework.common as common
20
21 __author__ = 'vmriccox'
22
23
24 class HeatManager:
25
26     def __init__(self, credentials):
27         self.ip_controller = credentials['ip_controller']
28         self.heat_url = credentials['heat_url']
29         self.user = credentials['user']
30         self.password = credentials['password']
31         self.auth_uri = credentials['auth_uri']
32         self.project_id = credentials['project']
33         self.heat = None
34
35     def init_heat(self):
36         keystone = keystoneClient.Client(username=self.user,
37                                          password=self.password,
38                                          tenant_name=self.project_id,
39                                          auth_url=self.auth_uri)
40         auth_token = keystone.auth_token
41         self.heat_url = keystone.service_catalog.url_for(
42             service_type='orchestration')
43         self.heat = heatClient.Client('1', endpoint=self.heat_url,
44                                       token=auth_token)
45
46     def print_stacks(self, name=None):
47         for stack in self.heat.stacks.list():
48             if (name and stack.stack_name == name) or not name:
49                 common.LOG.info("Stack Name: " + stack.stack_name)
50                 common.LOG.info("Stack Status: " + stack.stack_status)
51
52     def create_stack(self, template_file, stack_name, parameters):
53         self.init_heat()
54         # self.print_stacks()
55         tpl_files, template = \
56             template_utils.get_template_contents(template_file)
57
58         fields = {
59             'template': template,
60             'files': dict(list(tpl_files.items()))
61         }
62         self.heat.stacks.create(stack_name=stack_name, files=fields['files'],
63                                 template=template, parameters=parameters)
64         self.print_stacks(stack_name)
65
66     def is_stack_deployed(self, stack_name):
67         self.init_heat()
68         if stack_name in self.heat.stacks.list():
69             return True
70         return False
71
72     def check_stack_status(self, stack_name):
73         """
74         Returns a string representing the status of a stack from Heat
75         perspective
76         :param stack_name: Name of the stack to be checked (type: str)
77         :return: (type: str)
78         """
79         for stack in self.heat.stacks.list():
80             if stack.stack_name == stack_name:
81                 return stack.stack_status
82         return 'NOT_FOUND'
83
84     def validate_heat_template(self, heat_template_file):
85         self.init_heat()
86         if not self.heat.stacks.validate(template=open(heat_template_file,
87                                                        'r').read()):
88             raise ValueError('The provided heat template "' +
89                              heat_template_file +
90                              '" is not in the correct format')
91
92     def delete_stack(self, stack_name):
93         self.init_heat()
94         try:
95             for stack in self.heat.stacks.list():
96                 if stack.stack_name == stack_name:
97                     self.heat.stacks.delete(stack.id)
98                     return True
99         except:
100             pass
101         return False