Merge "Cleaned up extra network created in the test test_set_one_port_two_ip_two_subn...
[snaps.git] / snaps / config / volume_type.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 enum
16 from neutronclient.common.utils import str2bool
17
18
19 class VolumeTypeConfig(object):
20     def __init__(self, **kwargs):
21         """
22         Constructor
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
28                        (default - False)
29
30         TODO - Implement project_access parameter that will associate this
31         VolumeType to a list of project names
32         """
33
34         self.name = kwargs.get('name')
35         self.description = kwargs.get('description')
36         self.qos_spec_name = kwargs.get('qos_spec_name')
37
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']
45         else:
46             self.encryption = None
47
48         if 'public' in kwargs:
49             if isinstance(kwargs['public'], str):
50                 self.public = str2bool(kwargs['public'])
51             else:
52                 self.public = kwargs['public']
53         else:
54             self.public = False
55
56         if not self.name:
57             raise VolumeTypeConfigError("The attribute name is required")
58
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)
65
66
67 class ControlLocation(enum.Enum):
68     """
69     QoS Specification consumer types
70     """
71     front_end = 'front-end'
72     back_end = 'back-end'
73
74
75 class VolumeTypeEncryptionConfig(object):
76     def __init__(self, **kwargs):
77         """
78         Constructor
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
83                                  value is 'front-end.'
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
90         """
91
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'])
98         else:
99             self.control_location = None
100
101         self.cipher = kwargs.get('cipher')
102         self.key_size = kwargs.get('key_size')
103
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 '
108                 'are required')
109
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)
116
117
118 def map_control_location(control_location):
119     """
120     Takes a the protocol value maps it to the Consumer enum. When None return
121     None
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
125     """
126     if not control_location:
127         return None
128     elif isinstance(control_location, ControlLocation):
129         return control_location
130     else:
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
136         else:
137             raise VolumeTypeConfigError('Invalid Consumer - ' + proto_str)
138
139
140 class VolumeTypeConfigError(Exception):
141     """
142     Exception to be thrown when an volume settings are incorrect
143     """
144
145     def __init__(self, message):
146         Exception.__init__(self, message)