如何获得Method: instances的部分响应?aggregatedList GCP中的计算API



我试图通过将fields请求参数设置为https://cloud.google.com/resource-manager/docs/performance#partial-response来获得计算API方法instances.aggregatedList的特定响应

但我得到了400 BAD REQUEST

是否有一个样本,我可以参考获得部分响应的聚合方法?

如果使用以下CURL命令:

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://compute.googleapis.com/compute/v1/projects/[CHANGE-FOR-YOUR-PROJECT-ID]/aggregated/instances?maxResults=1"

你会注意到结果的形式类似于:

{
"id": "projects/[PROJECT-ID]/aggregated/instances",
"items": {
"zones/us-central1-a": {
"instances": [
{
"id": "[INSTANCE-ID]",
"creationTimestamp": "2020-09-21T06:22:21.604-07:00",
"name": "instance-1",
"description": "",
"tags": {
"items": [
"http-server",
"https-server"
],
"fingerprint": "XXXXXX"
},
"machineType": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a/machineTypes/e2-medium",
"status": "RUNNING",
"zone": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a",
"canIpForward": false,
"networkInterfaces": [
{
"network": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/global/networks/default",
"subnetwork": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/regions/us-central1/subnetworks/[SUBNETWORK_NAME]",
"networkIP": "10.8.0.13",
"name": "nic0",
... with a lot more fields

可以看到,结果与文档中解释的响应体略有不同:

{
"id": string,
"items": [
{
"scopeName": string,
"instances": [
{
"id": string,
"creationTimestamp": string,
"name": string,
"description": string,
"tags": {
"items": [
string
],
"fingerprint": string
},
"machineType": string,
"status": enum,
"statusMessage": string,
"zone": string,
"canIpForward": boolean,
"networkInterfaces": [
{
"network": string,
"subnetwork": string,
"networkIP": string,
"ipv6Address": string,
"name": string,
.... with a lot more fields

注意,如果比较两个结果,您收到的实际响应在instances:字段之前有一个额外的"zones/us-central1-a":字段,我认为这是导致您所经历的行为的原因。

如果您对处理部分资源感兴趣,并且只在响应中获得一些特定的字段,则只需遵守您共享的文档中解释的语法规则,并在查询参数中使用相应的转义字符。

。如果您只对获得项目的id以及instances'name,machineTypestatus感兴趣,我用我的GCP项目测试了来自Cloud Shell的以下curl命令,它没有问题:

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) "https://compute.googleapis.com/compute/v1/projects/[PROJECT-ID]/aggregated/instances?fields=id,items/zones%2Finstances(name,machineType,status)"

,我看到返回类似下面的内容:

{
"id": "projects/[PROJECT-ID]/aggregated/instances",
"items": {
"zones/us-central1-a": {
"instances": [
{
"name": "instance-1",
"machineType": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a/machineTypes/e2-medium",
"status": "RUNNING"
},
{
"name": "instance-2",
"machineType": "https://www.googleapis.com/compute/v1/projects/[PROJECT-ID]/zones/us-central1-a/machineTypes/e2-medium",
"status": "TERMINATED"
}
]
}
}
}

相关内容

  • 没有找到相关文章

最新更新