2014-08-25 10:37:27 +12:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
|
2014-11-21 08:51:41 +10:00
|
|
|
import os
|
|
|
|
|
2016-02-05 14:36:58 +05:30
|
|
|
from heatclient import client as heat_client
|
2016-05-10 23:09:51 +08:00
|
|
|
from keystoneauth1.identity.generic import password
|
|
|
|
from keystoneauth1 import session
|
2020-08-28 15:52:39 +00:00
|
|
|
from keystoneclient.v3 import client as kc_v3
|
2016-02-05 14:36:58 +05:30
|
|
|
from novaclient import client as nova_client
|
|
|
|
from swiftclient import client as swift_client
|
2014-08-25 10:37:27 +12:00
|
|
|
|
|
|
|
|
2016-01-29 22:23:21 +05:30
|
|
|
class KeystoneWrapperClient(object):
|
|
|
|
"""Wrapper object for keystone client
|
|
|
|
|
2016-02-05 14:36:58 +05:30
|
|
|
This wraps keystone client, so we can encpasulate certain
|
2016-01-29 22:23:21 +05:30
|
|
|
added properties like auth_token, project_id etc.
|
|
|
|
"""
|
|
|
|
def __init__(self, auth_plugin, verify=True):
|
|
|
|
self.auth_plugin = auth_plugin
|
|
|
|
self.session = session.Session(
|
|
|
|
auth=auth_plugin,
|
|
|
|
verify=verify)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auth_token(self):
|
|
|
|
return self.auth_plugin.get_token(self.session)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auth_ref(self):
|
|
|
|
return self.auth_plugin.get_access(self.session)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def project_id(self):
|
|
|
|
return self.auth_plugin.get_project_id(self.session)
|
|
|
|
|
|
|
|
def get_endpoint_url(self, service_type, region=None):
|
|
|
|
kwargs = {
|
|
|
|
'service_type': service_type,
|
2016-05-10 23:09:51 +08:00
|
|
|
'region_name': region}
|
2016-01-29 22:23:21 +05:30
|
|
|
return self.auth_ref.service_catalog.url_for(**kwargs)
|
|
|
|
|
|
|
|
|
2014-08-25 10:37:27 +12:00
|
|
|
class ClientManager(object):
|
2015-09-24 16:49:03 +03:00
|
|
|
"""Provides access to the official python clients for calling various APIs.
|
|
|
|
|
2014-08-25 10:37:27 +12:00
|
|
|
Manager that provides access to the official python clients for
|
|
|
|
calling various OpenStack APIs.
|
|
|
|
"""
|
|
|
|
|
2024-07-16 19:41:13 +09:00
|
|
|
HEAT_API_VERSION = '1'
|
|
|
|
KEYSTONE_API_VERSION = '3'
|
2016-06-02 14:39:13 +08:00
|
|
|
NOVA_API_VERSION = '2.1'
|
2014-08-25 10:37:27 +12:00
|
|
|
|
2016-08-01 22:05:37 +00:00
|
|
|
def __init__(self, conf, admin_credentials=False):
|
2014-08-25 10:37:27 +12:00
|
|
|
self.conf = conf
|
2016-08-01 22:05:37 +00:00
|
|
|
self.admin_credentials = admin_credentials
|
|
|
|
|
2016-03-07 04:47:00 -08:00
|
|
|
self.insecure = self.conf.disable_ssl_certificate_validation
|
|
|
|
self.ca_file = self.conf.ca_file
|
2016-05-03 11:19:41 +05:30
|
|
|
|
2014-08-25 10:37:27 +12:00
|
|
|
self.identity_client = self._get_identity_client()
|
2020-08-28 15:52:39 +00:00
|
|
|
self.keystone_client = self._get_keystone_client()
|
2014-08-25 10:37:27 +12:00
|
|
|
self.orchestration_client = self._get_orchestration_client()
|
|
|
|
self.compute_client = self._get_compute_client()
|
2015-02-03 18:53:30 +10:00
|
|
|
self.object_client = self._get_object_client()
|
2014-08-25 10:37:27 +12:00
|
|
|
|
2016-08-01 22:05:37 +00:00
|
|
|
def _username(self):
|
|
|
|
if self.admin_credentials:
|
|
|
|
return self.conf.admin_username
|
|
|
|
return self.conf.username
|
|
|
|
|
|
|
|
def _password(self):
|
|
|
|
if self.admin_credentials:
|
|
|
|
return self.conf.admin_password
|
|
|
|
return self.conf.password
|
|
|
|
|
2017-04-11 09:54:07 +05:30
|
|
|
def _project_name(self):
|
2016-08-01 22:05:37 +00:00
|
|
|
if self.admin_credentials:
|
2017-04-11 09:54:07 +05:30
|
|
|
return self.conf.admin_project_name
|
|
|
|
return self.conf.project_name
|
2016-08-01 22:05:37 +00:00
|
|
|
|
2014-08-25 10:37:27 +12:00
|
|
|
def _get_orchestration_client(self):
|
2014-11-21 08:51:41 +10:00
|
|
|
endpoint = os.environ.get('HEAT_URL')
|
|
|
|
if os.environ.get('OS_NO_CLIENT_AUTH') == 'True':
|
2017-02-18 13:02:20 +05:30
|
|
|
session = None
|
2014-11-21 08:51:41 +10:00
|
|
|
else:
|
2017-02-18 13:02:20 +05:30
|
|
|
session = self.identity_client.session
|
|
|
|
|
|
|
|
return heat_client.Client(
|
2024-07-16 19:41:13 +09:00
|
|
|
self.HEAT_API_VERSION,
|
2017-02-18 13:02:20 +05:30
|
|
|
endpoint,
|
|
|
|
session=session,
|
|
|
|
endpoint_type='publicURL',
|
|
|
|
service_type='orchestration',
|
|
|
|
region_name=self.conf.region,
|
|
|
|
username=self._username(),
|
|
|
|
password=self._password())
|
2014-08-25 10:37:27 +12:00
|
|
|
|
|
|
|
def _get_identity_client(self):
|
2016-12-01 09:54:37 +05:30
|
|
|
user_domain_id = self.conf.user_domain_id
|
|
|
|
project_domain_id = self.conf.project_domain_id
|
2016-03-21 21:09:20 +05:30
|
|
|
user_domain_name = self.conf.user_domain_name
|
|
|
|
project_domain_name = self.conf.project_domain_name
|
2016-01-29 22:23:21 +05:30
|
|
|
kwargs = {
|
2016-08-01 22:05:37 +00:00
|
|
|
'username': self._username(),
|
|
|
|
'password': self._password(),
|
2017-04-11 09:54:07 +05:30
|
|
|
'project_name': self._project_name(),
|
2024-07-16 19:41:13 +09:00
|
|
|
'auth_url': self.conf.auth_url,
|
|
|
|
'user_domain_id': user_domain_id,
|
|
|
|
'project_domain_id': project_domain_id,
|
|
|
|
'user_domain_name': user_domain_name,
|
|
|
|
'project_domain_name': project_domain_name
|
2016-01-29 22:23:21 +05:30
|
|
|
}
|
|
|
|
auth = password.Password(**kwargs)
|
2016-03-07 04:47:00 -08:00
|
|
|
if self.insecure:
|
|
|
|
verify_cert = False
|
|
|
|
else:
|
|
|
|
verify_cert = self.ca_file or True
|
|
|
|
|
|
|
|
return KeystoneWrapperClient(auth, verify_cert)
|
2014-08-25 10:37:27 +12:00
|
|
|
|
2020-08-28 15:52:39 +00:00
|
|
|
def _get_keystone_client(self):
|
|
|
|
# Create our default Keystone client to use in testing
|
|
|
|
return kc_v3.Client(
|
|
|
|
session=self.identity_client.session,
|
2021-11-04 10:04:58 +11:00
|
|
|
interface='publicURL',
|
2020-08-28 15:52:39 +00:00
|
|
|
region_name=self.conf.region)
|
|
|
|
|
2014-08-25 10:37:27 +12:00
|
|
|
def _get_compute_client(self):
|
|
|
|
# Create our default Nova client to use in testing
|
2016-02-05 14:36:58 +05:30
|
|
|
return nova_client.Client(
|
2016-06-02 14:39:13 +08:00
|
|
|
self.NOVA_API_VERSION,
|
2016-05-03 11:19:41 +05:30
|
|
|
session=self.identity_client.session,
|
2014-08-25 10:37:27 +12:00
|
|
|
service_type='compute',
|
|
|
|
endpoint_type='publicURL',
|
2017-02-14 18:59:15 +05:30
|
|
|
region_name=self.conf.region,
|
2017-01-20 07:53:38 +05:30
|
|
|
os_cache=False,
|
2014-08-25 10:37:27 +12:00
|
|
|
http_log_debug=True)
|
|
|
|
|
2015-02-03 18:53:30 +10:00
|
|
|
def _get_object_client(self):
|
|
|
|
args = {
|
2024-07-16 19:41:13 +09:00
|
|
|
'auth_version': self.KEYSTONE_API_VERSION,
|
2017-02-14 18:59:15 +05:30
|
|
|
'session': self.identity_client.session,
|
|
|
|
'os_options': {'endpoint_type': 'publicURL',
|
|
|
|
'region_name': self.conf.region,
|
|
|
|
'service_type': 'object-store'},
|
2015-02-03 18:53:30 +10:00
|
|
|
}
|
2016-02-05 14:36:58 +05:30
|
|
|
return swift_client.Connection(**args)
|