【问题标题】:Google Cloud Platform: List available projects using apiGoogle Cloud Platform:使用 api 列出可用项目
【发布时间】:2020-02-10 11:10:27
【问题描述】:

我想专门通过api调用(python)来达到gcloud projects list的相同效果。

但是,我在浏览文档时唯一能遇到的就是这个。

此操作是否专门与资源管理器 API 相关联?

对于其他项目的可见性会发生什么?如果 RM 绑定到一个项目,它如何查看(并因此列出)其他项目?

【问题讨论】:

    标签: api google-cloud-platform gcloud google-cloud-iam


    【解决方案1】:

    您只能列出您有权访问的项目。这意味着您无法查看所有项目,除非您有权访问它们。在下面的示例中,我展示了需要哪些范围。这也意味着您可以跨账户列出项目。这使您可以使用示例中指定的凭据查看您可以访问哪些项目。我将展示如何使用应用程序默认凭据 (ADC) 和服务帐户凭据(Json 文件格式)。

    更多信息你可以阅读我关于项目的文章here

    这些示例已在 Windows 10 Professional 上使用 Python 3.6 进行了测试。这些示例将完全按照 CLI 显示项目列表。

    使用 Python 客户端库的示例 1(服务发现方法):

    from googleapiclient import discovery
    from oauth2client.client import GoogleCredentials
    from google.oauth2 import service_account
    
    # Example using the Python Client Library
    
    # Documentation
    # https://github.com/googleapis/google-api-python-client
    # https://developers.google.com/resources/api-libraries/documentation/cloudresourcemanager/v2/python/latest/
    
    # Library Installation
    # pip install -U google-api-python-client
    # pip install -U oauth2client
    
    # Requires one of the following scopes
    # https://www.googleapis.com/auth/cloud-platform
    # https://www.googleapis.com/auth/cloud-platform.read-only
    # https://www.googleapis.com/auth/cloudplatformprojects
    # https://www.googleapis.com/auth/cloudplatformprojects.readonly
    
    print('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))
    
    # Uncomment to use Application Default Credentials (ADC)
    credentials = GoogleCredentials.get_application_default()
    
    # Uncomment to use Service Account Credentials in Json format
    # credentials = service_account.Credentials.from_service_account_file('service-account.json')
    
    service = discovery.build('cloudresourcemanager', 'v1', credentials=credentials)
    
    request = service.projects().list()
    
    while request is not None:
        response = request.execute()
    
        for project in response.get('projects', []):
            print('{:<20} {:<22} {:<21}'.format(project['projectId'], project['name'], project['projectNumber']))
    
        request = service.projects().list_next(previous_request=request, previous_response=response)
    

    使用 Python Google Cloud Resource Manager API 客户端库的示例 2:

    from google.cloud import resource_manager
    
    # Example using the Python Google Cloud Resource Manager API Client Library
    
    # Documentation
    # https://pypi.org/project/google-cloud-resource-manager/
    # https://github.com/googleapis/google-cloud-python
    # https://googleapis.github.io/google-cloud-python/latest/resource-manager/index.html
    # https://googleapis.github.io/google-cloud-python/latest/resource-manager/client.html
    # https://googleapis.github.io/google-cloud-python/latest/resource-manager/project.html
    
    # Library Installation
    # pip install -U google-cloud-resource-manager
    
    # Requires one of the following scopes
    # https://www.googleapis.com/auth/cloud-platform
    # https://www.googleapis.com/auth/cloud-platform.read-only
    # https://www.googleapis.com/auth/cloudplatformprojects
    # https://www.googleapis.com/auth/cloudplatformprojects.readonly
    
    print('{:<20} {:<22} {:<21}'.format('PROJECT_ID', 'NAME', 'PROJECT_NUMBER'))
    
    # Uncomment to use Application Default Credentials (ADC)
    client = resource_manager.Client()
    
    # Uncomment to use Service Account Credentials in Json format
    # client = resource_manager.Client.from_service_account_json('service-account.json')
    
    for project in client.list_projects():
        print('{:<20} {:<22} {:<21}'.format(project.project_id, project.name, project.number))
    

    【讨论】:

    【解决方案2】:

    自从@john-hanley 的彻底回答以来,客户端库已经发生了变化。这是一个使用 API v3 和客户端 v1.3.3 的版本:

    from google.cloud.resourcemanager import ProjectsClient
    
    ORGANIZATION_ID = 'your-org's-numeric-id'
    
    client = ProjectsClient()
    project_pager = client.list_projects(parent=f'organizations/{ORGANIZATION_ID}')
    pjs = []
    for page in project_pager:
        pjs.append(page)
    

    pjsgoogle.cloud.resourcemanager_v3.types.projects.Project 对象的列表。

    请注意,这假设您使用的是应用程序默认凭据 (ADC),并且相关用户帐户具有 resourcemanager.projects.list 权限。

    要使用 ADC,请在更改用户帐户后运行 gcloud auth application-default logingcloud auth login --update-adc

    如果您可以成功运行gcloud projects list,那么您就拥有使用上述代码所需的访问权限。

    【讨论】:

      猜你喜欢
      • 2020-02-23
      • 2016-04-08
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 2019-03-28
      • 1970-01-01
      相关资源
      最近更新 更多