Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support both UUIDs and names in consul_acl_role.policies #363

Merged
merged 3 commits into from
Oct 25, 2023

Conversation

remilapeyre
Copy link
Collaborator

@remilapeyre remilapeyre commented Oct 9, 2023

The consul_acl_role attribute policies only support UUIDs like:

resource "consul_acl_policy" "read-policy" {
  name        = "read-policy"
  rules       = "node \"\" { policy = \"read\" }"
}

resource "consul_acl_role" "read" {
  name        = "foo"

  policies = [
    consul_acl_policy.read-policy.id
  ]
}

This differs from the Consul API payload where the Policies attribute is actually a list of objects, not a list of strings:

{
  "Name": "example-role",
  "Policies": [
    {
      "ID": "783beef3-783f-f41f-7422-7087dc272765"
    }
  ]
}

This makes it possible to set the policies using either the ID, or the name:

{
  "Name": "example-role",
  "Policies": [
    {
      "ID": "783beef3-783f-f41f-7422-7087dc272765"
    },
    {
      "Name: "Test"
    }
  ]
}

This is not supported by the consul_acl_role resource.

Not being able to add a policy using its name makes things harder for users and hanging its schema to make policies a list of objects would be best, but it would also break the backward compatibility of the provider which I always try not to.

This patch makes it possible possible to use either and tries first to fetch the corresponding policy using the ID, then using the name.

resourceConsulACLRoleRead() is also updated to set the correct value in Terraform so that we don't have a perpetual diff.

Closes #344.

The `consul_acl_role` attribute `policies` only support UUIDs like:

```terraform
resource "consul_acl_policy" "read-policy" {
  name        = "read-policy"
  rules       = "node \"\" { policy = \"read\" }"
}

resource "consul_acl_role" "read" {
  name        = "foo"

  policies = [
    consul_acl_policy.read-policy.id
  ]
}
```

This differs from the Consul API payload where the `Policies` attribute
is actually a list of objects, not a list of strings:

```json
{
  "Name": "example-role",
  "Policies": [
    {
      "ID": "783beef3-783f-f41f-7422-7087dc272765"
	}
  ]
}
```

This makes it possible to set the policies using either the ID, or the
name:

```json
{
  "Name": "example-role",
  "Policies": [
    {
      "ID": "783beef3-783f-f41f-7422-7087dc272765"
	},
	{
	  "Name: "Test"
	}
  ]
}
```

This is not supported by the `consul_acl_role` resource.

Not being able to add a policy using its name makes things harder for
users and hanging its schema to make `policies` a list of objects would
be best, but it would also break the backward compatibility of the provider
which I always try not to.

This patch makes it possible possible to use either and tries first to
fetch the corresponding policy using the ID, then using the name.

`resourceConsulACLRoleRead()` is also updated to set the correct value in
Terraform so that we don't have a perpetual diff.
Copy link
Contributor

@dhiaayachi dhiaayachi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a comment that could lead to simplified code, but it LGTM!

consul/resource_consul_acl_role.go Outdated Show resolved Hide resolved
@remilapeyre remilapeyre merged commit fe9f1cd into hashicorp:master Oct 25, 2023
4 checks passed
@remilapeyre remilapeyre deleted the issue-344 branch October 25, 2023 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

consul_acl_role support policies by name
2 participants