Add freezerclient.client in order to use the python api directly

The simplest way to create a client instance is initialization with your
 credentials:

        >>> from freezerclient import client
        >>> freezer = client.Client("2")
        >>> freezer.jobs.list_all()

Change-Id: I04f1316d6786579a48d1db7969c7cae3ef8bd9e4
This commit is contained in:
Cai Hui
2018-10-12 01:54:58 -04:00
parent 97d6315279
commit 90542f8a2c
2 changed files with 155 additions and 0 deletions

73
freezerclient/client.py Normal file
View File

@@ -0,0 +1,73 @@
# (c) Copyright 2018 ZTE Corporation.
#
# 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.
from freezerclient import utils
import os
def Client(version=None, endpoint=None, username=None, password=None,
project_name=None, auth_url=None, **kwargs):
"""Initialize client object based on given version.
HOW-TO:
The simplest way to create a client instance is initialization with your
credentials::
>>> from freezerclient import client
>>> freezer = client.Client(VERSION, ENDPOINT, USERNAME, PASSWORD,
... PROJECT_NAME, AUTH_URL)
Here ``VERSION`` is freezer API Version, you can use ``1`` or ``2``,
default is API v2.
Alternatively, you can create a client instance using the keystoneauth
session API. See "The freezerclient Python API" page at
python-freezerclient's doc.
"""
if endpoint:
kwargs["endpoint"] = endpoint
else:
kwargs["endpoint"] = os.environ.get('OS_BACKUP_URL')
if username:
kwargs["username"] = username
else:
kwargs["username"] = os.environ.get('OS_USERNAME')
if password:
kwargs["password"] = password
else:
kwargs["password"] = os.environ.get('OS_PASSWORD')
if project_name:
kwargs["project_name"] = project_name
else:
kwargs["project_name"] = os.environ.get('OS_PROJECT_NAME')
if auth_url:
kwargs["auth_url"] = auth_url
else:
kwargs["auth_url"] = os.environ.get('OS_AUTH_URL')
kwargs["token"] = os.environ.get('OS_TOKEN')
kwargs["tenant_name"] = os.environ.get('OS_PROJECT_NAME')
kwargs["endpoint_type"] = os.environ.get('OS_ENDPOINT_TYPE') or 'public'
kwargs["project_domain_id"] = os.environ.get('OS_PROJECT_DOMAIN_ID')
kwargs["user_domain_id"] = os.environ.get('OS_USER_DOMAIN_ID')
if not version:
version = os.environ.get('OS_BACKUP_API_VERSION', '2')
client_class = utils.get_client_class(version)
return client_class(**kwargs)

View File

@@ -0,0 +1,82 @@
# (c) Copyright 2018 ZTE Corporation.
#
# 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 mock
import unittest
from freezerclient import client
from freezerclient import v1
from freezerclient import v2
class ClientTest(unittest.TestCase):
def setUp(self):
self.session = mock.Mock()
def test_client_v1_endpoint(self):
gc = client.Client(version='1', endpoint='http://freezer.org',
session=self.session)
self.assertEqual("http://freezer.org", gc.opts.os_backup_url)
self.assertIsInstance(gc, v1.client.Client)
def test_client_v1_auth_url(self):
gc = client.Client(version='1', auth_url='http://example.com',
endpoint='http://freezer.org',
session=self.session)
self.assertEqual("http://freezer.org", gc.opts.os_backup_url)
self.assertIsInstance(gc, v1.client.Client)
def test_client_v1_username(self):
gc = client.Client('1', endpoint='http://freezer.org',
username='caihui', session=self.session)
self.assertEqual("caihui", gc.opts.os_username)
self.assertIsInstance(gc, v1.client.Client)
def test_client_v1_password(self):
gc = client.Client('1', password='password',
endpoint='http://freezer.org',
session=self.session)
self.assertEqual("password", gc.opts.os_password)
self.assertIsInstance(gc, v1.client.Client)
def test_client_v2_auth_url(self):
gc = client.Client(version='2', auth_url='http://example.com',
session=self.session)
self.assertEqual("http://example.com", gc.opts.os_auth_url)
self.assertIsInstance(gc, v2.client.Client)
def test_client_v2_endpoint(self):
gc = client.Client(version='2', endpoint='http://freezer.org',
session=self.session)
self.assertEqual("http://freezer.org", gc.opts.os_backup_url)
self.assertIsInstance(gc, v2.client.Client)
def test_client_v2_username(self):
gc = client.Client('2', username='caihui', session=self.session)
self.assertEqual("caihui", gc.opts.os_username)
self.assertIsInstance(gc, v2.client.Client)
def test_client_v2_password(self):
gc = client.Client('2', password='password', session=self.session)
self.assertEqual("password", gc.opts.os_password)
self.assertIsInstance(gc, v2.client.Client)
def test_client_v2_porject_name(self):
gc = client.Client('2', project_name='tecs', session=self.session)
self.assertEqual("tecs", gc.opts.os_project_name)
self.assertIsInstance(gc, v2.client.Client)
def test_client_default_version(self):
gc = client.Client(session=self.session)
self.assertIsInstance(gc, v2.client.Client)