Skip to content

Commit e0e5fdc

Browse files
Merge pull request #83 from bradmwilliams/custom_apiobjects
Adding the ability to return custom APIObject subclasses from a selector
2 parents 87a22f2 + 703aec6 commit e0e5fdc

File tree

5 files changed

+45
-8
lines changed

5 files changed

+45
-8
lines changed

ansible/rebuild_module.digest

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6c22c2bda1140a30873e660f35179305 -
1+
10685da388ff02fca420c9d1e10275fa -

ansible/roles/openshift_client_python/library/openshift_client_python.py

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/custom_apiobjects.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python
2+
3+
import openshift as oc
4+
from openshift import APIObject
5+
6+
7+
class MyCustomPodClass(APIObject):
8+
def __init__(self, *args, **kwargs):
9+
super().__init__(*args, **kwargs)
10+
11+
def super_cool_awesomeness(self):
12+
print('Calling: super_cool_awesomeness() on pod: {}/{}'.format(self.model.metadata.namespace, self.model.metadata.name))
13+
14+
15+
if __name__ == '__main__':
16+
with oc.client_host():
17+
with oc.project('openshift-monitoring'):
18+
19+
objs = oc.selector('pods', labels={'app': 'prometheus'}).objects(cls=MyCustomPodClass)
20+
21+
for obj in objs:
22+
print(type(obj))
23+
obj.super_cool_awesomeness()

packages/openshift/apiobject.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -666,8 +666,9 @@ def patch(self, patch_dict, strategy="strategic", cmd_args=None):
666666
r.fail_if("Error running patch on objects")
667667
return r
668668

669-
def elements(self):
669+
def elements(self, cls=None):
670670
"""
671+
:param cls A custom subclass of APIObject to return in place of APIObjects
671672
:return: Returns a python list of APIObjects. If receiver is an OpenShift 'List', each element will be
672673
added to the returned list. If the receiver is not of kind List, the [self] will be returned.
673674
"""
@@ -688,7 +689,12 @@ def elements(self):
688689
if item_kind:
689690
d['kind'] = item_kind
690691

691-
l.append(APIObject(d))
692+
if cls is not None:
693+
obj = cls(d)
694+
else:
695+
obj = APIObject(d)
696+
697+
l.append(obj)
692698

693699
return l
694700

packages/openshift/selector.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,15 +391,16 @@ def object_json(self, ignore_not_found=False):
391391

392392
return r.out()
393393

394-
def object(self, ignore_not_found=False):
394+
def object(self, ignore_not_found=False, cls=None):
395395
"""
396396
Returns a single APIObject that represents the selected resource. If multiple
397397
resources are being selected an exception will be thrown (use objects() when
398398
there is a possibility of selecting multiple objects).
399399
:param ignore_not_found: If True and no object exists, None will be returned instead of an exception.
400+
:param cls: Custom APIObject class to return
400401
:return: A Model of the selected resource.
401402
"""
402-
objs = self.objects()
403+
objs = self.objects(cls=cls)
403404
if len(objs) == 0:
404405
if ignore_not_found:
405406
return None
@@ -409,17 +410,24 @@ def object(self, ignore_not_found=False):
409410

410411
return objs[0]
411412

412-
def objects(self, ignore_not_found=True):
413+
def objects(self, ignore_not_found=True, cls=None):
413414
"""
414415
Returns a python list of APIObject objects that represent the selected resources. An
415416
empty is returned if nothing is selected.
416417
:param ignore_not_found: If true, missing named resources will not raise an exception.
418+
:param cls: Custom APIObject class to return
417419
:return: A list of Model objects representing the receiver's selected resources.
418420
"""
419421
from .apiobject import APIObject
420422

421423
obj = json.loads(self.object_json(ignore_not_found=ignore_not_found))
422-
return APIObject(obj).elements()
424+
425+
if cls is not None:
426+
api_objects = cls(obj).elements(cls)
427+
else:
428+
api_objects = APIObject(obj).elements()
429+
430+
return api_objects
423431

424432
def start_build(self, cmd_args=None):
425433
r = Selector('start_build')

0 commit comments

Comments
 (0)