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.
19 from cinderclient.exceptions import NotFound
21 from snaps.openstack.openstack_creator import OpenStackVolumeObject
22 from snaps.openstack.utils import cinder_utils
24 __author__ = 'spisarski'
26 logger = logging.getLogger('create_qos')
28 IMAGE_ACTIVE_TIMEOUT = 600
30 STATUS_ACTIVE = 'active'
33 class OpenStackQoS(OpenStackVolumeObject):
35 Class responsible for managing an qos in OpenStack
38 def __init__(self, os_creds, qos_settings):
41 :param os_creds: The OpenStack connection credentials
42 :param qos_settings: The qos settings
45 super(self.__class__, self).__init__(os_creds)
47 self.qos_settings = qos_settings
52 Loads the existing QoS
53 :return: The QoS domain object or None
55 super(self.__class__, self).initialize()
57 self.__qos = cinder_utils.get_qos(
58 self._cinder, qos_settings=self.qos_settings)
64 Creates the qos in OpenStack if it does not already exist and returns
66 :return: The QoS domain object or None
71 self.__qos = cinder_utils.create_qos(
72 self._cinder, self.qos_settings)
75 'Created qos with name - %s', self.qos_settings.name)
81 Cleanse environment of all artifacts
86 cinder_utils.delete_qos(self._cinder, self.__qos)
94 Returns the domain QoS object as it was populated when create() was
101 class Consumer(enum.Enum):
103 QoS Specification consumer types
105 front_end = 'front-end'
106 back_end = 'back-end'
111 def __init__(self, **kwargs):
114 :param name: the qos's name (required)
115 :param consumer: the qos's consumer type (required)
116 :param specs: dict of key/values
119 self.name = kwargs.get('name')
121 if kwargs.get('consumer'):
122 self.consumer = map_consumer(kwargs['consumer'])
126 self.specs = kwargs.get('specs')
130 if not self.name or not self.consumer:
131 raise QoSSettingsError(
132 "The attributes name and consumer are required")
135 def map_consumer(consumer):
137 Takes a the protocol value maps it to the Consumer enum. When None return
139 :param consumer: the value to map to the Enum
140 :return: the Protocol enum object
141 :raise: Exception if value is invalid
145 elif isinstance(consumer, Consumer):
148 proto_str = str(consumer)
149 if proto_str == 'front-end':
150 return Consumer.front_end
151 elif proto_str == 'back-end':
152 return Consumer.back_end
153 elif proto_str == 'both':
156 raise QoSSettingsError('Invalid Consumer - ' + proto_str)
159 class QoSSettingsError(Exception):
161 Exception to be thrown when an qos settings are incorrect
164 def __init__(self, message):
165 Exception.__init__(self, message)
168 class QoSCreationError(Exception):
170 Exception to be thrown when an qos cannot be created
173 def __init__(self, message):
174 Exception.__init__(self, message)