2015-03-24 17:15:20 +00:00
|
|
|
"""
|
|
|
|
Copyright 2014 Hewlett-Packard
|
|
|
|
|
|
|
|
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).
|
|
|
|
========================================================================
|
|
|
|
"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2015-04-16 12:00:36 +01:00
|
|
|
from openstackclient.identity import client as os_client
|
|
|
|
|
2015-03-24 17:15:20 +00:00
|
|
|
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
|
|
|
|
os.pardir, os.pardir, os.pardir))
|
|
|
|
if os.path.exists(os.path.join(possible_topdir, 'freezer', '__init__.py')):
|
|
|
|
sys.path.insert(0, possible_topdir)
|
|
|
|
|
2015-04-14 17:57:38 +01:00
|
|
|
from freezer.apiclient.backups import BackupsManager
|
2015-04-27 17:23:13 +01:00
|
|
|
from freezer.apiclient.registration import RegistrationManager
|
2015-05-01 12:36:19 +01:00
|
|
|
from freezer.apiclient.actions import ActionManager
|
2015-04-16 12:00:36 +01:00
|
|
|
import exceptions
|
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
|
|
|
|
self._auth = None
|
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-05-01 12:36:19 +01:00
|
|
|
self.actions = ActionManager(self)
|
2015-04-16 12:00:36 +01:00
|
|
|
|
|
|
|
def _update_api_endpoint(self):
|
|
|
|
services = self.auth.services.list()
|
|
|
|
try:
|
|
|
|
freezer_service = next(x for x in services if x.name == 'freezer')
|
|
|
|
except:
|
|
|
|
raise exceptions.AuthFailure(
|
|
|
|
'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:
|
|
|
|
raise exceptions.AuthFailure(
|
|
|
|
'freezer endpoint not found in endpoint list')
|
2015-04-27 17:23:13 +01:00
|
|
|
self._endpoint = freezer_endpoint.publicurl
|
2015-04-16 12:00:36 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def auth(self):
|
|
|
|
if self._auth is None:
|
|
|
|
if self.username and self.password:
|
|
|
|
self._auth = os_client.IdentityClientv2(
|
|
|
|
auth_url=self.auth_url,
|
|
|
|
username=self.username,
|
|
|
|
password=self.password,
|
|
|
|
tenant_name=self.tenant_name)
|
|
|
|
elif self.token:
|
|
|
|
self._auth = os_client.IdentityClientv2(
|
|
|
|
endpoint=self.auth_url,
|
|
|
|
token=self.token)
|
|
|
|
else:
|
|
|
|
raise exceptions.AuthFailure("Missing auth credentials")
|
|
|
|
return self._auth
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auth_token(self):
|
|
|
|
return self.auth.auth_token
|
|
|
|
|
|
|
|
@property
|
2015-04-27 17:23:13 +01:00
|
|
|
def endpoint(self):
|
|
|
|
if self._endpoint is None:
|
2015-04-16 12:00:36 +01:00
|
|
|
self._update_api_endpoint()
|
2015-04-27 17:23:13 +01:00
|
|
|
return self._endpoint
|
2015-04-16 12:00:36 +01:00
|
|
|
|
|
|
|
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
|