Skip to content

cloudflare_api_token_permissions_groups_list usage in terraform cloudflare provider v5 #4

@nuxdie

Description

@nuxdie

Hey, just passing by. I noticed that you have this

# # TODO cloudflare_api_token_permissions_groups_list not working
# # permission_groups = [for group in data.cloudflare_api_token_permissions_groups_list.all.result : group if contains(["Zone Read", "Zone Settings Read", "DNS Write"], group.name) ]
# # permission_groups = [
# # data.cloudflare_api_token_permissions_groups_list.all.zone["Zone Read"],
# # data.cloudflare_api_token_permissions_groups_list.all.zone["Zone Settings Read"],
# # data.cloudflare_api_token_permissions_groups_list.all.zone["DNS Write"],
# # ]
# permission_groups = [
# { id = "517b21aee92c4d89936c976ba6e4be55" }, # Zone Settings Read
# { id = "c8fed203ed3043cba015a93ad1616f1f" }, # Zone Read
# { id = "4755a26eedb94da69e1066d98aa820be" } # DNS Write
# ]

I'm in the process of upgrading to terraform cloudflare provider v5 myself. Believe it or not, but your commented out code is the only thing that mentions cloudflare_api_token_permissions_groups_list data source anywhere on the web ATM.

anyways, here's what i found. querrying https://api.cloudflare.com/client/v4/user/tokens/permission_groups i got a list like this:

{
  "result": [
    {
      "id": "19637fbb73d242c0a92845d8db0b95b1",
      "name": "AI Audit Read",
      "description": "Grants access to reading AI Audit",
      "scopes": [
        "com.cloudflare.api.account.zone"
      ]
    },
    {
      "id": "1ba6ab4cacdb454b913bbb93e1b8cb8c",
      "name": "AI Audit Write",
      "description": "Grants access to reading and editing AI Audit",
      "scopes": [
        "com.cloudflare.api.account.zone"
      ]
    },
    {
      "id": "4dc8917b4b40457d88d3035d5dadb054",
      "name": "AI Gateway Read",
      "description": "Grants access to reading AI Gateways",
      "scopes": [
        "com.cloudflare.api.account"
      ]
    },
...

and so on. So, result is actually a giant flat array of objects. (in v4 of cloudflare provider it was a bit different: https://registry.terraform.io/providers/cloudflare/cloudflare/4.51.0/docs/data-sources/api_token_permission_groups#permissions-1 split, into account, user, r2 and zone, BTW.)

basically your idea of:

#     # permission_groups = [for group in data.cloudflare_api_token_permissions_groups_list.all.result : group if contains(["Zone Read", "Zone Settings Read", "DNS Write"], group.name) ]

was really close to what you need. here's the solution that works for me:

data "cloudflare_api_token_permissions_groups_list" "all" {
  account_id = cloudflare_account.<account>.id
}

locals {
  # Create a map of permission names to IDs
  permission_map = {
    for group in data.cloudflare_api_token_permissions_groups_list.all.result :
    group.name => group.id
  }
}

resource "cloudflare_api_token" "my_api_token" {
  name = "my_api_token"
  policies = [{
    effect = "allow"
    permission_groups = [
      {id = local.permission_map["Workers R2 Storage Write"]},
      {id = local.permission_map["Workers R2 Storage Read"]},
    ]
    resources = { 
      "com.cloudflare.api.account.${cloudflare_account.<account>.id}" = "*" 
    }
  }]
}

as you can see, i'm creating a token for using r2 on cloudflare, but you can use any permission descriptions you can get from https://developers.cloudflare.com/api/resources/user/subresources/tokens/subresources/permission_groups/methods/list/ and any resources that are relevant to you.

hope this helps!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions