-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from pehala/modify_decorator
Add modify decorator
- Loading branch information
Showing
4 changed files
with
55 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters