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.
16 from neutronclient.common.utils import str2bool
19 class VolumeTypeConfig(object):
20 def __init__(self, **kwargs):
23 :param name: the volume's name (required)
24 :param description: the volume's name (optional)
25 :param encryption: VolumeTypeEncryptionConfig (optional)
26 :param qos_spec_name: name of the QoS Spec to associate (optional)
27 :param public: volume visibility where True denotes global
30 TODO - Implement project_access parameter that will associate this
31 VolumeType to a list of project names
34 self.name = kwargs.get('name')
35 self.description = kwargs.get('description')
36 self.qos_spec_name = kwargs.get('qos_spec_name')
38 if 'encryption' in kwargs:
39 if isinstance(kwargs['encryption'], dict):
40 self.encryption = VolumeTypeEncryptionConfig(
41 **kwargs['encryption'])
42 elif isinstance(kwargs['encryption'],
43 VolumeTypeEncryptionConfig):
44 self.encryption = kwargs['encryption']
46 self.encryption = None
48 if 'public' in kwargs:
49 if isinstance(kwargs['public'], str):
50 self.public = str2bool(kwargs['public'])
52 self.public = kwargs['public']
57 raise VolumeTypeConfigError("The attribute name is required")
59 def __eq__(self, other):
60 return (self.name == other.name
61 and self.description == other.description
62 and self.qos_spec_name == other.qos_spec_name
63 and self.encryption == other.encryption
64 and self.public == other.public)
67 class ControlLocation(enum.Enum):
69 QoS Specification consumer types
71 front_end = 'front-end'
75 class VolumeTypeEncryptionConfig(object):
76 def __init__(self, **kwargs):
79 :param name: the volume's name (required)
80 :param provider_class: the volume's provider class (e.g. LuksEncryptor)
81 :param control_location: the notional service where encryption is
82 performed (e.g., front-end=Nova). The default
84 :param cipher: the encryption algorithm/mode to use
85 (e.g., aes-xts-plain64). If the field is left empty,
86 the provider default will be used
87 :param key_size: the size of the encryption key, in bits
88 (e.g., 128, 256). If the field is left empty, the
89 provider default will be used
92 self.name = kwargs.get('name')
93 self.provider_class = kwargs.get('provider_class')
94 self.control_location = kwargs.get('control_location')
95 if kwargs.get('control_location'):
96 self.control_location = map_control_location(
97 kwargs['control_location'])
99 self.control_location = None
101 self.cipher = kwargs.get('cipher')
102 self.key_size = kwargs.get('key_size')
104 if (not self.name or not self.provider_class
105 or not self.control_location):
106 raise VolumeTypeConfigError(
107 'The attributes name, provider_class, and control_location '
110 def __eq__(self, other):
111 return (self.name == other.name
112 and self.provider_class == other.provider_class
113 and self.control_location == other.control_location
114 and self.cipher == other.cipher
115 and self.key_size == other.key_size)
118 def map_control_location(control_location):
120 Takes a the protocol value maps it to the Consumer enum. When None return
122 :param control_location: the value to map to the Enum
123 :return: a ControlLocation enum object
124 :raise: Exception if control_location parameter is invalid
126 if not control_location:
128 elif isinstance(control_location, ControlLocation):
129 return control_location
131 proto_str = str(control_location)
132 if proto_str == 'front-end':
133 return ControlLocation.front_end
134 elif proto_str == 'back-end':
135 return ControlLocation.back_end
137 raise VolumeTypeConfigError('Invalid Consumer - ' + proto_str)
140 class VolumeTypeConfigError(Exception):
142 Exception to be thrown when an volume settings are incorrect
145 def __init__(self, message):
146 Exception.__init__(self, message)