Fixed test to not download images when configured with a disk file path.
[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, username, password, auth_url, project_name, identity_api_version=2, image_api_version=2,
24                  network_api_version=2, compute_api_version=2, user_domain_id='default', project_domain_id='default',
25                  interface="admin", proxy_settings=None, cacert=True):
26         """
27         Constructor
28         :param username: The user (required)
29         :param password: The user's password (required)
30         :param auth_url: The OpenStack cloud's authorization URL (required)
31         :param project_name: The project/tenant name
32         :param identity_api_version: The OpenStack's API version to use for Keystone clients
33         :param image_api_version: The OpenStack's API version to use for Glance clients
34         :param network_api_version: The OpenStack's API version to use for Neutron clients
35         :param compute_api_version: The OpenStack's API version to use for Nova clients
36         :param user_domain_id: Used for v3 APIs
37         :param project_domain_id: Used for v3 APIs
38         :param interface: Used to specify the endpoint type for keystone as public, admin, internal
39         :param proxy_settings: instance of os_credentials.ProxySettings class
40         :param cacert: Default to be True for http, or the certification file is specified for https verification,
41                        or set to be False to disable server certificate verification without cert file
42         """
43         self.username = username
44         self.password = password
45         self.auth_url = auth_url
46         self.project_name = project_name
47         self.identity_api_version = identity_api_version
48         self.image_api_version = image_api_version
49         self.network_api_version = network_api_version
50         self.compute_api_version = compute_api_version
51         self.user_domain_id = user_domain_id
52         self.project_domain_id = project_domain_id
53         self.interface = interface
54         self.proxy_settings = proxy_settings
55         self.cacert = cacert
56
57         if self.proxy_settings and not isinstance(self.proxy_settings, ProxySettings):
58             raise Exception('proxy_settings must be an instance of the class ProxySettings')
59
60         if self.auth_url:
61             auth_url_tokens = self.auth_url.split('/')
62             last_token = auth_url_tokens[len(auth_url_tokens) - 1]
63             if len(last_token) == 0:
64                 last_token = auth_url_tokens[len(auth_url_tokens) - 2]
65
66             if not last_token.startswith('v'):
67                 raise Exception('auth_url last toke must start with \'v\'')
68
69     def __str__(self):
70         """Converts object to a string"""
71         return 'OSCreds - username=' + str(self.username) + \
72                ', password=' + str(self.password) + \
73                ', auth_url=' + str(self.auth_url) + \
74                ', project_name=' + str(self.project_name) + \
75                ', identity_api_version=' + str(self.identity_api_version) + \
76                ', image_api_version=' + str(self.image_api_version) + \
77                ', network_api_version=' + str(self.network_api_version) + \
78                ', compute_api_version=' + str(self.compute_api_version) + \
79                ', user_domain_id=' + str(self.user_domain_id) + \
80                ', interface=' + str(self.interface) + \
81                ', proxy_settings=' + str(self.proxy_settings) + \
82                ', cacert=' + str(self.cacert)
83
84
85 class ProxySettings:
86     """
87     Represents the information required for sending traffic (HTTP & SSH) through a proxy
88     """
89
90     def __init__(self, host, port, ssh_proxy_cmd=None):
91         """
92         Constructor
93         :param host: the HTTP proxy host
94         :param port: the HTTP proxy port
95         :param ssh_proxy_cmd: the SSH proxy command string (optional)
96         """
97         # TODO - Add necessary fields here when adding support for secure proxies
98
99         self.host = host
100         self.port = port
101         self.ssh_proxy_cmd = ssh_proxy_cmd
102
103         if not self.host and not self.port:
104             raise Exception('host & port are required')
105
106     def __str__(self):
107         """Converts object to a string"""
108         return 'ProxySettings - host=' + str(self.host) + \
109                ', port=' + str(self.port) + \
110                ', ssh_proxy_cmd=' + str(self.ssh_proxy_cmd)