如何解决如何在 GCP 中获得 Method:instance.aggregatedList Compute API 的部分响应
我试图通过根据 https://cloud.google.com/resource-manager/docs/performance#partial-response 设置 instances.aggregatedList
请求参数来从 Compute API 方法 fields
获得特定响应
但我收到了 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
如您所见,结果与 response body explained in the documentation 略有不同:
{
"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,.... with a lot more fields
请注意,如果您比较两个结果,您收到的实际响应在 "zones/us-central1-a":
字段之前有一个额外的 instances:
字段,我认为这会导致您遇到的行为。
如果您对使用部分资源感兴趣并且仅在响应中获得某些特定字段,您只需遵守您共享的文档中解释的 syntax rules 并在您的文件中使用 escape characters accordingly查询参数。
例如如果您只对获取项目的 id
以及 instances
' name
、machineType
和 status
感兴趣,我测试了以下 curl 命令将 Cloud Shell 与我的 GCP 项目一起运行,并且没有问题:
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","status": "RUNNING"
},{
"name": "instance-2","status": "TERMINATED"
}
]
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。