Fixed vm instance instantiation from Heat when using nested resources
[snaps.git] / snaps / config / stack.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 STACK_DELETE_TIMEOUT = 1200
17 STACK_COMPLETE_TIMEOUT = 1200
18 POLL_INTERVAL = 3
19 STATUS_CREATE_FAILED = 'CREATE_FAILED'
20 STATUS_CREATE_COMPLETE = 'CREATE_COMPLETE'
21 STATUS_DELETE_COMPLETE = 'DELETE_COMPLETE'
22 STATUS_DELETE_FAILED = 'DELETE_FAILED'
23
24
25 class StackConfig(object):
26     """
27     Configuration for Heat stack
28     """
29
30     def __init__(self, **kwargs):
31         """
32         Constructor
33         :param name: the stack's name (required)
34         :param template: the heat template in dict() format (required if
35                          template_path attribute is None)
36         :param template_path: the location of the heat template file (required
37                               if template attribute is None)
38         :param resource_files: List of file paths to the resources referred to
39                                by the template
40         :param env_values: dict() of strings for substitution of template
41                            default values (optional)
42         """
43
44         self.name = kwargs.get('name')
45         self.template = kwargs.get('template')
46         self.template_path = kwargs.get('template_path')
47         self.resource_files = kwargs.get('resource_files')
48         self.env_values = kwargs.get('env_values')
49
50         if 'stack_create_timeout' in kwargs:
51             self.stack_create_timeout = kwargs['stack_create_timeout']
52         else:
53             self.stack_create_timeout = STACK_COMPLETE_TIMEOUT
54
55         if not self.name:
56             raise StackConfigError('name is required')
57
58         if not self.template and not self.template_path:
59             raise StackConfigError('A Heat template is required')
60
61         if self.resource_files and not isinstance(self.resource_files, list):
62             raise StackConfigError(
63                 'resource_files must be a list when not None')
64
65     def __eq__(self, other):
66         return (self.name == other.name and
67                 self.template == other.template and
68                 self.template_path == other.template_path and
69                 self.env_values == other.env_values and
70                 self.stack_create_timeout == other.stack_create_timeout)
71
72
73 class StackConfigError(Exception):
74     """
75     Exception to be thrown when an stack configuration are incorrect
76     """