heat api management and template creation functions
[bottlenecks.git] / utils / infra_setup / heat / manager.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from heatclient import client as heat_client
11 from keystoneclient.v2_0 import client as keystone_client
12 from heatclient.common import template_utils
13
14 import heat.common as common
15
16 class HeatManager:
17
18     def __init__(self, credentials):
19         self.user = credentials['user']
20         self.password = credentials['password']
21         self.controller_ip = credentials['controller_ip']
22         self.heat_url = credentials['heat_url']
23         self.auth_uri = credentials['auth_uri']
24         self.project_id = credentials['project']
25         self.heat = None
26
27     def heat_init(self):
28         keystone = keystone_client.Client(username=self.user,
29                                          password=self.password,
30                                          tenant_name=self.project_id,
31                                          auth_url=self.auth_uri)
32         auth_token = keystone.auth_token
33         self.heat_url = keystone.service_catalog.url_for(
34             service_type='orchestration')
35         self.heat = heat_client.Client('1', endpoint=self.heat_url,
36                                       token=auth_token)
37
38     def stacks_list(self, name=None):
39         for stack in self.heat.stacks.list():
40             if (name and stack.stack_name == name) or not name:
41                 common.LOG.info("stack name " + stack.stack_name)
42                 common.LOG.info("stack status " + stack.stack_status)
43
44     def stack_generate(self, template_file, stack_name, parameters):
45         self.heat_init()
46         self.stacks_list()
47         tpl_files, template = template_utils.get_template_contents(template_file)
48
49         fields = {
50             'template': template,
51             'files': dict(list(tpl_files.items()))
52         }
53         self.heat.stacks.create(stack_name=stack_name, files=fields['files'],
54                                 template=template, parameters=parameters)
55         self.stacks_list(stack_name)
56
57     def stack_is_deployed(self, stack_name):
58         self.heat_init()
59         if stack_name in self.heat.stacks.list():
60             return True
61         return False
62
63     def stack_check_status(self, stack_name):
64         for stack in self.heat.stacks.list():
65             if stack.stack_name == stack_name:
66                 return stack.stack_status
67         return 'NOT_FOUND'
68
69     def heat_validate_template(self, heat_template_file):
70         self.heat_init()
71         if not self.heat.stacks.validate(template=open(heat_template_file,
72                                                        'r').read()):
73             raise ValueError('The provided heat template "' +
74                              heat_template_file +
75                              '" is in the wrong format')
76
77     def stack_delete(self, stack_name):
78         self.heat_init()
79         try:
80             for stack in self.heat.stacks.list():
81                 if stack.stack_name == stack_name:
82                     self.heat.stacks.delete(stack.id)
83                     return True
84         except:
85             pass
86         return False