Removed project query restriction when looking up network for port creation.
[snaps.git] / snaps / config / keypair.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 from neutronclient.common.utils import str2bool
16
17
18 class KeypairConfig(object):
19     """
20     Class representing a keypair configuration
21     """
22
23     def __init__(self, **kwargs):
24         """
25         Constructor - all parameters are optional
26         :param name: The keypair name.
27         :param public_filepath: The path to/from the filesystem where the
28                                 public key file is or will be stored
29         :param private_filepath: The path where the generated private key file
30                                  will be stored
31         :param key_size: The number of bytes for the key size when it needs to
32                          be generated (Must be >=512 default 1024)
33         :param delete_on_clean: when True, the key files will be deleted when
34                                 OpenStackKeypair#clean() is called
35         :return:
36         """
37
38         self.name = kwargs.get('name')
39         self.public_filepath = kwargs.get('public_filepath')
40         self.private_filepath = kwargs.get('private_filepath')
41         self.key_size = int(kwargs.get('key_size', 1024))
42
43         if kwargs.get('delete_on_clean') is not None:
44             if isinstance(kwargs.get('delete_on_clean'), bool):
45                 self.delete_on_clean = kwargs.get('delete_on_clean')
46             else:
47                 self.delete_on_clean = str2bool(kwargs.get('delete_on_clean'))
48         else:
49             self.delete_on_clean = None
50
51         if not self.name:
52             raise KeypairConfigError('Name is a required attribute')
53
54         if self.key_size < 512:
55             raise KeypairConfigError('key_size must be >=512')
56
57
58 class KeypairConfigError(Exception):
59     """
60     Exception to be thrown when keypair settings are incorrect
61     """