First of several patches for adding volume support.
[snaps.git] / snaps / openstack / utils / cinder_utils.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 import logging
16
17 from cinderclient.client import Client
18
19 from snaps.domain.volume import QoSSpec
20 from snaps.openstack.utils import keystone_utils
21
22 __author__ = 'spisarski'
23
24 logger = logging.getLogger('cinder_utils')
25
26 VERSION_1 = 1
27 VERSION_2 = 2
28 VERSION_3 = 3
29
30 """
31 Utilities for basic neutron API calls
32 """
33
34
35 def cinder_client(os_creds):
36     """
37     Creates and returns a cinder client object
38     :return: the cinder client
39     """
40     return Client(version=os_creds.volume_api_version,
41                   session=keystone_utils.keystone_session(os_creds),
42                   region_name=os_creds.region_name)
43
44
45 def get_qos(cinder, qos_name=None, qos_settings=None):
46     """
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
52     """
53     if not qos_name and not qos_settings:
54         return None
55
56     qos_name = qos_name
57     if qos_settings:
58         qos_name = qos_settings.name
59
60     qoss = cinder.qos_specs.list()
61     for qos in qoss:
62         if qos.name == qos_name:
63             if qos_settings:
64                 if qos_settings.consumer.value == qos.consumer:
65                     return QoSSpec(name=qos.name, spec_id=qos.id,
66                                    consumer=qos.consumer)
67             else:
68                 return QoSSpec(name=qos.name, spec_id=qos.id,
69                                consumer=qos.consumer)
70
71
72 def get_qos_by_id(cinder, qos_id):
73     """
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
78     """
79     qos = cinder.qos_specs.get(qos_id)
80     return QoSSpec(name=qos.name, spec_id=qos.id, consumer=qos.consumer)
81
82
83 def create_qos(cinder, qos_settings):
84     """
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
90     """
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)
95
96
97 def delete_qos(cinder, qos):
98     """
99     Deletes an QoS from OpenStack
100     :param cinder: the cinder client
101     :param qos: the qos domain object to delete
102     """
103     logger.info('Deleting QoS named - %s', qos.name)
104     cinder.qos_specs.delete(qos.id)
105
106
107 class CinderException(Exception):
108     """
109     Exception when calls to the Cinder client cannot be served properly
110     """