Skip to content

Commit a35e6d3

Browse files
grafana_role_assignment: Support new service account ID format (#1036)
When #824 was done, this was missed because there were no tests for it
1 parent cd23418 commit a35e6d3

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

docs/resources/role_assignment.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ resource "grafana_role_assignment" "test" {
6161

6262
### Optional
6363

64-
- `service_accounts` (Set of Number) IDs of service accounts that the role should be assigned to.
64+
- `service_accounts` (Set of String) IDs of service accounts that the role should be assigned to.
6565
- `teams` (Set of String) IDs of teams that the role should be assigned to.
6666
- `users` (Set of Number) IDs of users that the role should be assigned to.
6767

internal/resources/grafana/resource_role_assignment.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,13 @@ func ResourceRoleAssignment() *schema.Resource {
6060
Optional: true,
6161
ForceNew: false,
6262
Description: "IDs of service accounts that the role should be assigned to.",
63+
// Ignore the org ID of the team when hashing. It works with or without it.
64+
Set: func(i interface{}) int {
65+
_, saID := SplitOrgResourceID(i.(string))
66+
return schema.HashString(saID)
67+
},
6368
Elem: &schema.Schema{
64-
Type: schema.TypeInt,
69+
Type: schema.TypeString,
6570
},
6671
},
6772
},
@@ -100,9 +105,11 @@ func UpdateRoleAssignments(ctx context.Context, d *schema.ResourceData, meta int
100105
_, teamIDStr := SplitOrgResourceID(t.(string))
101106
teams[i], _ = strconv.Atoi(teamIDStr)
102107
}
103-
serviceAccounts, err := collectRoleAssignmentsToFn(d.Get("service_accounts"))
104-
if err != nil {
105-
return diag.Errorf("invalid service account IDs specified %v", err)
108+
serviceAccountsStrings := d.Get("service_accounts").(*schema.Set).List()
109+
serviceAccounts := make([]int, len(serviceAccountsStrings))
110+
for i, t := range serviceAccountsStrings {
111+
_, saIDStr := SplitOrgResourceID(t.(string))
112+
serviceAccounts[i], _ = strconv.Atoi(saIDStr)
106113
}
107114

108115
ra := &gapi.RoleAssignments{
@@ -151,7 +158,11 @@ func setRoleAssignments(assignments *gapi.RoleAssignments, d *schema.ResourceDat
151158
if err := d.Set("teams", teams); err != nil {
152159
return err
153160
}
154-
if err := d.Set("service_accounts", assignments.ServiceAccounts); err != nil {
161+
serviceAccounts := make([]string, len(assignments.ServiceAccounts))
162+
for i, sa := range assignments.ServiceAccounts {
163+
serviceAccounts[i] = strconv.Itoa(sa)
164+
}
165+
if err := d.Set("service_accounts", serviceAccounts); err != nil {
155166
return err
156167
}
157168

internal/resources/grafana/resource_role_assignment_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestRoleAssignments(t *testing.T) {
3434
"grafana_role_assignment.test", "users.#", "2",
3535
),
3636
resource.TestCheckResourceAttr(
37-
"grafana_role_assignment.test", "service_accounts.#", "0",
37+
"grafana_role_assignment.test", "service_accounts.#", "1",
3838
),
3939
resource.TestCheckResourceAttr(
4040
"grafana_role_assignment.test", "teams.#", "1",
@@ -113,10 +113,17 @@ resource "grafana_user" "test_user2" {
113113
password = "12345"
114114
}
115115
116+
resource "grafana_service_account" "test" {
117+
name = "%[1]s-terraform-test"
118+
role = "Editor"
119+
is_disabled = false
120+
}
121+
116122
resource "grafana_role_assignment" "test" {
117123
role_uid = grafana_role.test.uid
118124
users = [grafana_user.test_user.id, grafana_user.test_user2.id]
119125
teams = [grafana_team.test_team.id]
126+
service_accounts = [grafana_service_account.test.id]
120127
}
121128
`, name)
122129
}

0 commit comments

Comments
 (0)