使用 REST API 显示应用于 Google 云平台计算引擎实例的访客策略

Displaying Guest Policies Applied to a Google Cloud Platform Compute Engine Instance Using the REST API

我能够使用 Cloud SDK(gcloud):

gcloud beta compute os-config guest-policies lookup \
$GCE_INSTANCE_NAME \
--zone=$GCE_INSTANCE_ZONE

#=>

┌──────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                           SOFTWARE RECIPES                                               │
├───────────────────────────────────────────────────────────┬────────────────────┬─────────┬───────────────┤
│                          SOURCE                           │        NAME        │ VERSION │ DESIRED_STATE │
├───────────────────────────────────────────────────────────┼────────────────────┼─────────┼───────────────┤
│ projects/$GCP_PROJECT_ID/guestPolicies/. . .              │        . . .       │ . . .   │   . . .       │
│ projects/$GCP_PROJECT_ID/guestPolicies/$GUEST_POLICY_NAME │ $GUEST_POLICY_NAME │ 1.0     │ INSTALLED     │
│ projects/$GCP_PROJECT_ID/guestPolicies/. . .              │        . . .       │ . . .   │   . . .       │
└───────────────────────────────────────────────────────────┴────────────────────┴─────────┴───────────────┘

如何使用 REST API 检索相同的响应? lookup 方法似乎在 projects.guestPolicies 资源页面 here.

中丢失

您正在寻找 projects.zones.instances.lookupEffectiveGuestPolicy REST 方法,已找到 here

在 Ubuntu 的 任何 版本上安装软件的来宾策略示例:

curl \
--data-raw '{ "osArchitecture": "", "osShortName": "UBUNTU", "osVersion": "" }' \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--header 'Content-Type: text/plain' \
--location \
--request POST \
"https://osconfig.googleapis.com/v1beta/projects/$GCP_PROJECT_NUMBER/zones/$GCE_INSTANCE_ZONE/instances/$GCE_INSTANCE_NAME:lookupEffectiveGuestPolicy"

#=>

{
  "softwareRecipes": [
    . . .
    {
      "source": "projects/$GCP_PROJECT_NUMBER/guestPolicies/$GUEST_POLICY_NAME",
      "softwareRecipe": {
        "name": "$GUEST_POLICY_NAME",
        "version": "1.0",
        . . .
        "desiredState": "INSTALLED"
      }
    },
    . . .
  ]
}

注意$GCP_PROJECT_NUMBER$GCP_PROJECT_ID:

不同
gcloud projects describe $GCP_PROJECT_NAME

#=>

. . .
projectId: $GCP_PROJECT_ID
projectNumber: "$GCP_PROJECT_NUMBER"

注意:POST正文键的值

  • "osArchitecture"
  • "osShortName"
  • "osVersion"

可以使用 gcloud:

找到 $GUEST_POLICY_NAME
gcloud beta compute os-config guest-policies describe \
$GUEST_POLICY_NAME \
--flatten="assignment.osTypes" \
--format="table[box](assignment.osTypes.osArchitecture,
                     assignment.osTypes.osShortName,
                     assignment.osTypes.osVersion)"

#=>

┌─────────────────┬───────────────┬────────────┐
│ OS_ARCHITECTURE │ OS_SHORT_NAME │ OS_VERSION │
├─────────────────┼───────────────┼────────────┤
│ . . .           │ . . .         │ . . .      │
│                 │ UBUNTU        │            │
│ . . .           │ . . .         │ . . .      │
└─────────────────┴───────────────┴────────────┘

或 REST API:

curl \
--header "Authorization: Bearer $(gcloud auth print-access-token)" \
--location \
--request GET \
"https://osconfig.googleapis.com/v1beta/projects/$GCP_PROJECT_NUMBER/guestPolicies/$GUEST_POLICY_NAME"

#=>

{
    . . .
    "assignment": {
        . . .
        "osTypes": [
            . . .
            {
                "osShortName": "UBUNTU"
            }
            . . .
        ]
        . . .
    },
    . . .
}

注意:如果 "osArchitecture" and/or "osVersion" 缺失或为空,您应该在使用 REST 时将这些值保留为空字符串上面的方法 ("").