Search code examples
google-cloud-platformgoogle-api-python-client

Is there a way of listing the enable google cloud APIs in a given project?


I want to list every enabled API in a given Google Cloud project by using Python or another gcp API...

I can't find the right snippet code for reproducing some method that do that in the docs, the closest thing I reach was the list_services method from the python API but it is auto generated template code and does not run.


Solution

  • Posting this very good answer by @JohnHanley as community wiki for the benefit of the community members that might encounter this use case in the future:

    Per John:

    Here is an example that I wrote. Note. This code does not process the nextPageToken so it only prints the first 50 services. Add code to loop.

    from googleapiclient import discovery
    from oauth2client.client import GoogleCredentials
    
    credentials = GoogleCredentials.get_application_default()
    
    project = 'projects/myproject'
    
    service = discovery.build('serviceusage', 'v1', credentials=credentials)
    request = service.services().list(parent=project)
    
    response = ''
    
    try:
        response = request.execute()
    except Exception as e:
        print(e)
        exit(1)
    
    # FIX - This code does not process the nextPageToken
    # next = response.get('nextPageToken')
    
    services = response.get('services')
    
    for index in range(len(services)):
        item = services[index]
    
        name = item['config']['name']
        state = item['state']
    
        print("%-50s %s" % (name, state))
    

    The output of this code looks similar to this:

    abusiveexperiencereport.googleapis.com             DISABLED
    acceleratedmobilepageurl.googleapis.com            DISABLED
    accessapproval.googleapis.com                      DISABLED
    accesscontextmanager.googleapis.com                DISABLED
    actions.googleapis.com                             DISABLED
    adexchangebuyer-json.googleapis.com                DISABLED
    adexchangebuyer.googleapis.com                     DISABLED
    adexchangeseller.googleapis.com                    DISABLED
    adexperiencereport.googleapis.com                  DISABLED
    admin.googleapis.com                               ENABLED
    adsense.googleapis.com                             DISABLED
    

    Feel free to edit this answer for additional information.