MCPcopy Index your code
hub / github.com/kubernetes-client/python / ResourceList

Class ResourceList

kubernetes/base/dynamic/resource.py:108–229  ·  view source on GitHub ↗

Represents a list of API objects

Source from the content-addressed store, hash-verified

106
107
108class ResourceList(Resource):
109 """ Represents a list of API objects """
110
111 def __init__(self, client, group='', api_version='v1', base_kind='', kind=None, base_resource_lookup=None):
112 self.client = client
113 self.group = group
114 self.api_version = api_version
115 self.kind = kind or '{}List'.format(base_kind)
116 self.base_kind = base_kind
117 self.base_resource_lookup = base_resource_lookup
118 self.__base_resource = None
119
120 def base_resource(self):
121 if self.__base_resource:
122 return self.__base_resource
123 elif self.base_resource_lookup:
124 self.__base_resource = self.client.resources.get(**self.base_resource_lookup)
125 return self.__base_resource
126 elif self.base_kind:
127 self.__base_resource = self.client.resources.get(group=self.group, api_version=self.api_version, kind=self.base_kind)
128 return self.__base_resource
129 return None
130
131 def _items_to_resources(self, body):
132 """ Takes a List body and return a dictionary with the following structure:
133 {
134 'api_version': str,
135 'kind': str,
136 'items': [{
137 'resource': Resource,
138 'name': str,
139 'namespace': str,
140 }]
141 }
142 """
143 if body is None:
144 raise ValueError("You must provide a body when calling methods on a ResourceList")
145
146 api_version = body['apiVersion']
147 kind = body['kind']
148 items = body.get('items')
149 if not items:
150 raise ValueError('The `items` field in the body must be populated when calling methods on a ResourceList')
151
152 if self.kind != kind:
153 raise ValueError('Methods on a {} must be called with a body containing the same kind. Received {} instead'.format(self.kind, kind))
154
155 return {
156 'api_version': api_version,
157 'kind': kind,
158 'items': [self._item_to_resource(item) for item in items]
159 }
160
161 def _item_to_resource(self, item):
162 metadata = item.get('metadata', {})
163 resource = self.base_resource()
164 if not resource:
165 api_version = item.get('apiVersion', self.api_version)

Callers 3

default_groupsMethod · 0.85
object_hookMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected