c4ba76d7f340c18e9ce500e123a44982f910fcd3
[snaps.git] / snaps / openstack / cluster_template.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 import logging
17
18 from magnumclient.common.apiclient.exceptions import NotFound
19
20 from snaps.openstack.openstack_creator import OpenStackMagnumObject
21 from snaps.openstack.utils import magnum_utils
22
23 __author__ = 'spisarski'
24
25 logger = logging.getLogger('cluster_template')
26
27
28 class OpenStackClusterTemplate(OpenStackMagnumObject):
29     """
30     Class responsible for managing an volume in OpenStack
31     """
32
33     def __init__(self, os_creds, cluster_template_config):
34         """
35         Constructor
36         :param os_creds: The OpenStack connection credentials
37         :param cluster_template_config: The volume type settings
38         :return:
39         """
40         super(self.__class__, self).__init__(os_creds)
41
42         self.cluster_template_config = cluster_template_config
43         self.__cluster_template = None
44
45     def initialize(self):
46         """
47         Loads the existing Volume
48         :return: The Volume domain object or None
49         """
50         super(self.__class__, self).initialize()
51
52         self.__cluster_template = magnum_utils.get_cluster_template(
53             self._magnum, template_config=self.cluster_template_config)
54
55         return self.__cluster_template
56
57     def create(self):
58         """
59         Creates the volume in OpenStack if it does not already exist and
60         returns the domain Volume object
61         :return: The Volume domain object or None
62         """
63         self.initialize()
64
65         if not self.__cluster_template:
66             self.__cluster_template = magnum_utils.create_cluster_template(
67                 self._magnum, self.cluster_template_config)
68             logger.info(
69                 'Created volume type with name - %s',
70                 self.cluster_template_config.name)
71
72         return self.__cluster_template
73
74     def clean(self):
75         """
76         Cleanse environment of all artifacts
77         :return: void
78         """
79         if self.__cluster_template:
80             try:
81                 magnum_utils.delete_cluster_template(
82                     self._magnum, self.__cluster_template.id)
83             except NotFound:
84                 pass
85
86         self.__cluster_template = None
87
88     def get_cluster_template(self):
89         """
90         Returns the domain Volume object as it was populated when create() was
91         called
92         :return: the object
93         """
94         return self.__cluster_template