有没有一种方法可以列出给定项目中已启用的 Google Cloud API?

huangapple go评论54阅读模式
英文:

Is there a way of listing the enable google cloud APIs in a given project?

问题

我想使用Python或其他GCP API列出给定Google Cloud项目中的每个已启用API...

我找不到在文档中复制执行此操作的正确代码片段,我最接近的是来自Python API的list_services方法,但它是自动生成的模板代码,无法运行。

英文:

I want to list every enabled API in a given Google Cloud project by using Python or another gcp API...

I can't find the right snippet code for reproducing some method that do that in the docs, the closest thing I reach was the list_services method from the python API but it is auto generated template code and does not run.

答案1

得分: 1

将这个非常好的回答1由@JohnHanley作为社区维基发布,以使将来可能遇到这种用例的社区成员受益:

根据John的说法:

> 这是我写的一个示例。请注意,此代码不处理nextPageToken,因此只打印前50个服务。添加循环代码。

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

project = 'projects/myproject'

service = discovery.build('serviceusage', 'v1', credentials=credentials)
request = service.services().list(parent=project)

response = ''

try:
    response = request.execute()
except Exception as e:
    print(e)
    exit(1)

# FIX - This code does not process the nextPageToken
# next = response.get('nextPageToken')

services = response.get('services')

for index in range(len(services)):
    item = services[index]

    name = item['config']['name']
    state = item['state']

    print("%-50s %s" % (name, state))

> 此代码的输出类似于以下内容:

abusiveexperiencereport.googleapis.com             DISABLED
acceleratedmobilepageurl.googleapis.com            DISABLED
accessapproval.googleapis.com                      DISABLED
accesscontextmanager.googleapis.com                DISABLED
actions.googleapis.com                             DISABLED
adexchangebuyer-json.googleapis.com                DISABLED
adexchangebuyer.googleapis.com                     DISABLED
adexchangeseller.googleapis.com                    DISABLED
adexperiencereport.googleapis.com                  DISABLED
admin.googleapis.com                               ENABLED
adsense.googleapis.com                             DISABLED

请随时编辑此答案以获取其他信息。

英文:

Posting this very good answer by @JohnHanley as community wiki for the benefit of the community members that might encounter this use case in the future:

Per John:

>Here is an example that I wrote. Note. This code does not process the nextPageToken so it only prints the first 50 services. Add code to loop.

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

project = 'projects/myproject'

service = discovery.build('serviceusage', 'v1', credentials=credentials)
request = service.services().list(parent=project)

response = ''

try:
    response = request.execute()
except Exception as e:
    print(e)
    exit(1)

# FIX - This code does not process the nextPageToken
# next = response.get('nextPageToken')

services = response.get('services')

for index in range(len(services)):
    item = services[index]

    name = item['config']['name']
    state = item['state']

    print("%-50s %s" % (name, state))

> The output of this code looks similar to this:

abusiveexperiencereport.googleapis.com             DISABLED
acceleratedmobilepageurl.googleapis.com            DISABLED
accessapproval.googleapis.com                      DISABLED
accesscontextmanager.googleapis.com                DISABLED
actions.googleapis.com                             DISABLED
adexchangebuyer-json.googleapis.com                DISABLED
adexchangebuyer.googleapis.com                     DISABLED
adexchangeseller.googleapis.com                    DISABLED
adexperiencereport.googleapis.com                  DISABLED
admin.googleapis.com                               ENABLED
adsense.googleapis.com                             DISABLED

Feel free to edit this answer for additional information.

答案2

得分: 0

from googleapiclient import discovery

project_id = '我的项目标识'

client = discovery.build('serviceusage', 'v1beta1')
response = client.services().list(
                parent=f'projects/{project_id}',
                filter='state:ENABLED'
            ).execute()

[enabled_service.get('config').get('name') for enabled_service in response.get('services')]
英文:
from googleapiclient import discovery

project_id = 'my-project-id'

client = discovery.build('serviceusage', 'v1beta1')
response = client.services().list(
                parent=f'projects/{project_id}',
                filter='state:ENABLED'
            ).execute()

[enabled_service.get('config').get('name') for enabled_service in response.get('services')]

huangapple
  • 本文由 发表于 2023年6月5日 22:36:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定