Is there any Python API to fetch the list of all the active resources(VM's, Buckets, Datasets, etc) and their metadata for a project in GCP?

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

Is there any Python API to fetch the list of all the active resources(VM's, Buckets, Datasets, etc) and their metadata for a project in GCP?

问题

我正在寻找一个Python API,用于获取GCP项目中的实例、存储桶、数据集、数据处理等资源的元数据。我查看了云监控API,但它只提供资源描述符、指标描述符和时间序列数据。我已经探索了资源管理器API,但我无法获取项目的服务级别资源。

例如,我需要VM元数据,如实例ID、安装的操作系统、内存配置、机器系列等。同样,我也需要存储、BigQuery、Dataproc和其他服务的元数据。

在Python中是否有这样的API可用?

英文:

I'm looking for a Python API to fetch instances, buckets, datasets, data proc, and other resource metadata for my project in GCP. I looked into cloud monitoring API, but it only gives resource descriptors, metric descriptors, and time series data. I've explored resource manager API, but I am unable to fetch resources at the service level for a project.

For example, I required VM metadata like instance_id, OS installed, memory config, machine series, etc. Similarly, I required metadata for storage, bigquery, dataproc, and other services too.

Is there any such API available in Python?

答案1

得分: 0

是的,我们可以使用Cloud Asset Inventory API获取项目中活动资源的列表。请查看从这里列出资源所需的权限:https://cloud.google.com/asset-inventory/docs/access-control

以下是使用Python的google-cloud-asset API检索所有带有元数据的资源的代码示例:

from google.oauth2 import service_account
from google.cloud import asset_v1

credentials_path = ''
credentials = service_account.Credentials.from_service_account_file(credentials_path)
project_id = ''
client = asset_v1.AssetServiceClient(credentials=credentials)
results = client.search_all_resources(scope=project_id)
for res in results:
    print(res)

您可以在这里了解更多有关API的信息:https://cloud.google.com/python/docs/reference/cloudasset/latest

英文:

Yes, we can use the Cloud Asset Inventory API to fetch the list of active resources for our project. Check the permissions required to list resources from here: https://cloud.google.com/asset-inventory/docs/access-control

Here is a code sample in Python that uses google-cloud-asset API to fetch all resources with metadata:

from google.oauth2 import service_account
from google.cloud import asset_v1


credentials_path = ''
credentials = 
service_account.Credentials.from_service_account_file(credentials_path)
project_id = ''
client = asset_v1.AssetServiceClient(credentials=credentials)
results = client.search_all_resources(scope=project_id)
for res in results:
    print(res)

You can explore more about the API here: https://cloud.google.com/python/docs/reference/cloudasset/latest

huangapple
  • 本文由 发表于 2023年3月9日 13:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75680703.html
匿名

发表评论

匿名网友

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

确定