aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/vTC/apexlake/experimental_framework/heat_manager.py
blob: 7400ebd21e2fb917fc4b2484ede37b767aceeea2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright (c) 2015 Intel Research and Development Ireland Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from keystoneclient.v2_0 import client as keystoneClient
from heatclient import client as heatClient
from heatclient.common import template_utils

import experimental_framework.common as common

__author__ = 'vmriccox'


class HeatManager:

    def __init__(self, credentials):
        self.ip_controller = credentials['ip_controller']
        self.heat_url = credentials['heat_url']
        self.user = credentials['user']
        self.password = credentials['password']
        self.auth_uri = credentials['auth_uri']
        self.project_id = credentials['project']
        self.heat = None

    def init_heat(self):
        keystone = keystoneClient.Client(username=self.user,
                                         password=self.password,
                                         tenant_name=self.project_id,
                                         auth_url=self.auth_uri)
        auth_token = keystone.auth_token
        self.heat_url = keystone.service_catalog.url_for(
            service_type='orchestration')
        self.heat = heatClient.Client('1', endpoint=self.heat_url,
                                      token=auth_token)

    def print_stacks(self, name=None):
        for stack in self.heat.stacks.list():
            if (name and stack.stack_name == name) or not name:
                common.LOG.info("Stack Name: " + stack.stack_name)
                common.LOG.info("Stack Status: " + stack.stack_status)

    def create_stack(self, template_file, stack_name, parameters):
        self.init_heat()
        # self.print_stacks()
        tpl_files, template = \
            template_utils.get_template_contents(template_file)

        fields = {
            'template': template,
            'files': dict(list(tpl_files.items()))
        }
        self.heat.stacks.create(stack_name=stack_name, files=fields['files'],
                                template=template, parameters=parameters)
        self.print_stacks(stack_name)

    def is_stack_deployed(self, stack_name):
        self.init_heat()
        if stack_name in self.heat.stacks.list():
            return True
        return False

    def check_stack_status(self, stack_name):
        """
        Returns a string representing the status of a stack from Heat
        perspective
        :param stack_name: Name of the stack to be checked (type: str)
        :return: (type: str)
        """
        if self.heat:
            for stack in self.heat.stacks.list():
                if stack.stack_name == stack_name:
                    return stack.stack_status
        return 'NOT_FOUND'

    def validate_heat_template(self, heat_template_file):
        self.init_heat()
        if not self.heat.stacks.validate(template=open(heat_template_file,
                                                       'r').read()):
            raise ValueError('The provided heat template "' +
                             heat_template_file +
                             '" is not in the correct format')

    def delete_stack(self, stack_name):
        self.init_heat()
        try:
            for stack in self.heat.stacks.list():
                if stack.stack_name == stack_name:
                    self.heat.stacks.delete(stack.id)
                    return True
        except Exception as e:
            common.LOG.debug(e.message)
            pass
        return False