cloudfoundry-community/cf-python-client

KeyError when fetching service_instances credentials

krezreb opened this issue · 2 comments

Example code is using the v3 API to fetch the credentials of a service_instance

for si in client.v3.service_instances.list():
    print(dict(si.credentials()))

results in a keyError in entities.py line 160

KeyError                                  Traceback (most recent call last)
/tmp/ipykernel_885316/3232475555.py in <module>
      1 for sa in cfClient.v3.service_instances.list(names="eudevpg12-db"):
      2     creds = sa.credentials()
----> 3     for c in creds:
      4         print(c)
      5 

~/repos/renault/kamereon/az-postgresql-migrate/list/venv/lib/python3.9/site-packages/cloudfoundry_client/v3/entities.py in _paginate(self, url_requested, entity_type)
    158         while True:
    159             response_json = self._read_response(response, JsonObject)
--> 160             for resource in response_json["resources"]:
    161                 yield self._entity(resource, entity_type)
    162             pagination = response_json.get("pagination")

KeyError: 'resources'

It appears that the EntityManager class always assumes pagination, which is not the case here.

Proposed patch is to check if "resources" in response_json: and only paginate if True otherwise return the object directly

Closed by #150