summaryrefslogtreecommitdiffstats
path: root/snaps/openstack/utils/cinder_utils.py
blob: 61abe22622b2d09acc5fd77589ad198e5a62ac54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
#                    and others.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging

from cinderclient.client import Client
from cinderclient.exceptions import NotFound

from snaps.domain.volume import (
    QoSSpec, VolumeType, VolumeTypeEncryption, Volume)
from snaps.openstack.utils import keystone_utils

__author__ = 'spisarski'

logger = logging.getLogger('cinder_utils')

VERSION_2 = 2
VERSION_3 = 3

"""
Utilities for basic neutron API calls
"""


def cinder_client(os_creds, session=None):
    """
    Creates and returns a cinder client object
    :param os_creds: the credentials for connecting to the OpenStack remote API
    :param session: the keystone session object (optional)
    :return: the cinder client
    """
    if not session:
        session = keystone_utils.keystone_session(os_creds)

    return Client(version=os_creds.volume_api_version,
                  session=session,
                  region_name=os_creds.region_name)


def get_volume(cinder, keystone=None, volume_name=None, volume_settings=None,
               project_name=None):
    """
    Returns an OpenStack volume object for a given name
    :param cinder: the Cinder client
    :param keystone: the Keystone client (required if project_name or
                     volume_settings.project_name is not None
    :param volume_name: the volume name to lookup
    :param volume_settings: the volume settings used for lookups
    :param project_name: the name of the project associated with the volume
    :return: the volume object or None
    """
    if volume_settings:
        volume_name = volume_settings.name

    volumes = cinder.volumes.list()
    for os_volume in volumes:
        if os_volume.name == volume_name:
            project_id = None
            if hasattr(os_volume, 'os-vol-tenant-attr:tenant_id'):
                project_id = getattr(
                    os_volume, 'os-vol-tenant-attr:tenant_id')

            if volume_settings and volume_settings.project_name:
                project_name = volume_settings.project_name

            if project_name:
                project = keystone_utils.get_project_by_id(
                    keystone, project_id)

                if project and project.name == project_name:
                    return __map_os_volume_to_domain(os_volume)
            else:
                return __map_os_volume_to_domain(os_volume)


def __get_os_volume_by_id(cinder, volume_id):
    """
    Returns an OpenStack volume object for a given name
    :param cinder: the Cinder client
    :param volume_id: the volume ID to lookup
    :return: the SNAPS-OO Domain Volume object or None
    """
    return cinder.volumes.get(volume_id)


def get_volume_by_id(cinder, volume_id):
    """
    Returns an OpenStack volume object for a given name
    :param cinder: the Cinder client
    :param volume_id: the volume ID to lookup
    :return: the SNAPS-OO Domain Volume object or None
    """
    os_volume = __get_os_volume_by_id(cinder, volume_id)
    return __map_os_volume_to_domain(os_volume)


def __map_os_volume_to_domain(os_volume):
    """
    Returns a SNAPS-OO domain Volume object that is created by an OpenStack
    Volume object
    :param os_volume: the OpenStack volume object
    :return: Volume domain object
    """
    project_id = None
    if hasattr(os_volume, 'os-vol-tenant-attr:tenant_id'):
        project_id = getattr(
            os_volume, 'os-vol-tenant-attr:tenant_id')

    return Volume(
        name=os_volume.name, volume_id=os_volume.id,
        project_id=project_id, description=os_volume.description,
        size=os_volume.size, vol_type=os_volume.volume_type,
        availability_zone=os_volume.availability_zone,
        multi_attach=os_volume.multiattach,
        attachments=os_volume.attachments)


def get_volume_status(cinder, volume):
    """
    Returns a new OpenStack Volume object for a given OpenStack volume object
    :param cinder: the Cinder client
    :param volume: the domain Volume object
    :return: the OpenStack Volume object
    """
    os_volume = cinder.volumes.get(volume.id)
    return os_volume.status


def create_volume(cinder, keystone, volume_settings):
    """
    Creates and returns OpenStack volume object with an external URL
    :param cinder: the cinder client
    :param keystone: the keystone client
    :param volume_settings: the volume settings object
    :return: the OpenStack volume object
    :raise Exception if using a file and it cannot be found
    """
    project_id = None
    if volume_settings.project_name:
        project = keystone_utils.get_project(
            keystone, project_name=volume_settings.project_name)
        if project:
            project_id = project.id
        else:
            raise KeystoneUtilsException(
                'Project cannot be found with name - '
                + volume_settings.project_name)
    os_volume = cinder.volumes.create(
        name=volume_settings.name,
        project_id=project_id,
        description=volume_settings.description,
        size=volume_settings.size,
        imageRef=volume_settings.image_name,
        volume_type=volume_settings.type_name,
        availability_zone=volume_settings.availability_zone,
        multiattach=volume_settings.multi_attach)

    return __map_os_volume_to_domain(os_volume)


def delete_volume(cinder, volume):
    """
    Deletes an volume from OpenStack
    :param cinder: the cinder client
    :param volume: the volume to delete
    """
    logger.info('Deleting volume named - %s', volume.name)
    return cinder.volumes.delete(volume.id)


def get_volume_type(cinder, volume_type_name=None, volume_type_settings=None):
    """
    Returns an OpenStack volume type object for a given name
    :param cinder: the Cinder client
    :param volume_type_name: the volume type name to lookup
    :param volume_type_settings: the volume type settings used for lookups
    :return: the volume type object or None
    """
    if not volume_type_name and not volume_type_settings:
        return None

    if volume_type_settings:
        volume_type_name = volume_type_settings.name

    volume_types = cinder.volume_types.list()
    for vol_type in volume_types:
        if vol_type.name == volume_type_name:
            encryption = get_volume_encryption_by_type(cinder, vol_type)
            return VolumeType(vol_type.name, vol_type.id, vol_type.is_public,
                              encryption, None)


def __get_os_volume_type_by_id(cinder, volume_type_id):
    """
    Returns an OpenStack volume type object for a given name
    :param cinder: the Cinder client
    :param volume_type_id: the volume_type ID to lookup
    :return: the SNAPS-OO Domain Volume object or None
    """
    try:
        return cinder.volume_types.get(volume_type_id)
    except NotFound:
        logger.info('Volume with ID [%s] does not exist',
                    volume_type_id)


def get_volume_type_by_id(cinder, volume_type_id):
    """
    Returns an OpenStack volume type object for a given name
    :param cinder: the Cinder client
    :param volume_type_id: the volume_type ID to lookup
    :return: the SNAPS-OO Domain Volume object or None
    """
    os_vol_type = __get_os_volume_type_by_id(cinder, volume_type_id)
    if os_vol_type:
        temp_vol_type = VolumeType(os_vol_type.name, os_vol_type.id,
                                   os_vol_type.is_public, None, None)
        encryption = get_volume_encryption_by_type(cinder, temp_vol_type)

        qos_spec = None
        if os_vol_type.qos_specs_id:
            qos_spec = get_qos_by_id(cinder, os_vol_type.qos_specs_id)

        return VolumeType(os_vol_type.name, os_vol_type.id,
                          os_vol_type.is_public, encryption, qos_spec)


def create_volume_type(cinder, type_settings):
    """
    Creates and returns OpenStack volume type object with an external URL
    :param cinder: the cinder client
    :param type_settings: the volume type settings object
    :return: the volume type domain object
    :raise Exception if using a file and it cannot be found
    """
    vol_type = cinder.volume_types.create(
        type_settings.name, type_settings.description,
        type_settings.public)

    vol_encryption = None
    if type_settings.encryption:
        try:
            vol_encryption = create_volume_encryption(
                cinder, vol_type, type_settings.encryption)
        except Exception as e:
            logger.warn('Error creating volume encryption - %s', e)

    qos_spec = None
    if type_settings.qos_spec_name:
        try:
            qos_spec = get_qos(cinder, qos_name=type_settings.qos_spec_name)
            cinder.qos_specs.associate(qos_spec, vol_type.id)
        except NotFound as e:
            logger.warn('Unable to locate qos_spec named %s - %s',
                        type_settings.qos_spec_name, e)

    return VolumeType(vol_type.name, vol_type.id, vol_type.is_public,
                      vol_encryption, qos_spec)


def delete_volume_type(cinder, vol_type):
    """
    Deletes an volume from OpenStack
    :param cinder: the cinder client
    :param vol_type: the VolumeType domain object
    """
    logger.info('Deleting volume named - %s', vol_type.name)
    cinder.volume_types.delete(vol_type.id)


