我正在尝试通过按照https://cloud.google.com/resource-manager/docs/performance#partial-responseinstances.aggregatedList
设置请求参数来从 Compute API 方法获得特定响应fields
但我越来越400 BAD REQUEST
。
有没有我可以参考的样本来获取聚合方法的部分响应?
我正在尝试通过按照https://cloud.google.com/resource-manager/docs/performance#partial-responseinstances.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
如您所见,结果与文档中解释的响应正文略有不同:
{
"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
请注意,如果您比较两个结果,您收到的实际响应在我认为导致您遇到的行为"zones/us-central1-a":
的字段之前有一个附加字段。instances:
如果您有兴趣使用部分资源并且只获得响应中的某些特定字段,您只需遵守您共享的文档中解释的语法规则,并在查询参数上相应地使用转义字符。
例如,如果您只对获取id
项目的 ' 以及instances
'感兴趣name
,machineType
并且status
我使用我的 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"
}
]
}
}
}