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 of the enum type Consumer
117 :param specs: dict of key/values
120 self.name = kwargs.get('name')
122 if kwargs.get('consumer'):
123 self.consumer = map_consumer(kwargs['consumer'])
127 self.specs = kwargs.get('specs')
131 if not self.name or not self.consumer:
132 raise QoSSettingsError(
133 "The attributes name and consumer are required")
136 def map_consumer(consumer):
138 Takes a the protocol value maps it to the Consumer enum. When None return
140 :param consumer: the value to map to the Enum
141 :return: the Protocol enum object
142 :raise: Exception if value is invalid
146 elif isinstance(consumer, Consumer):
149 proto_str = str(consumer)
150 if proto_str == 'front-end':
151 return Consumer.front_end
152 elif proto_str == 'back-end':
153 return Consumer.back_end
154 elif proto_str == 'both':
157 raise QoSSettingsError('Invalid Consumer - ' + proto_str)
160 class QoSSettingsError(Exception):
162 Exception to be thrown when an qos settings are incorrect
165 def __init__(self, message):
166 Exception.__init__(self, message)
169 class QoSCreationError(Exception):
171 Exception to be thrown when an qos cannot be created
174 def __init__(self, message):
175 Exception.__init__(self, message)