A parsed instance of an API resource. It exists solely to ease interaction with API objects by allowing attributes to be accessed with '.' notation.
| 279 | |
| 280 | |
| 281 | class ResourceInstance: |
| 282 | """ A parsed instance of an API resource. It exists solely to |
| 283 | ease interaction with API objects by allowing attributes to |
| 284 | be accessed with '.' notation. |
| 285 | """ |
| 286 | |
| 287 | def __init__(self, client, instance): |
| 288 | self.client = client |
| 289 | # If we have a list of resources, then set the apiVersion and kind of |
| 290 | # each resource in 'items' |
| 291 | kind = instance['kind'] |
| 292 | if kind.endswith('List') and 'items' in instance: |
| 293 | kind = instance['kind'][:-4] |
| 294 | if not instance['items']: |
| 295 | instance['items'] = [] |
| 296 | for item in instance['items']: |
| 297 | if 'apiVersion' not in item: |
| 298 | item['apiVersion'] = instance['apiVersion'] |
| 299 | if 'kind' not in item: |
| 300 | item['kind'] = kind |
| 301 | |
| 302 | self.attributes = self.__deserialize(instance) |
| 303 | self.__initialised = True |
| 304 | |
| 305 | def __deserialize(self, field): |
| 306 | if isinstance(field, dict): |
| 307 | return ResourceField(params={ |
| 308 | k: self.__deserialize(v) for k, v in field.items() |
| 309 | }) |
| 310 | elif isinstance(field, (list, tuple)): |
| 311 | return [self.__deserialize(item) for item in field] |
| 312 | else: |
| 313 | return field |
| 314 | |
| 315 | def __serialize(self, field): |
| 316 | if isinstance(field, ResourceField): |
| 317 | return { |
| 318 | k: self.__serialize(v) for k, v in field.__dict__.items() |
| 319 | } |
| 320 | elif isinstance(field, (list, tuple)): |
| 321 | return [self.__serialize(item) for item in field] |
| 322 | elif isinstance(field, ResourceInstance): |
| 323 | return field.to_dict() |
| 324 | else: |
| 325 | return field |
| 326 | |
| 327 | def to_dict(self): |
| 328 | return self.__serialize(self.attributes) |
| 329 | |
| 330 | def to_str(self): |
| 331 | return repr(self) |
| 332 | |
| 333 | def __repr__(self): |
| 334 | return "ResourceInstance[{}]:\n {}".format( |
| 335 | self.attributes.kind, |
| 336 | ' '.join(yaml.safe_dump(self.to_dict()).splitlines(True)) |
| 337 | ) |
| 338 |
no outgoing calls