def get_volume_encryption_by_type(cinder, volume_type):
    """
    Returns an OpenStack volume type object for a given name
    :param cinder: the Cinder client
    :param volume_type: the VolumeType domain object
    :return: the VolumeEncryption domain object or None
    """
    os_vol_type = __get_os_volume_type_by_id(cinder, volume_type.id)
    encryption = cinder.volume_encryption_types.get(os_vol_type)
    if hasattr(encryption, 'encryption_id'):
        cipher = None
        if hasattr(encryption, 'cipher'):
            cipher = encryption.cipher
        key_size = None
        if hasattr(encryption, 'key_size'):
            key_size = encryption.key_size
        return VolumeTypeEncryption(
            encryption.encryption_id, encryption.volume_type_id,
            encryption.control_location, encryption.provider, cipher, key_size)


def create_volume_encryption(cinder, volume_type, encryption_settings):
    """
    Creates and returns OpenStack volume type object with an external URL
    :param cinder: the cinder client
    :param volume_type: the VolumeType object to associate the encryption
    :param encryption_settings: the volume type encryption settings object
    :return: the VolumeTypeEncryption domain object
    """
    specs = {'name': encryption_settings.name,
             'provider': encryption_settings.provider_class}
    if encryption_settings.key_size:
        specs['key_size'] = encryption_settings.key_size
    if encryption_settings.provider_class:
        specs['provider_class'] = encryption_settings.provider_class
    if encryption_settings.control_location:
        specs['control_location'] = encryption_settings.control_location.value
    if encryption_settings.cipher:
        specs['cipher'] = encryption_settings.cipher

    encryption = cinder.volume_encryption_types.create(volume_type.id, specs)

    cipher = None
    if hasattr(encryption, 'cipher'):
        cipher = encryption.cipher
    key_size = None
    if hasattr(encryption, 'key_size'):
        key_size = encryption.key_size
    return VolumeTypeEncryption(
        encryption.encryption_id, encryption.volume_type_id,
        encryption.control_location, encryption.provider, cipher, key_size)


def delete_volume_type_encryption(cinder, vol_type):
    """
    Deletes an volume from OpenStack
    :param cinder: the cinder client
    :param vol_type: the associated VolumeType domain object
    """
    logger.info('Deleting volume encryption for volume type - %s',
                vol_type.name)
    os_vol_type = __get_os_volume_type_by_id(cinder, vol_type.id)
    cinder.volume_encryption_types.delete(os_vol_type)


def __get_os_qos(cinder, qos_name=None, qos_settings=None):
    """
    Returns an OpenStack QoS object for a given name
    :param cinder: the Cinder client
    :param qos_name: the qos name to lookup
    :param qos_settings: the qos settings used for lookups
    :return: the qos object or None
    """
    if not qos_name and not qos_settings:
        return None

    if qos_settings:
        qos_name = qos_settings.name

    qoss = cinder.qos_specs.list()
    for qos in qoss:
        if qos.name == qos_name:
            return qos


def get_qos(cinder, qos_name=None, qos_settings=None):
    """
    Returns an OpenStack QoS object for a given name
    :param cinder: the Cinder client
    :param qos_name: the qos name to lookup
    :param qos_settings: the qos settings used for lookups
    :return: the qos object or None
    """
    os_qos = __get_os_qos(cinder, qos_name, qos_settings)
    if os_qos:
        return QoSSpec(name=os_qos.name, spec_id=os_qos.id,
                       consumer=os_qos.consumer)


def get_qos_by_id(cinder, qos_id):
    """
    Returns an OpenStack qos object for a given name
    :param cinder: the Cinder client
    :param qos_id: the qos ID to lookup
    :return: the SNAPS-OO Domain Volume object or None
    """
    qos = cinder.qos_specs.get(qos_id)
    return QoSSpec(name=qos.name, spec_id=qos.id, consumer=qos.consumer)


def create_qos(cinder, qos_settings):
    """
    Creates and returns OpenStack qos object with an external URL
    :param cinder: the cinder client
    :param qos_settings: the qos settings object
    :return: the qos domain object
    :raise Exception if using a file and it cannot be found
    """
    specs = qos_settings.specs
    specs['consumer'] = qos_settings.consumer.value
    qos = cinder.qos_specs.create(qos_settings.name, qos_settings.specs)
    return QoSSpec(name=qos.name, spec_id=qos.id, consumer=qos.consumer)


def delete_qos(cinder, qos):
    """
    Deletes an QoS from OpenStack
    :param cinder: the cinder client
    :param qos: the qos domain object to delete
    """
    logger.info('Deleting QoS named - %s', qos.name)
    cinder.qos_specs.delete(qos.id)


class KeystoneUtilsException(Exception):
    """
    Exception when calls to the Keystone client cannot be served properly
    """