如何使用 Google Cloud SDK 列出所有 Cloud Functions?
Posted
技术标签:
【中文标题】如何使用 Google Cloud SDK 列出所有 Cloud Functions?【英文标题】:How can I list all Cloud Functions using Google Cloud SDK? 【发布时间】:2020-09-10 09:30:48 【问题描述】:我是谷歌云应用程序的新手,所以我想在 python 中使用 GCP SDK 时使用各种方法列出所有不同的服务列表。
我编写了以下代码来为计算服务创建一个客户端实例,以列出 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。我想不出办法,所以在这里发帖。
非常感谢任何帮助。 提前致谢。 ????
【问题讨论】:
【参考方案1】:没有图书馆。您可以直接调用API 或像这样使用API Discovery 库:(带有此依赖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)
您必须查看所有位置并汇总结果,才能全面了解项目的所有功能。
【讨论】:
用破折号-
代替us-central1
以从所有位置获取。以上是关于如何使用 Google Cloud SDK 列出所有 Cloud Functions?的主要内容,如果未能解决你的问题,请参考以下文章