cloudfoundry-community / cf-python-client

Small cloudfoundry client implemented in python
Apache License 2.0
53 stars 50 forks source link

Manager App v2/v3 shows PCF title services #65

Open johnalejandro001 opened 4 years ago

johnalejandro001 commented 4 years ago

Hi I couldn't find documentation on how to retrieves services attached to an specific app.

  1. Another thing how do you set a specific org, space in the code?
  2. Is there limitation on the apps in context of pagination - what is the limit?
antechrestos commented 4 years ago

Which services are you referring to? Service bindings, instances or services?

About the pagination limit, I have not tested it with great number of page but I assume this should work.

About fixing the space and org, do you mean like the cf cli? If so there is not. This is a library meant to interract with the CF API. However, you can do things like (given you want to load all applications of a org/space )

for app in client.v2.apps.list(space_guid='space-id', organization_guid='org-id'):
    pass

Entities returned by client wrap all the urls returned as a method

Exemple of an application:

{
      "metadata": {
        "guid": "6064d98a-95e6-400b-bc03-be65e6d59622",
        "url": "/v2/apps/6064d98a-95e6-400b-bc03-be65e6d59622",
        "created_at": "2016-06-08T16:41:45Z",
        "updated_at": "2016-06-08T16:41:45Z"
      },
      "entity": {
        "name": "name-2443",
        /* ... */
        "space_url": "/v2/spaces/9c5c8a91-a728-4608-9f5e-6c8026c3a2ac",
        "stack_url": "/v2/stacks/f6c960cc-98ba-4fd1-b197-ecbf39108aa2",
        "routes_url": "/v2/apps/6064d98a-95e6-400b-bc03-be65e6d59622/routes",
        "events_url": "/v2/apps/6064d98a-95e6-400b-bc03-be65e6d59622/events",
        "service_bindings_url": "/v2/apps/6064d98a-95e6-400b-bc03-be65e6d59622/service_bindings",
        "route_mappings_url": "/v2/apps/6064d98a-95e6-400b-bc03-be65e6d59622/route_mappings"
      }
    }

will result to an object having extra methods such as

If you take a look at the application class, you will see that it has extra methods (start, stop and so on)

So, you can for a given application do

app = client.v2.apps['app-guid' ] # similar to client.v2.apps.get('app-guid')
app_service_bindings = [ s for s in app.service_bindings() ]
# or if you don't care about the application object
app_service_bindings = [ s for s in client.v2.apps.list_service_bindings('app-guid') ]
# then you can resolve service instances
app_service_instances = [ s.service_instance() for s in app_service_bindings ]
# then from service instance  you load service plans
app_service_plans = [ s.service_plan() for s in app_service_instances ]
# and then the service
app_services = [ s.service() for s in app_service_plans ]

Take a look at the v2 documentation or the v3 documentation.

If you see an entry that does not exist I will gladly implement it.

I hope you found answers

johnalejandro001 commented 4 years ago

Thank you very much for helping me out with this. Definitely it helps me to get more clarity. You guys have done a pretty awesome job. Here is the actual code snippet that I'm slowly working on with all the data available. So the far the objects are to retrieve app-name, db-host and export them to csv.

import os import logging import threading from cloudfoundry_client.client import CloudFoundryClient import arrow _logger = logging.getLogger(name) logging.basicConfig(level=logging.INFO, format='%(levelname)5s - %(name)s - %(message)s') target_endpoint = 'https://api.paas.cool' proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', '')) client = CloudFoundryClient(target_endpoint, proxy=proxy, verify=True)

init with user credentials

client.init_with_user_credentials('username', 'password')

init with refresh token (that will retrieve a fresh access token)

print("Displaying all available Organization guid available") for organization in client.v2.organizations: print(organization['metadata']['guid']['entity']['name'])

Setting scope to an specific org/space

print ("Setting Up space") target_space = 'our_uat_space' target_org = 'our_uat_org' target_space_apps = client.v2.apps.list (space_guid=target_space,organization_guid=target_org)

Iterating over the list of apps available per space/org

for app in target_space_apps: pass app_guid = (app['metadata']['guid']) app_name = (app['entity']['name']) app_service_bindings = [s for s in client.v2.apps.list_service_bindings (app_guid)] print(app_name) try: if len(app_service_bindings) > 1: print(app_service_bindings[0]['entity']) filename = f"services_export.csv" with open (filename, 'w', newline='') as csvFile: writer = csv.writer(csvFile, dialect='excel', delimiter=',') headers = [ 'app-name', 'db-name', 'dc' ] writer.writerow (headers) service = [ row["app_name"], row["app_service_bindings"] ] writer.writerow(service) except IndexError: pass continue

johnalejandro001 commented 4 years ago

I'm very new to Python as you might note. So any constructive critics will be taken pretty positive.