Skip to content

Commit

Permalink
Merge pull request #38 from pehala/modify_decorator
Browse files Browse the repository at this point in the history
Add modify decorator
  • Loading branch information
pehala authored Aug 8, 2022
2 parents e2aa342 + 24ec267 commit 856fc41
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 32 deletions.
43 changes: 43 additions & 0 deletions testsuite/openshift/objects/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Base classes/methods for our own APIObjects"""
import functools

from openshift import APIObject


def modify(func):
"""Wraps method of a subclass of OpenShiftObject to use modify_and_apply when the object
is already committed to the server, or run it normally if it isn't.
All methods modifying the target object in any way should be decorated by this"""
def _custom_partial(func, *args, **kwargs):
"""Custom partial function which makes sure that self is always assigned correctly"""

def _func(self):
func(self, *args, **kwargs)

return _func

@functools.wraps(func)
def _wrap(self, *args, **kwargs):
if self.commited:
result, _ = self.modify_and_apply(_custom_partial(func, *args, **kwargs))
assert result.status
else:
func(self, *args, **kwargs)

return _wrap


class OpenShiftObject(APIObject):
"""Custom APIObjects which tracks if the object was already committed to the server or not"""
def __init__(self, dict_to_model=None, string_to_model=None, context=None):
super().__init__(dict_to_model, string_to_model, context)
self.commited = False

def commit(self):
"""
Creates object on the server and returns created entity.
It will be the same class but attributes might differ, due to server adding/rejecting some of them.
"""
self.create(["--save-config=true"])
self.commited = True
return self.refresh()
13 changes: 2 additions & 11 deletions testsuite/openshift/objects/api_key.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
"""API Key Secret CR object"""

from openshift import APIObject

from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject


class APIKey(APIObject):
class APIKey(OpenShiftObject):
"""Represents API Key Secret CR for Authorino"""

@classmethod
Expand All @@ -29,11 +28,3 @@ def create_instance(cls, openshift: OpenShiftClient, name, label, api_key_string
}

return cls(model, context=openshift.context)

def commit(self):
"""
Creates object on the server and returns created entity.
It will be the same class but attributes might differ, due to server adding/rejecting some of them.
"""
self.create(["--save-config=true"])
return self.refresh()
18 changes: 7 additions & 11 deletions testsuite/openshift/objects/auth_config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
"""AuthConfig CR object"""
from typing import Dict

from openshift import APIObject

from testsuite.objects import Authorization
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject, modify


class AuthConfig(APIObject, Authorization):
class AuthConfig(OpenShiftObject, Authorization):
"""Represents AuthConfig CR from Authorino"""

@classmethod
Expand All @@ -30,23 +29,19 @@ def create_instance(cls, openshift: OpenShiftClient, name, host, labels: Dict[st

return cls(model, context=openshift.context)

def commit(self):
"""
Creates object on the server and returns created entity.
It will be the same class but attributes might differ, due to server adding/rejecting some of them.
"""
self.create(["--save-config=true"])
return self.refresh()

@modify
def add_host(self, hostname):
self.model.spec.hosts.append(hostname)

@modify
def remove_host(self, hostname):
self.model.spec.hosts.remove(hostname)

@modify
def remove_all_hosts(self):
self.model.spec.hosts = []

@modify
def add_oidc_identity(self, name, endpoint):
"""Adds OIDC identity"""
identities = self.model.spec.setdefault("identity", [])
Expand All @@ -57,6 +52,7 @@ def add_oidc_identity(self, name, endpoint):
}
})

@modify
def add_api_key_identity(self, name, label):
"""Adds API Key identity"""
identities = self.model.spec.setdefault("identity", [])
Expand Down
13 changes: 3 additions & 10 deletions testsuite/openshift/objects/authorino.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
from typing import Any, Dict, List

import openshift
from openshift import APIObject, selector
from openshift import selector

from testsuite.objects import Authorino
from testsuite.openshift.client import OpenShiftClient
from testsuite.openshift.objects import OpenShiftObject


class AuthorinoCR(APIObject, Authorino):
class AuthorinoCR(OpenShiftObject, Authorino):
"""Represents Authorino CR objects from Authorino-operator"""

@classmethod
Expand Down Expand Up @@ -61,14 +62,6 @@ def wait_for_ready(self):
assert success, "Authorino did got get ready in time"
self.refresh()

def commit(self):
"""
Creates object on the server and returns created entity.
It will be the same class but attributes might differ, due to server adding/rejecting some of them.
"""
self.create(["--save-config=true"])
return self.refresh()

@property
def deployment(self):
"""Returns Deployment object for this Authorino"""
Expand Down

0 comments on commit 856fc41

Please sign in to comment.