Skip to content

Conversation

twu
Copy link
Contributor

@twu twu commented Jul 14, 2025

Related-To: https://github.com/grafana/support-escalations/issues/17189, https://github.com/grafana/grafana-enterprise/pull/9055

What?

Adds duplicate checking to grafana_role_assignment_item resource to prevent sending duplicate team/user/service account IDs in role assignment API requests.

  • Modified Create() method in resource_role_assignment_item.go to check for existing assignments before appending new ones
  • Added TestAccRoleAssignmentItem_NoDuplicates test case to verify the fix
  • Applied the same logic to teams, users, and service accounts

Why?

When multiple grafana_role_assignment_item resources target the same role, the provider was appending assignments to existing ones without checking for duplicates. This caused API requests with duplicate IDs, resulting in 400 errors from Grafana. I opened a PR to gracefully handle duplicates but I'm not 100% whether this is how it should be done (See https://github.com/grafana/grafana-enterprise/pull/9055)

Reproduce

data "grafana_role" "role" {
  name     = "plugins:grafana-oncall-app:admin"
}

data "grafana_team" "team_a" {
  name     = "A"
}

resource "grafana_user" "alice" {
  email    = "alice@localhost"
  login    = "alice"
  password = "alice"
}

resource "grafana_role_assignment_item" "alice" {
  role_uid = data.grafana_role.role.uid
  user_id  = grafana_user.alice.id
}

resource "grafana_role_assignment_item" "team_a" {
  role_uid = data.grafana_role.role.uid  // Same role as alice assignment
  team_id  = data.grafana_team.team_a.id
}

This fails with Failed to set role assignments: [PUT /access-control/roles/{roleUID}/assignments] setRoleAssignments (status 400): {}

The second assignment item would fetch existing assignments [users: [15], teams: []], append team 2, but due to state inconsistencies could result in duplicate entries like [users: [15], teams: [2, 2]].

We now check if assignment already exists before adding, preventing duplicates.

Copy link
Contributor

In order to lower resource usage and have a faster runtime, PRs will not run Cloud tests automatically.
To do so, a Grafana Labs employee must trigger the cloud acceptance tests workflow manually.

@twu twu changed the title fix(roles): Handle issue where duplicate ids cause invalid requests fix(roles): Handle issue with duplicate IDs cause 400 despite using existing resources Jul 14, 2025

This comment was marked as outdated.

Copy link
Contributor

🔧 Schema Update Required

This PR introduces changes that affect the Terraform provider schema. The issue templates need to be updated.

📋 Required Action

Please run the generator to update templates:

make generate-templates

Then commit the updated files:

  • provider_schema.json
  • .github/ISSUE_TEMPLATE/3-bug-report-enhanced.yml

This comment was generated automatically because schema changes were detected.

Copy link
Contributor

🔧 Schema Update Required

This PR introduces changes that affect the Terraform provider schema. The issue templates need to be updated.

📋 Required Action

Please run the generator to update templates:

make generate-templates

Then commit the updated files:

  • provider_schema.json
  • .github/ISSUE_TEMPLATE/3-bug-report-enhanced.yml

This comment was generated automatically because schema changes were detected.

@twu twu marked this pull request as ready for review July 15, 2025 08:41
@twu twu requested a review from a team as a code owner July 15, 2025 08:41
Copy link
Contributor

🔧 Schema Update Required

This PR introduces changes that affect the Terraform provider schema. The issue templates need to be updated.

📋 Required Action

Please run the generator to update templates:

make generate-templates

Then commit the updated files:

  • provider_schema.json
  • .github/ISSUE_TEMPLATE/3-bug-report-enhanced.yml

This comment was generated automatically because schema changes were detected.

This comment was marked as outdated.

1 similar comment

This comment was marked as outdated.

Copy link
Contributor

🔧 Schema Update Required

This PR introduces changes that affect the Terraform provider schema. The issue templates need to be updated.

📋 Required Action

Please run the generator to update templates:

make generate-templates

Then commit the updated files:

  • provider_schema.json
  • .github/ISSUE_TEMPLATE/3-bug-report-enhanced.yml

This comment was generated automatically because schema changes were detected.

@twu twu enabled auto-merge (squash) July 15, 2025 10:37
@twu twu disabled auto-merge July 15, 2025 10:43
@spinillos spinillos merged commit 421c932 into main Jul 15, 2025
30 of 32 checks passed
@spinillos spinillos deleted the twu/depdupe-resource-ids branch July 15, 2025 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants