1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
# (c) Copyright 2014-2016 Hewlett-Packard Development Company, L.P.
#
# 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.
import socket
from keystoneauth1 import loading as kaloading
from freezerclient import utils
from freezerclient.v2.managers import actions
from freezerclient.v2.managers import backups
from freezerclient.v2.managers import clients
from freezerclient.v2.managers import jobs
from freezerclient.v2.managers import sessions
FREEZER_SERVICE_TYPE = 'backup'
class Client(object):
"""Client for the OpenStack Disaster Recovery v2 API.
"""
def __init__(self, token=None, username=None, password=None,
auth_url=None, session=None, endpoint=None,
endpoint_type=None, opts=None, project_name=None,
user_domain_name=None, user_domain_id=None,
project_domain_name=None, project_domain_id=None,
cert=None, cacert=None, insecure=False, project_id=None):
"""
Initialize a new client for the Disaster Recovery v2 API.
:param token: keystone token
:param username: openstack username
:param password: openstack password
:param auth_url: keystone-api endpoint
:param session: keystone.Session
:param endpoint: freezer-api endpoint
:param endpoint_type: type of endpoint
:param opts: a namespace to store all keystone data
:param project_name: only for version 3
:param user_domain_name: only for version 3
:param user_domain_id: only for version 3
:param project_domain_name: only for version 3
:param project_domain_id: only for version 3
:param insecure: The verification arguments to pass to requests.
These are of the same form as requests expects,
so True or False to verify (or not) against system
certificates or a path to a bundle or CA certs to
check against or None for requests to
attempt to locate and use certificates. (optional,
defaults to True)
:param cert: Path to cert
:return: freezerclient.Client
"""
self.project_id = project_id
if opts is None:
self.opts = utils.Namespace({})
self.opts.os_token = token or None
self.opts.os_username = username or None
self.opts.os_password = password or None
self.opts.os_auth_url = auth_url or None
self.opts.os_backup_url = endpoint or None
self.opts.os_endpoint_type = endpoint_type or None
self.opts.os_project_name = project_name or None
self.opts.os_project_id = project_id or None
self.opts.os_user_domain_name = user_domain_name or None
self.opts.os_user_domain_id = user_domain_id or None
self.opts.os_project_domain_name = project_domain_name or None
self.opts.os_project_domain_id = project_domain_id or None
self.opts.os_cacert = cacert or None
self.opts.insecure = insecure
self.opts.cert = cert
else:
self.opts = opts
self.cert = cert
self.cacert = cacert or self.opts.os_cacert
self._session = session
verify = self.opts.os_cacert
if self.opts.insecure:
verify = False
self.validate()
self.project_id = project_id or self.get_project_id
self.jobs = jobs.JobManager(self, verify=verify)
self.clients = clients.ClientManager(self, verify=verify)
self.backups = backups.BackupsManager(self, verify=verify)
self.sessions = sessions.SessionManager(self, verify=verify)
self.actions = actions.ActionManager(self, verify=verify)
@utils.CachedProperty
def session(self):
if self._session:
return self._session
auth_type = 'password'
auth_kwargs = {
'auth_url': self.opts.os_auth_url,
'project_id': self.opts.os_project_id,
'project_name': self.opts.os_project_name,
'project_domain_id': self.opts.os_project_domain_id,
'project_domain_name': self.opts.os_project_domain_name,
}
if self.opts.os_username and self.opts.os_password:
auth_kwargs.update({
'username': self.opts.os_username,
'password': self.opts.os_password,
'user_domain_id': self.opts.os_user_domain_id,
'user_domain_name': self.opts.os_user_domain_name,
})
elif self.opts.os_token:
auth_type = 'token'
auth_kwargs.update({
'token': self.opts.os_token,
})
loader = kaloading.get_plugin_loader(auth_type)
auth_plugin = loader.load_from_options(**auth_kwargs)
# Let keystoneauth do the necessary parameter conversions
session = kaloading.session.Session().load_from_options(
auth=auth_plugin, insecure=self.opts.insecure, cacert=self.cacert,
cert=self.cert)
return session
@utils.CachedProperty
def endpoint(self):
if self.opts.os_backup_url:
return self.opts.os_backup_url
else:
auth_ref = self.session.auth.get_auth_ref(self.session)
endpoint = auth_ref.service_catalog.url_for(
service_type=FREEZER_SERVICE_TYPE,
interface=self.opts.os_endpoint_type,
)
return endpoint
@property
def auth_token(self):
return self.session.get_token()
@property
def get_project_id(self):
return self.session.get_project_id()
@utils.CachedProperty
def client_id(self):
return '{0}_{1}'.format(self.session.get_project_id(),
socket.gethostname())
def validate(self):
"""Validate that the client objects gets created correctly.
:return: bool
"""
if not self._session and self.opts.os_auth_url is None:
raise Exception('OS_AUTH_URL should be provided.')
|