cloudfoundry-community/cf-python-client

Get app from service_instance if it's bound?

alxxthegeek opened this issue · 2 comments

Hi your library has been very useful!

Needing to reverse lookup app's , their space and org from the service instance (and volumes in aws)
so we can track who owns idle instances.
Can get the spaces and org's no problem.

From aws cli I can get the service instance from the instance or volume.
I can get the app_guid (if the service is bound) using

cf curl /v2/service_instances/service_instance_guid/service_bindings

Would much prefer to do this using cf-python.
Could be I've just missed something and it's already possible/blinding obvious and I can't see it ?

Hi,

thank you for the feedback .

In V2 api, entities are made navigable using the ***_url attribute (if it ends with a sthen it is a generator, otherwise a single entity) ie you can do something like

service_instance = client.v2.service_instances.get('service-instance-id')
for service_binding in service_instance.service_bindings():
  app =  service_binding.app()

Cf service binding attributes and service instance ones

In V3, objects are a little differently structure, yet the navigation has also been implemtented, this time deduced from link section, following the same rule:

  • if it ends with a s, we return a generator
  • a single entity otherwise

I am not very familiar with V3 representation of binding, maybe you will find reading documentation

Maybe the following code does the job

service_instance = client.v3.service_instances.get(('service-instance-id')
for service_credential_binding in service_instance.service_credential_bindings():
 if service_credential_binding["type"] == "app":
   app =  service_binding.app()

See service instance and service credentials binding

Great thank you!