a7198d869024b36c9b119aab5ed7ff497173c651
[snaps.git] / snaps / openstack / create_volume_type.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 cinderclient.exceptions import NotFound
19
20 from snaps.config.volume_type import (
21     VolumeTypeConfig,  VolumeTypeEncryptionConfig)
22 from snaps.openstack.openstack_creator import OpenStackVolumeObject
23 from snaps.openstack.utils import cinder_utils
24
25 __author__ = 'spisarski'
26
27 logger = logging.getLogger('create_volume_type')
28
29
30 class OpenStackVolumeType(OpenStackVolumeObject):
31     """
32     Class responsible for managing an volume in OpenStack
33     """
34
35     def __init__(self, os_creds, volume_type_settings):
36         """
37         Constructor
38         :param os_creds: The OpenStack connection credentials
39         :param volume_type_settings: The volume type settings
40         :return:
41         """
42         super(self.__class__, self).__init__(os_creds)
43
44         self.volume_type_settings = volume_type_settings
45         self.__volume_type = None
46
47     def initialize(self):
48         """
49         Loads the existing Volume
50         :return: The Volume domain object or None
51         """
52         super(self.__class__, self).initialize()
53
54         self.__volume_type = cinder_utils.get_volume_type(
55             self._cinder, volume_type_settings=self.volume_type_settings)
56
57         return self.__volume_type
58
59     def create(self):
60         """
61         Creates the volume in OpenStack if it does not already exist and
62         returns the domain Volume object
63         :return: The Volume domain object or None
64         """
65         self.initialize()
66
67         if not self.__volume_type:
68             self.__volume_type = cinder_utils.create_volume_type(
69                 self._cinder, self.volume_type_settings)
70             logger.info(
71                 'Created volume type with name - %s',
72                 self.volume_type_settings.name)
73
74         return self.__volume_type
75
76     def clean(self):
77         """
78         Cleanse environment of all artifacts
79         :return: void
80         """
81         if self.__volume_type:
82             try:
83                 cinder_utils.delete_volume_type(self._cinder,
84                                                 self.__volume_type)
85             except NotFound:
86                 pass
87
88         self.__volume_type = None
89
90     def get_volume_type(self):
91         """
92         Returns the domain Volume object as it was populated when create() was
93         called
94         :return: the object
95         """
96         return self.__volume_type
97
98
99 class VolumeTypeSettings(VolumeTypeConfig):
100     """
101     Class to hold the configuration settings required for creating OpenStack
102     Volume Type objects
103     deprecated
104     """
105
106     def __init__(self, **kwargs):
107         from warnings import warn
108         warn('Use snaps.config.volume_type.VolumeTypeConfig instead',
109              DeprecationWarning)
110         super(self.__class__, self).__init__(**kwargs)
111
112
113 class VolumeTypeEncryptionSettings(VolumeTypeEncryptionConfig):
114     """
115     Class to hold the configuration settings required for creating OpenStack
116     Volume Type Encryption objects
117     deprecated
118     """
119
120     def __init__(self, **kwargs):
121         from warnings import warn
122         warn('Use snaps.config.volume_type.VolumeTypeEncryptionConfig instead',
123              DeprecationWarning)
124         super(self.__class__, self).__init__(**kwargs)
125
126
127 class VolumeTypeCreationError(Exception):
128     """
129     Exception to be thrown when an volume cannot be created
130     """
131
132     def __init__(self, message):
133         Exception.__init__(self, message)