Refactoring of StackSettings to extend StackConfig
[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 env_values: dict() of strings for substitution of template
39                            default values (optional)
40         """
41
42         self.name = kwargs.get('name')
43         self.template = kwargs.get('template')
44         self.template_path = kwargs.get('template_path')
45         self.env_values = kwargs.get('env_values')
46         if 'stack_create_timeout' in kwargs:
47             self.stack_create_timeout = kwargs['stack_create_timeout']
48         else:
49             self.stack_create_timeout = STACK_COMPLETE_TIMEOUT
50
51         if not self.name:
52             raise StackConfigError('name is required')
53
54         if not self.template and not self.template_path:
55             raise StackConfigError('A Heat template is required')
56
57     def __eq__(self, other):
58         return (self.name == other.name and
59                 self.template == other.template and
60                 self.template_path == other.template_path and
61                 self.env_values == other.env_values and
62                 self.stack_create_timeout == other.stack_create_timeout)
63
64
65 class StackConfigError(Exception):
66     """
67     Exception to be thrown when an stack configuration are incorrect
68     """