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.
15 from neutronclient.common.utils import str2bool
17 from snaps import file_utils
18 from snaps.openstack.utils import glance_utils, keystone_utils
20 __author__ = 'spisarski'
25 Represents the credentials required to connect with OpenStack servers
28 def __init__(self, **kwargs):
31 :param username: The user (required)
32 :param password: The user's password (required)
33 :param auth_url: The OpenStack cloud's authorization URL (required)
34 :param project_name: The project/tenant name
35 :param identity_api_version: The OpenStack's API version to use for
37 :param image_api_version: The OpenStack's API version to use for Glance
39 :param network_api_version: The OpenStack's API version to use for
41 :param compute_api_version: The OpenStack's API version to use for Nova
43 :param heat_api_version: The OpenStack's API version to use for Heat
45 :param user_domain_id: Used for v3 APIs (default='default')
46 :param project_domain_id: Used for v3 APIs (default='default')
47 :param interface: Used to specify the endpoint type for keystone as
48 public, admin, internal
49 :param proxy_settings: instance of os_credentials.ProxySettings class
50 :param cacert: Default to be True for http, or the certification file
51 is specified for https verification, or set to be False
52 to disable server certificate verification without cert
54 :param region_name: the region (optional default = None)
56 self.username = kwargs.get('username')
57 self.password = kwargs.get('password')
58 self.auth_url = kwargs.get('auth_url')
59 self.project_name = kwargs.get('project_name')
61 if kwargs.get('identity_api_version') is None:
62 self.identity_api_version = keystone_utils.V2_VERSION_NUM
64 self.identity_api_version = float(kwargs['identity_api_version'])
66 if kwargs.get('image_api_version') is None:
67 self.image_api_version = glance_utils.VERSION_2
69 self.image_api_version = float(kwargs['image_api_version'])
71 if kwargs.get('network_api_version') is None:
72 self.network_api_version = 2
74 self.network_api_version = float(kwargs['network_api_version'])
76 if kwargs.get('compute_api_version') is None:
77 self.compute_api_version = 2
79 self.compute_api_version = float(kwargs['compute_api_version'])
81 if kwargs.get('heat_api_version') is None:
82 self.heat_api_version = 1
84 self.heat_api_version = float(kwargs['heat_api_version'])
86 if kwargs.get('user_domain_id') is None:
87 self.user_domain_id = 'default'
89 self.user_domain_id = kwargs['user_domain_id']
91 if kwargs.get('project_domain_id') is None:
92 self.project_domain_id = 'default'
94 self.project_domain_id = kwargs['project_domain_id']
96 if kwargs.get('interface') is None:
97 self.interface = 'admin'
99 self.interface = kwargs['interface']
101 self.region_name = kwargs.get('region_name', None)
104 if kwargs.get('cacert') is not None:
105 if isinstance(kwargs.get('cacert'), str):
106 if file_utils.file_exists(kwargs['cacert']):
107 self.cacert = kwargs['cacert']
109 self.cacert = str2bool(kwargs['cacert'])
111 self.cacert = kwargs['cacert']
113 if isinstance(kwargs.get('proxy_settings'), ProxySettings):
114 self.proxy_settings = kwargs.get('proxy_settings')
115 elif isinstance(kwargs.get('proxy_settings'), dict):
116 self.proxy_settings = ProxySettings(**kwargs.get('proxy_settings'))
118 self.proxy_settings = None
120 if (not self.username or not self.password or not self.auth_url
121 or not self.project_name):
122 raise OSCredsError('username, password, auth_url, and project_name'
125 auth_url_tokens = self.auth_url.split('/')
126 last_token = auth_url_tokens[len(auth_url_tokens) - 1]
127 if len(last_token) == 0:
128 last_token = auth_url_tokens[len(auth_url_tokens) - 2]
130 if not last_token.startswith('v'):
131 raise OSCredsError('auth_url last toke must start with \'v\'')
135 """Converts object to a string"""
136 return ('OSCreds - username=' + str(self.username) +
137 ', password=' + str(self.password) +
138 ', auth_url=' + str(self.auth_url) +
139 ', project_name=' + str(self.project_name) +
140 ', identity_api_version=' + str(self.identity_api_version) +
141 ', image_api_version=' + str(self.image_api_version) +
142 ', network_api_version=' + str(self.network_api_version) +
143 ', compute_api_version=' + str(self.compute_api_version) +
144 ', user_domain_id=' + str(self.user_domain_id) +
145 ', interface=' + str(self.interface) +
146 ', proxy_settings=' + str(self.proxy_settings) +
147 ', cacert=' + str(self.cacert))
152 Represents the information required for sending traffic (HTTP & SSH)
156 def __init__(self, **kwargs):
159 :param host: the HTTP proxy host
160 :param port: the HTTP proxy port
161 :param https_host: the HTTPS proxy host (defaults to host)
162 :param https_port: the HTTPS proxy port (defaults to port)
163 :param port: the HTTP proxy port
164 :param ssh_proxy_cmd: the SSH proxy command string (optional)
166 self.host = kwargs.get('host')
167 self.port = kwargs.get('port')
169 self.https_host = kwargs.get('https_host', self.host)
170 self.https_port = kwargs.get('https_port', self.port)
172 self.ssh_proxy_cmd = kwargs.get('ssh_proxy_cmd')
174 if not self.host or not self.port:
175 raise ProxySettingsError('host & port are required')
178 """Converts object to a string"""
179 return 'ProxySettings - host=' + str(self.host) + \
180 ', port=' + str(self.port) + \
181 ', ssh_proxy_cmd=' + str(self.ssh_proxy_cmd)
184 class ProxySettingsError(Exception):
186 Exception to be thrown when an OSCred are invalid
190 class OSCredsError(Exception):
192 Exception to be thrown when an OSCred are invalid