2015-03-24 17:15:20 +00:00
|
|
|
"""
|
2015-06-08 12:05:08 +01:00
|
|
|
Copyright 2015 Hewlett-Packard
|
2015-03-24 17:15:20 +00: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.
|
|
|
|
|
|
|
|
This product includes cryptographic software written by Eric Young
|
|
|
|
(eay@cryptsoft.com). This product includes software written by Tim
|
|
|
|
Hudson (tjh@cryptsoft.com).
|
|
|
|
========================================================================
|
|
|
|
"""
|
|
|
|
|
2015-06-08 12:05:08 +01:00
|
|
|
import socket
|
2015-03-24 17:15:20 +00:00
|
|
|
|
2015-04-16 12:00:36 +01:00
|
|
|
from openstackclient.identity import client as os_client
|
|
|
|
|
2015-06-08 12:05:08 +01:00
|
|
|
from backups import BackupsManager
|
|
|
|
from registration import RegistrationManager
|
|
|
|
from jobs import JobManager
|
2015-06-08 12:05:08 +01:00
|
|
|
from actions import ActionManager
|
|
|
|
from sessions import SessionManager
|
2015-03-24 17:15:20 +00:00
|
|
|
|
2015-04-16 12:00:36 +01:00
|
|
|
import exceptions
|
2015-03-24 17:15:20 +00:00
|
|
|
|
|
|
|
|
2015-06-08 12:05:08 +01:00
|
|
|
class cached_property(object):
|
|
|
|
|
|
|
|
def __init__(self, func):
|
|
|
|
self.__doc__ = getattr(func, '__doc__')
|
|
|
|
self.func = func
|
|
|
|
|
|
|
|
def __get__(self, obj, cls):
|
|
|
|
if obj is None:
|
|
|
|
return self
|
|
|
|
value = obj.__dict__[self.func.__name__] = self.func(obj)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2015-03-24 17:15:20 +00:00
|
|
|
class Client(object):
|
|
|
|
|
|
|
|
def __init__(self, version='1',
|
|
|
|
token=None,
|
|
|
|
username=None,
|
|
|
|
password=None,
|
|
|
|
tenant_name=None,
|
|
|
|
auth_url=None,
|
2015-04-16 12:00:36 +01:00
|
|
|
session=None,
|
2015-04-27 17:23:13 +01:00
|
|
|
endpoint=None):
|
2015-04-16 12:00:36 +01:00
|
|
|
self.version = version
|
|
|
|
self.token = token
|
|
|
|
self.username = username
|
|
|
|
self.tenant_name = tenant_name
|
|
|
|
self.password = password
|
|
|
|
self.auth_url = auth_url
|
2015-04-27 17:23:13 +01:00
|
|
|
self._endpoint = endpoint
|
2015-04-16 12:00:36 +01:00
|
|
|
self.session = session
|
2015-03-24 17:15:20 +00:00
|
|
|
self.backups = BackupsManager(self)
|
2015-04-27 17:23:13 +01:00
|
|
|
self.registration = RegistrationManager(self)
|
2015-06-08 12:05:08 +01:00
|
|
|
self.jobs = JobManager(self)
|
2015-06-08 12:05:08 +01:00
|
|
|
self.actions = ActionManager(self)
|
|
|
|
self.sessions = SessionManager(self)
|
2015-04-16 12:00:36 +01:00
|
|
|
|
2015-06-08 12:05:08 +01:00
|
|
|
@cached_property
|
|
|
|
def endpoint(self):
|
|
|
|
if self._endpoint:
|
|
|
|
return self._endpoint
|
2015-04-16 12:00:36 +01:00
|
|
|
services = self.auth.services.list()
|
|
|
|
try:
|
|
|
|
freezer_service = next(x for x in services if x.name == 'freezer')
|
|
|
|
except:
|
2015-06-08 12:05:08 +01:00
|
|
|
raise exceptions.ApiClientException(
|
2015-04-16 12:00:36 +01:00
|
|
|
'freezer service not found in services list')
|
|
|
|
endpoints = self.auth.endpoints.list()
|
|
|
|
try:
|
|
|
|
freezer_endpoint =\
|
|
|
|
next(x for x in endpoints
|
|
|
|
if x.service_id == freezer_service.id)
|
|
|
|
except:
|
2015-06-08 12:05:08 +01:00
|
|
|
raise exceptions.ApiClientException(
|
2015-04-16 12:00:36 +01:00
|
|
|
'freezer endpoint not found in endpoint list')
|
2015-06-08 12:05:08 +01:00
|
|
|
return freezer_endpoint.publicurl
|
2015-04-16 12:00:36 +01:00
|
|
|
|
2015-06-08 12:05:08 +01:00
|
|
|
@cached_property
|
2015-04-16 12:00:36 +01:00
|
|
|
def auth(self):
|
2015-06-08 12:05:08 +01:00
|
|
|
if self.username and self.password:
|
|
|
|
_auth = os_client.IdentityClientv2(
|
|
|
|
auth_url=self.auth_url,
|
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
tenant_name=self.tenant_name)
|
|
|
|
elif self.token:
|
|
|
|
_auth = os_client.IdentityClientv2(
|
|
|
|
endpoint=self.auth_url,
|
|
|
|
token=self.token)
|
|
|
|
else:
|
|
|
|
raise exceptions.ApiClientException("Missing auth credentials")
|
|
|
|
return _auth
|
2015-04-16 12:00:36 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def auth_token(self):
|
|
|
|
return self.auth.auth_token
|
|
|
|
|
|
|
|
def api_exists(self):
|
|
|
|
try:
|
2015-04-27 17:23:13 +01:00
|
|
|
if self.endpoint is not None:
|
2015-04-16 12:00:36 +01:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
2015-06-08 12:05:08 +01:00
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def client_id(self):
|
|
|
|
return '{0}_{1}'.format(self.auth.project_id, socket.gethostname())
|