Skip to content

Commit

Permalink
Merge pull request #32 from arenadata/adcm-1359
Browse files Browse the repository at this point in the history
ADCM-1359 Added decorator for compare version of server and client.
  • Loading branch information
acmnu authored Jul 14, 2020
2 parents a13c9a7 + 6274771 commit bc31b8c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
34 changes: 31 additions & 3 deletions adcm_client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=R0901
from adcm_client.util.search import search_one, search
from adcm_client.wrappers.api import ADCMApiWrapper
from collections import UserList, OrderedDict
from functools import wraps
from pprint import pprint
from time import sleep

import coreapi
from version_utils import rpm

from adcm_client.util.search import search_one, search
from adcm_client.wrappers.api import ADCMApiWrapper


def pp(*args, **kwargs):
Expand Down Expand Up @@ -69,6 +73,30 @@ class PagingEnds(Exception):
"""There are no more data in paginated mode."""


class TooOldServerVersion(Exception):
"""Incompatible version, upgrade version ADCM."""
def __init__(self, method_name='', version='', message=None):
if message is None:
self.message = (f'The "{method_name}" method works with versions older {version},'
f' upgrade version ADCM.')
else:
self.message = message
super(TooOldServerVersion, self).__init__(self.message)


def min_server_version(version):
def decorate(func):
@wraps(func)
def wrapper(*args, **kwargs):
# The ADCM version must be greater than or equal to the method version
# args[0].adcm_version >= version
if rpm.compare_versions(args[0].adcm_version, version) < 0:
raise TooOldServerVersion(func.__name__, version)
return func(*args, **kwargs)
return wrapper
return decorate


class Paging:
def __init__(self, paged_object, limit=50, **args):
self._paged_object = paged_object
Expand Down Expand Up @@ -245,7 +273,7 @@ def __init__(self, api: ADCMApiWrapper, path=None, path_args=None, **args):

self._endpoint = EndPoint(api, self.IDNAME, path, path_args, self.FILTERS)
self._api = api

self.adcm_version = self._api.adcm_version
self._client = api.objects

self._data = self._endpoint.search_one(**args)
Expand Down
16 changes: 7 additions & 9 deletions adcm_client/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from adcm_client.base import (
ActionHasIssues, ADCMApiError, BaseAPIListObject, BaseAPIObject, ObjectNotFound,
TooManyArguments, strip_none_keys
TooManyArguments, strip_none_keys, min_server_version
)
from adcm_client.util import stream
from adcm_client.wrappers.api import ADCMApiWrapper
Expand Down Expand Up @@ -431,9 +431,10 @@ def service_add(self, **args) -> "Service":
data = self._subcall("service", "create", prototype_id=proto.id)
return self._subobject(Service, service_id=data['id'])

# @allure_step("Remove service to cluster")
# def service_delete(self, service):
# self._subcall("service", "delete", service_id=service.id)
@min_server_version('2020.05.13.00')
def service_delete(self, service: "Service"):
with allure_step("Remove service {} from cluster {}".format(service.name, self.name)):
self._subcall("service", "delete", service_id=service.id)

def hostcomponent(self):
return self._subcall("hostcomponent", "list")
Expand Down Expand Up @@ -889,6 +890,7 @@ def __init__(self, api=None, url=None, user=None, password=None):
self.auth(user, password)
if self.api_token() is not None:
self.guess_adcm_url()
self.adcm_version = self._api.adcm_version

def auth(self, user=None, password=None):
if user is None or password is None:
Expand All @@ -899,11 +901,7 @@ def auth(self, user=None, password=None):
self._check_min_version()

def _check_min_version(self):
try:
version = self._api.objects.info.list()['adcm_version']
except (KeyError, AttributeError):
version = "0"
if rpm.compare_versions(self._MIN_VERSION, version) > 0:
if rpm.compare_versions(self._MIN_VERSION, self._api.adcm_version) > 0:
raise ADCMApiError("That client supports ADCM versions >= {}".format(self._MIN_VERSION))

def api_token(self):
Expand Down
5 changes: 5 additions & 0 deletions adcm_client/wrappers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def __init__(self, url):
self.schema = None
self.objects = None
self.api_token = None
self.adcm_version = None

def _check_for_error(self, data):
if data is not None:
Expand All @@ -124,6 +125,10 @@ def auth(self, username, password):
self.schema = self.client.get("{}{}schema/".format(self.url, self.api_url))
self.objects = self._parse_schema(self.schema, is_allure=IS_ALLURE)
self.api_token = token['token']
try:
self.adcm_version = self.objects.info.list()['adcm_version']
except (KeyError, AttributeError):
self.adcm_version = "0"

def action(self, *args, **kvargs):
"""
Expand Down

0 comments on commit bc31b8c

Please sign in to comment.