Skip to content

Commit bd9bdad

Browse files
grafana_service_account: Fix updates on v11 (#1503)
Issue: #1493 Seems to be a regression in Grafana 11 where not passing the Role in an SA update results in a 500 error This should fix it, we can pass all the fields all the time in TF
1 parent b888796 commit bd9bdad

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

internal/resources/grafana/resource_service_account.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,10 @@ func UpdateServiceAccount(ctx context.Context, d *schema.ResourceData, meta inte
132132
return diag.FromErr(err)
133133
}
134134

135-
updateRequest := models.UpdateServiceAccountForm{}
136-
if d.HasChange("name") {
137-
updateRequest.Name = d.Get("name").(string)
138-
}
139-
if d.HasChange("role") {
140-
updateRequest.Role = d.Get("role").(string)
141-
}
142-
if d.HasChange("is_disabled") {
143-
updateRequest.IsDisabled = d.Get("is_disabled").(bool)
135+
updateRequest := models.UpdateServiceAccountForm{
136+
Name: d.Get("name").(string),
137+
Role: d.Get("role").(string),
138+
IsDisabled: d.Get("is_disabled").(bool),
144139
}
145140

146141
params := service_accounts.NewUpdateServiceAccountParams().

internal/resources/grafana/resource_service_account_test.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
2626
CheckDestroy: serviceAccountCheckExists.destroyed(&updatedSA, nil),
2727
Steps: []resource.TestStep{
2828
{
29-
Config: testServiceAccountConfig(name, "Editor"),
29+
Config: testServiceAccountConfig(name, "Editor", false),
3030
Check: resource.ComposeTestCheckFunc(
3131
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
3232
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
@@ -38,7 +38,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
3838
},
3939
// Change the name. Check that the ID stays the same.
4040
{
41-
Config: testServiceAccountConfig(name+"-updated", "Editor"),
41+
Config: testServiceAccountConfig(name+"-updated", "Editor", false),
4242
Check: resource.ComposeTestCheckFunc(
4343
serviceAccountCheckExists.exists("grafana_service_account.test", &updatedSA),
4444
func(s *terraform.State) error {
@@ -54,6 +54,12 @@ func TestAccServiceAccount_basic(t *testing.T) {
5454
resource.TestMatchResourceAttr("grafana_service_account.test", "id", defaultOrgIDRegexp),
5555
),
5656
},
57+
// Import test
58+
{
59+
ResourceName: "grafana_service_account.test",
60+
ImportState: true,
61+
ImportStateVerify: true,
62+
},
5763
},
5864
})
5965
}
@@ -69,7 +75,7 @@ func TestAccServiceAccount_NoneRole(t *testing.T) {
6975
CheckDestroy: serviceAccountCheckExists.destroyed(&sa, nil),
7076
Steps: []resource.TestStep{
7177
{
72-
Config: testServiceAccountConfig(name, "None"),
78+
Config: testServiceAccountConfig(name, "None", false),
7379
Check: resource.ComposeTestCheckFunc(
7480
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
7581
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
@@ -79,6 +85,24 @@ func TestAccServiceAccount_NoneRole(t *testing.T) {
7985
resource.TestMatchResourceAttr("grafana_service_account.test", "id", defaultOrgIDRegexp),
8086
),
8187
},
88+
// Disable the SA
89+
{
90+
Config: testServiceAccountConfig(name, "None", true),
91+
Check: resource.ComposeTestCheckFunc(
92+
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
93+
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
94+
resource.TestCheckResourceAttr("grafana_service_account.test", "org_id", "1"),
95+
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "None"),
96+
resource.TestCheckResourceAttr("grafana_service_account.test", "is_disabled", "true"),
97+
resource.TestMatchResourceAttr("grafana_service_account.test", "id", defaultOrgIDRegexp),
98+
),
99+
},
100+
// Import test
101+
{
102+
ResourceName: "grafana_service_account.test",
103+
ImportState: true,
104+
ImportStateVerify: true,
105+
},
82106
},
83107
})
84108
}
@@ -121,7 +145,7 @@ func TestAccServiceAccount_invalid_role(t *testing.T) {
121145
Steps: []resource.TestStep{
122146
{
123147
ExpectError: regexp.MustCompile(`.*expected role to be one of \[.+\], got InvalidRole`),
124-
Config: testServiceAccountConfig("any", "InvalidRole"),
148+
Config: testServiceAccountConfig("any", "InvalidRole", false),
125149
},
126150
},
127151
})
@@ -143,11 +167,11 @@ func testManyServiceAccountsConfig(prefix string, count int) string {
143167
return config
144168
}
145169

146-
func testServiceAccountConfig(name, role string) string {
170+
func testServiceAccountConfig(name, role string, disabled bool) string {
147171
return fmt.Sprintf(`
148172
resource "grafana_service_account" "test" {
149173
name = "%[1]s"
150174
role = "%[2]s"
151-
is_disabled = false
152-
}`, name, role)
175+
is_disabled = %[3]t
176+
}`, name, role, disabled)
153177
}

0 commit comments

Comments
 (0)