Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ansible/rebuild_module.digest
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6c22c2bda1140a30873e660f35179305 -
10685da388ff02fca420c9d1e10275fa -

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions examples/custom_apiobjects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python

import openshift as oc
from openshift import APIObject


class MyCustomPodClass(APIObject):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def super_cool_awesomeness(self):
print('Calling: super_cool_awesomeness() on pod: {}/{}'.format(self.model.metadata.namespace, self.model.metadata.name))


if __name__ == '__main__':
with oc.client_host():
with oc.project('openshift-monitoring'):

objs = oc.selector('pods', labels={'app': 'prometheus'}).objects(cls=MyCustomPodClass)

for obj in objs:
print(type(obj))
obj.super_cool_awesomeness()
10 changes: 8 additions & 2 deletions packages/openshift/apiobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,9 @@ def patch(self, patch_dict, strategy="strategic", cmd_args=None):
r.fail_if("Error running patch on objects")
return r

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

l.append(APIObject(d))
if cls is not None:
obj = cls(d)
else:
obj = APIObject(d)

l.append(obj)

return l

Expand Down
16 changes: 12 additions & 4 deletions packages/openshift/selector.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,16 @@ def object_json(self, ignore_not_found=False):

return r.out()

def object(self, ignore_not_found=False):
def object(self, ignore_not_found=False, cls=None):
"""
Returns a single APIObject that represents the selected resource. If multiple
resources are being selected an exception will be thrown (use objects() when
there is a possibility of selecting multiple objects).
:param ignore_not_found: If True and no object exists, None will be returned instead of an exception.
:param cls: Custom APIObject class to return
:return: A Model of the selected resource.
"""
objs = self.objects()
objs = self.objects(cls=cls)
if len(objs) == 0:
if ignore_not_found:
return None
Expand All @@ -409,17 +410,24 @@ def object(self, ignore_not_found=False):

return objs[0]

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

obj = json.loads(self.object_json(ignore_not_found=ignore_not_found))
return APIObject(obj).elements()

if cls is not None:
api_objects = cls(obj).elements(cls)
else:
api_objects = APIObject(obj).elements()

return api_objects

def start_build(self, cmd_args=None):
r = Selector('start_build')
Expand Down