1
votes

I am new to google cloud applications and so I was figuring out to use various methods to list all different services list in using GCP SDK in python.

I wrote following code to create a client instance for Compute services to list down all Firewalls present in the project in Python3.

from google.oauth2 import service_account
import googleapiclient.discovery

credentials = service_account.Credentials.from_service_account_file(filename='/path/to/cred/file.json')

client = googleapiclient.discovery.build(
    'compute', 'v1', credentials=credentials)

def __test_firewall():
    result = client.firewalls().list(project='project-id').execute()
    return result['items'] if 'items' in result else None

This code returned me all the firewalls present in the project successfully as I wanted.

I want something similar that I can use to list down all Cloud Functions in GCP through SDK and I do not want to use CLI. I could not figure out the way to do it so posting here.

Any help is highly appreciated. Thanks in advance. ????

1

1 Answers

2
votes

There is no libraries for that. You can call directly the API or use the API Discovery library like this: (with this dependency google-api-python-client)

from googleapiclient.discovery import build


service = build('cloudfunctions', 'v1')
locations = service.projects().locations().list(name="projects/YOUR_PROJECT_ID").execute()
print(locations) # here to list all the locations available for Cloud Functions on your project
# Loop on the location and perform a sample request like this.
functions = service.projects().locations().functions().list(parent="projects/YOUR_PROJECT_ID/locations/us-central1").execute()
print(functions)

You have to look into all the location and aggregate the results to have a full view on all your functions of your project.