Dem*_*ara 10

Terraform 的 Google Cloud 提供商(版本 >= 4.14.0)现在支持创建 API 密钥

用一个例子更新答案(按照@noamt的建议,谢谢)。

在这种情况下,关键是将可以使用的 API 限制为某些 GMap 的 API:

resource "google_apikeys_key" "maps" {
  name         = "maps-api-key"
  display_name = "Nice name displayed in the UI"

  restrictions {
        # Example of whitelisting Maps Javascript API and Places API only
        api_targets {
            service = "maps-backend.googleapis.com"
        }
        api_targets {
            service = "places-backend.googleapis.com"
        }
  }
}
Run Code Online (Sandbox Code Playgroud)


R. *_*oma 5

还没有,但谷歌似乎正在努力公开 API 密钥管理的 API。最新的 cloud sdk(使用 287.0.0 测试)具有 alpha 支持,如下所示:

$ gcloud alpha services api-keys
ERROR: (gcloud.alpha.services.api-keys) Command name argument expected.

Available commands for gcloud alpha services api-keys:

      clone                   *(ALPHA)*  Create a new API key with the same
                              metadata as input key.
      create                  *(ALPHA)*  Create an API key.
      delete                  *(ALPHA)*  Delete an API key.
      describe                *(ALPHA)*  Describe an API key's metadata.
      get-key-string          *(ALPHA)*  Get key string of an API key.
      list                    *(ALPHA)*  Lists API keys.
      lookup                  *(ALPHA)*  Look up resource name of a key string.
      undelete                *(ALPHA)*  Undelete an API key.
      update                  *(ALPHA)*  Update an API key's metadata.
Run Code Online (Sandbox Code Playgroud)

使用 列出项目 API 密钥时,--log-http您可以看到使用的 API 端点:

$ gcloud alpha services api-keys list --project $PROJECT --log-http
...
==== request start ====
uri: https://apikeys.googleapis.com/v2alpha1/projects/$PROJECT/keys?alt=json
...
Run Code Online (Sandbox Code Playgroud)

即使云 sdk 正在使用v2alpha1,也有v2beta1可用的。验证如下:

$ curl -s -H"Authorization: Bearer $(gcloud auth print-access-token)" \
   https://apikeys.googleapis.com/v2beta1/projects/$PROJECT/keys
{
  "keys": [
    {
      "name": "projects/REDACTED/keys/REDACTED",
      "displayName": "REDACTED",
      "createTime": "2019-04-15T10:39:53.558Z",
      "updateTime": "2019-04-15T10:40:06.616639Z",
      "restrictions": {
        "androidKeyRestrictions": {},
        "apiTargets": [
          {
            "service": "geocoding_backend"
          }
        ]
      },
      "state": "ACTIVE"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

由于 terraform google 提供商通常会很快添加新功能,因此我只能假设支持即将到来。你可能想创建一个 Github 问题来表达你的兴趣。或者查看beta 提供商的更改日志。