Introduce client manager base class

Each manager class is initialized from the object,
which means that some logic, like endpoint defenition, is
duplicated over all managers.

In order to solve that, and also efficiently refactor endpoint
defenition for being a property, base manager class is being
introduced.

Regarding the endpoint defenition, right now it is constructed
statically on client init.
While this does work, it may result in an unexpected behavior, when
client is being re-used.

To be more error-prone, it's construction moved to a
property.

Assisted-By: Gemini 3.1
Change-Id: I3e163accffb34ab3ef36b7eec0c309e1af03a67a
Signed-off-by: Dmitriy Rabotyagov <dmitriy.rabotyagov@cleura.com>
This commit is contained in:
Dmitriy Rabotyagov
2026-05-12 20:09:01 +02:00
parent 4179b1b28f
commit 15be334c4b
7 changed files with 127 additions and 60 deletions
+62
View File
@@ -0,0 +1,62 @@
# (c) Copyright 2026 Cleura AB
#
# 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 unittest
from unittest import mock
from freezerclient.v2.managers import base
class FakeManager(base.BaseManager):
resource_name = 'fake_resource'
class TestBaseManager(unittest.TestCase):
def setUp(self):
self.mock_client = mock.MagicMock()
self.mock_client.endpoint = 'http://freezer.api:8989'
self.mock_client.project_id = 'test_project'
self.mock_client.auth_token = 'test_token'
self.manager = FakeManager(self.mock_client, verify=True)
def test_endpoint_construction_without_v2(self):
# Should append /v2/{project_id}/{resource}/
self.assertEqual(
'http://freezer.api:8989/v2/test_project/fake_resource/',
self.manager.endpoint
)
def test_endpoint_construction_with_v2(self):
# Should append /{resource}/ and avoid double v2
self.mock_client.endpoint = 'http://freezer.api:8989/v2'
self.assertEqual(
'http://freezer.api:8989/v2/fake_resource/',
self.manager.endpoint
)
def test_headers(self):
expected = {
'X-Auth-Token': 'test_token',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
self.assertEqual(expected, self.manager.headers)
def test_methods_raise_not_implemented(self):
self.assertRaises(NotImplementedError, self.manager.create)
self.assertRaises(NotImplementedError, self.manager.delete)
self.assertRaises(NotImplementedError, self.manager.list)
self.assertRaises(NotImplementedError, self.manager.get)
self.assertRaises(NotImplementedError, self.manager.update)
+3 -12
View File
@@ -17,20 +17,11 @@ import requests
from oslo_serialization import jsonutils as json
from freezerclient import exceptions
from freezerclient import utils
from freezerclient.v2.managers import base
class ActionManager(object):
def __init__(self, client, verify=True):
self.client = client
self.endpoint = '{0}/v2/{1}/actions/'.format(
self.client.endpoint, self.client.project_id)
self.verify = verify
@property
def headers(self):
return utils.create_headers_for_request(self.client.auth_token)
class ActionManager(base.BaseManager):
resource_name = 'actions'
def create(self, doc, action_id=''):
action_id = action_id or doc.get('action_id', '')
+3 -12
View File
@@ -17,20 +17,11 @@ import requests
from oslo_serialization import jsonutils as json
from freezerclient import exceptions
from freezerclient import utils
from freezerclient.v2.managers import base
class BackupsManager(object):
def __init__(self, client, verify=True):
self.client = client
self.endpoint = '{0}/v2/{1}/backups/'.format(
self.client.endpoint, self.client.project_id)
self.verify = verify
@property
def headers(self):
return utils.create_headers_for_request(self.client.auth_token)
class BackupsManager(base.BaseManager):
resource_name = 'backups'
def create(self, backup_metadata):
r = requests.post(self.endpoint,
+50
View File
@@ -0,0 +1,50 @@
# (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.
from freezerclient import utils
class BaseManager(object):
resource_name = None
def __init__(self, client, verify=True):
self.client = client
self.verify = verify
@property
def endpoint(self):
endpoint = self.client.endpoint.rstrip('/')
if '/v2' in endpoint:
return '{0}/{1}/'.format(endpoint, self.resource_name)
return '{0}/v2/{1}/{2}/'.format(
endpoint, self.client.project_id, self.resource_name)
@property
def headers(self):
return utils.create_headers_for_request(self.client.auth_token)
def create(self, *args, **kwargs):
raise NotImplementedError
def delete(self, *args, **kwargs):
raise NotImplementedError
def list(self, *args, **kwargs):
raise NotImplementedError
def get(self, *args, **kwargs):
raise NotImplementedError
def update(self, *args, **kwargs):
raise NotImplementedError
+3 -12
View File
@@ -17,20 +17,11 @@ import requests
from oslo_serialization import jsonutils as json
from freezerclient import exceptions
from freezerclient import utils
from freezerclient.v2.managers import base
class ClientManager(object):
def __init__(self, client, verify=True):
self.client = client
self.endpoint = '{0}/v2/{1}/clients/'.format(
self.client.endpoint, self.client.project_id)
self.verify = verify
@property
def headers(self):
return utils.create_headers_for_request(self.client.auth_token)
class ClientManager(base.BaseManager):
resource_name = 'clients'
def create(self, client_info):
r = requests.post(self.endpoint,
+3 -12
View File
@@ -17,20 +17,11 @@ import requests
from oslo_serialization import jsonutils as json
from freezerclient import exceptions
from freezerclient import utils
from freezerclient.v2.managers import base
class JobManager(object):
def __init__(self, client, verify=True):
self.client = client
self.endpoint = '{0}/v2/{1}/jobs/'.format(
self.client.endpoint, self.client.project_id)
self.verify = verify
@property
def headers(self):
return utils.create_headers_for_request(self.client.auth_token)
class JobManager(base.BaseManager):
resource_name = 'jobs'
def create(self, doc, job_id=''):
job_id = job_id or doc.get('job_id', '')
+3 -12
View File
@@ -17,20 +17,11 @@ import requests
from oslo_serialization import jsonutils as json
from freezerclient import exceptions
from freezerclient import utils
from freezerclient.v2.managers import base
class SessionManager(object):
def __init__(self, client, verify=True):
self.client = client
self.endpoint = '{0}/v2/{1}/sessions/'.format(
self.client.endpoint, self.client.project_id)
self.verify = verify
@property
def headers(self):
return utils.create_headers_for_request(self.client.auth_token)
class SessionManager(base.BaseManager):
resource_name = 'sessions'
def create(self, doc, session_id=''):
session_id = session_id or doc.get('session_id', '')