Created new class NeutronException.
[snaps.git] / snaps / openstack / os_credentials.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 __author__ = 'spisarski'
16
17
18 class OSCreds:
19     """
20     Represents the credentials required to connect with OpenStack servers
21     """
22
23     def __init__(self, **kwargs):
24         """
25         Constructor
26         :param username: The user (required)
27         :param password: The user's password (required)
28         :param auth_url: The OpenStack cloud's authorization URL (required)
29         :param project_name: The project/tenant name
30         :param identity_api_version: The OpenStack's API version to use for
31                                      Keystone clients
32         :param image_api_version: The OpenStack's API version to use for Glance
33                                   clients
34         :param network_api_version: The OpenStack's API version to use for
35                                     Neutron clients
36         :param compute_api_version: The OpenStack's API version to use for Nova
37                                     clients
38         :param user_domain_id: Used for v3 APIs
39         :param project_domain_id: Used for v3 APIs
40         :param interface: Used to specify the endpoint type for keystone as
41                           public, admin, internal
42         :param proxy_settings: instance of os_credentials.ProxySettings class
43         :param cacert: Default to be True for http, or the certification file
44                        is specified for https verification, or set to be False
45                        to disable server certificate verification without cert
46                        file
47         """
48         self.username = kwargs.get('username')
49         self.password = kwargs.get('password')
50         self.auth_url = kwargs.get('auth_url')
51         self.project_name = kwargs.get('project_name')
52         self.identity_api_version = kwargs.get('identity_api_version', 2)
53         self.image_api_version = kwargs.get('image_api_version', 2)
54         self.network_api_version = kwargs.get('network_api_version', 2)
55         self.compute_api_version = kwargs.get('compute_api_version', 2)
56         self.user_domain_id = kwargs.get('user_domain_id', 'default')
57         self.project_domain_id = kwargs.get('project_domain_id', 'default')
58         self.interface = kwargs.get('interface', 'admin')
59         self.cacert = kwargs.get('cacert', True)
60
61         if isinstance(kwargs.get('proxy_settings'), ProxySettings):
62             self.proxy_settings = kwargs.get('proxy_settings')
63         elif isinstance(kwargs.get('proxy_settings'), dict):
64             self.proxy_settings = ProxySettings(**kwargs.get('proxy_settings'))
65         else:
66             self.proxy_settings = None
67
68         if (not self.username or not self.password or not self.auth_url
69                 or not self.project_name):
70             raise OSCredsError('username, password, auth_url, and project_name'
71                                ' are required')
72
73         auth_url_tokens = self.auth_url.split('/')
74         last_token = auth_url_tokens[len(auth_url_tokens) - 1]
75         if len(last_token) == 0:
76             last_token = auth_url_tokens[len(auth_url_tokens) - 2]
77
78         if not last_token.startswith('v'):
79             raise OSCredsError('auth_url last toke must start with \'v\'')
80
81     @property
82     def __str__(self):
83         """Converts object to a string"""
84         return ('OSCreds - username=' + str(self.username) +
85                 ', password=' + str(self.password) +
86                 ', auth_url=' + str(self.auth_url) +
87                 ', project_name=' + str(self.project_name) +
88                 ', identity_api_version=' + str(self.identity_api_version) +
89                 ', image_api_version=' + str(self.image_api_version) +
90                 ', network_api_version=' + str(self.network_api_version) +
91                 ', compute_api_version=' + str(self.compute_api_version) +
92                 ', user_domain_id=' + str(self.user_domain_id) +
93                 ', interface=' + str(self.interface) +
94                 ', proxy_settings=' + str(self.proxy_settings) +
95                 ', cacert=' + str(self.cacert))
96
97
98 class ProxySettings:
99     """
100     Represents the information required for sending traffic (HTTP & SSH)
101     through a proxy
102     """
103
104     def __init__(self, **kwargs):
105         """
106         Constructor
107         :param host: the HTTP proxy host
108         :param port: the HTTP proxy port
109         :param ssh_proxy_cmd: the SSH proxy command string (optional)
110         """
111         self.host = kwargs.get('host')
112         self.port = kwargs.get('port')
113         self.ssh_proxy_cmd = kwargs.get('ssh_proxy_cmd')
114
115         if not self.host or not self.port:
116             raise ProxySettingsError('host & port are required')
117
118     def __str__(self):
119         """Converts object to a string"""
120         return 'ProxySettings - host=' + str(self.host) + \
121                ', port=' + str(self.port) + \
122                ', ssh_proxy_cmd=' + str(self.ssh_proxy_cmd)
123
124
125 class ProxySettingsError(Exception):
126     """
127     Exception to be thrown when an OSCred are invalid
128     """
129
130
131 class OSCredsError(Exception):
132     """
133     Exception to be thrown when an OSCred are invalid
134     """