英文:
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')]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论