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.
17 from cinderclient.client import Client
19 from snaps.domain.volume import QoSSpec
20 from snaps.openstack.utils import keystone_utils
22 __author__ = 'spisarski'
24 logger = logging.getLogger('cinder_utils')
31 Utilities for basic neutron API calls
35 def cinder_client(os_creds):
37 Creates and returns a cinder client object
38 :return: the cinder client
40 return Client(version=os_creds.volume_api_version,
41 session=keystone_utils.keystone_session(os_creds),
42 region_name=os_creds.region_name)
45 def get_qos(cinder, qos_name=None, qos_settings=None):
47 Returns an OpenStack QoS object for a given name
48 :param cinder: the Cinder client
49 :param qos_name: the qos name to lookup
50 :param qos_settings: the qos settings used for lookups
51 :return: the qos object or None
53 if not qos_name and not qos_settings:
58 qos_name = qos_settings.name
60 qoss = cinder.qos_specs.list()
62 if qos.name == qos_name:
64 if qos_settings.consumer.value == qos.consumer:
65 return QoSSpec(name=qos.name, spec_id=qos.id,
66 consumer=qos.consumer)
68 return QoSSpec(name=qos.name, spec_id=qos.id,
69 consumer=qos.consumer)
72 def get_qos_by_id(cinder, qos_id):
74 Returns an OpenStack qos object for a given name
75 :param cinder: the Cinder client
76 :param qos_id: the qos ID to lookup
77 :return: the SNAPS-OO Domain Volume object or None
79 qos = cinder.qos_specs.get(qos_id)
80 return QoSSpec(name=qos.name, spec_id=qos.id, consumer=qos.consumer)
83 def create_qos(cinder, qos_settings):
85 Creates and returns OpenStack qos object with an external URL
86 :param cinder: the cinder client
87 :param qos_settings: the qos settings object
88 :return: the qos domain object
89 :raise Exception if using a file and it cannot be found
91 specs = qos_settings.specs
92 specs['consumer'] = qos_settings.consumer.value
93 qos = cinder.qos_specs.create(qos_settings.name, qos_settings.specs)
94 return QoSSpec(name=qos.name, spec_id=qos.id, consumer=qos.consumer)
97 def delete_qos(cinder, qos):
99 Deletes an QoS from OpenStack
100 :param cinder: the cinder client
101 :param qos: the qos domain object to delete
103 logger.info('Deleting QoS named - %s', qos.name)
104 cinder.qos_specs.delete(qos.id)
107 class CinderException(Exception):
109 Exception when calls to the Cinder client cannot be served properly