【问题标题】:How can I list all Cloud Functions using Google Cloud SDK?如何使用 Google Cloud SDK 列出所有 Cloud Functions?
【发布时间】: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。我想不出办法,所以在这里发帖。

非常感谢任何帮助。 提前致谢。 ????

【问题讨论】:

    标签: google-cloud-platform google-cloud-functions python-3.7


    【解决方案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 以从所有位置获取。
    猜你喜欢
    • 2021-12-26
    • 2022-01-26
    • 2019-05-14
    • 2021-03-09
    • 2018-03-27
    • 2017-08-18
    • 2020-03-15
    • 2017-11-29
    • 1970-01-01
    相关资源
    最近更新 更多