如何使用Google Cloud SDK列出所有Cloud Functions?

问题描述

我是Google云应用程序的新手,因此我想在python中使用GCP SDK时要使用各种方法列出所有不同的服务列表。

我编写了以下代码来创建Compute服务的客户端实例,以列出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

代码可根据需要向我成功返回项目中存在的所有防火墙。

我想要类似的东西,可以用来通过SDK列出GCP中的所有 Cloud Functions ,而我不想使用CLI。我不知道要怎么做,所以在这里发布。

我们非常感谢您的帮助。 提前致谢。 ?

解决方法

没有用于此的库。您可以直接调用API或使用API​​发现库,如下所示:(具有此依赖性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)

您必须查看所有位置并汇总结果才能全面了解项目的所有功能。