f5a9b88d991365b210c499f0c6a2f71478d33d45
[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
17 class HeatManager:
18
19     def __init__(self, credentials):
20         self.user = credentials['user']
21         self.password = credentials['password']
22         self.controller_ip = credentials['controller_ip']
23         self.heat_url = credentials['heat_url']
24         self.auth_uri = credentials['auth_uri']
25         self.project_id = credentials['project']
26         self.heat = None
27
28     def heat_init(self):
29         keystone = keystone_client.Client(username=self.user,
30                                           password=self.password,
31                                           tenant_name=self.project_id,
32                                           auth_url=self.auth_uri)
33         auth_token = keystone.auth_token
34         self.heat_url = keystone.service_catalog.url_for(
35             service_type='orchestration')
36         self.heat = heat_client.Client('1', endpoint=self.heat_url,
37                                        token=auth_token)
38
39     def stacks_list(self, name=None):
40         for stack in self.heat.stacks.list():
41             if (name and stack.stack_name == name) or not name:
42                 common.LOG.info("stack name " + stack.stack_name)
43                 common.LOG.info("stack status " + stack.stack_status)
44
45     def stack_generate(self, template_file, stack_name, parameters):
46         self.heat_init()
47         self.stacks_list()
48         tpl_files, template = template_utils.get_template_contents(
49             template_file)
50
51         fields = {
52             'template': template,
53             'files': dict(list(tpl_files.items()))
54         }
55         self.heat.stacks.create(stack_name=stack_name, files=fields['files'],
56                                 template=template, parameters=parameters)
57         self.stacks_list(stack_name)
58
59     def stack_is_deployed(self, stack_name):
60         self.heat_init()
61         if stack_name in self.heat.stacks.list():
62             return True
63         return False
64
65     def stack_check_status(self, stack_name):
66         for stack in self.heat.stacks.list():
67             if stack.stack_name == stack_name:
68                 return stack.stack_status
69         return 'NOT_FOUND'
70
71     def heat_validate_template(self, heat_template_file):
72         self.heat_init()
73         if not self.heat.stacks.validate(template=open(heat_template_file,
74                                                        'r').read()):
75             raise ValueError('The provided heat template "' +
76                              heat_template_file +
77                              '" is in the wrong format')
78
79     def stack_delete(self, stack_name):
80         self.heat_init()
81         try:
82             for stack in self.heat.stacks.list():
83                 if stack.stack_name == stack_name:
84                     self.heat.stacks.delete(stack.id)
85                     return True
86         except:
87             pass
88         return False