1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
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:
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 from cinderclient.exceptions import NotFound
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
25 __author__ = 'spisarski'
27 logger = logging.getLogger('create_volume_type')
30 class OpenStackVolumeType(OpenStackVolumeObject):
32 Class responsible for managing an volume in OpenStack
35 def __init__(self, os_creds, volume_type_settings):
38 :param os_creds: The OpenStack connection credentials
39 :param volume_type_settings: The volume type settings
42 super(self.__class__, self).__init__(os_creds)
44 self.volume_type_settings = volume_type_settings
45 self.__volume_type = None
49 Loads the existing Volume
50 :return: The Volume domain object or None
52 super(self.__class__, self).initialize()
54 self.__volume_type = cinder_utils.get_volume_type(
55 self._cinder, volume_type_settings=self.volume_type_settings)
57 return self.__volume_type
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
67 if not self.__volume_type:
68 self.__volume_type = cinder_utils.create_volume_type(
69 self._cinder, self.volume_type_settings)
71 'Created volume type with name - %s',
72 self.volume_type_settings.name)
74 return self.__volume_type
78 Cleanse environment of all artifacts
81 if self.__volume_type:
83 cinder_utils.delete_volume_type(self._cinder,
88 self.__volume_type = None
90 super(self.__class__, self).clean()
92 def get_volume_type(self):
94 Returns the domain Volume object as it was populated when create() was
98 return self.__volume_type
101 class VolumeTypeSettings(VolumeTypeConfig):
103 Class to hold the configuration settings required for creating OpenStack
108 def __init__(self, **kwargs):
109 from warnings import warn
110 warn('Use snaps.config.volume_type.VolumeTypeConfig instead',
112 super(self.__class__, self).__init__(**kwargs)
115 class VolumeTypeEncryptionSettings(VolumeTypeEncryptionConfig):
117 Class to hold the configuration settings required for creating OpenStack
118 Volume Type Encryption objects
122 def __init__(self, **kwargs):
123 from warnings import warn
124 warn('Use snaps.config.volume_type.VolumeTypeEncryptionConfig instead',
126 super(self.__class__, self).__init__(**kwargs)
129 class VolumeTypeCreationError(Exception):
131 Exception to be thrown when an volume cannot be created
134 def __init__(self, message):
135 Exception.__init__(self, message)