Skip to content

Commit

Permalink
grafana_service_account: Fix updates on v11 (#1503)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
julienduchesne authored Apr 19, 2024
1 parent b888796 commit bd9bdad
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
13 changes: 4 additions & 9 deletions internal/resources/grafana/resource_service_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,10 @@ func UpdateServiceAccount(ctx context.Context, d *schema.ResourceData, meta inte
return diag.FromErr(err)
}

updateRequest := models.UpdateServiceAccountForm{}
if d.HasChange("name") {
updateRequest.Name = d.Get("name").(string)
}
if d.HasChange("role") {
updateRequest.Role = d.Get("role").(string)
}
if d.HasChange("is_disabled") {
updateRequest.IsDisabled = d.Get("is_disabled").(bool)
updateRequest := models.UpdateServiceAccountForm{
Name: d.Get("name").(string),
Role: d.Get("role").(string),
IsDisabled: d.Get("is_disabled").(bool),
}

params := service_accounts.NewUpdateServiceAccountParams().
Expand Down
38 changes: 31 additions & 7 deletions internal/resources/grafana/resource_service_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
CheckDestroy: serviceAccountCheckExists.destroyed(&updatedSA, nil),
Steps: []resource.TestStep{
{
Config: testServiceAccountConfig(name, "Editor"),
Config: testServiceAccountConfig(name, "Editor", false),
Check: resource.ComposeTestCheckFunc(
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
Expand All @@ -38,7 +38,7 @@ func TestAccServiceAccount_basic(t *testing.T) {
},
// Change the name. Check that the ID stays the same.
{
Config: testServiceAccountConfig(name+"-updated", "Editor"),
Config: testServiceAccountConfig(name+"-updated", "Editor", false),
Check: resource.ComposeTestCheckFunc(
serviceAccountCheckExists.exists("grafana_service_account.test", &updatedSA),
func(s *terraform.State) error {
Expand All @@ -54,6 +54,12 @@ func TestAccServiceAccount_basic(t *testing.T) {
resource.TestMatchResourceAttr("grafana_service_account.test", "id", defaultOrgIDRegexp),
),
},
// Import test
{
ResourceName: "grafana_service_account.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand All @@ -69,7 +75,7 @@ func TestAccServiceAccount_NoneRole(t *testing.T) {
CheckDestroy: serviceAccountCheckExists.destroyed(&sa, nil),
Steps: []resource.TestStep{
{
Config: testServiceAccountConfig(name, "None"),
Config: testServiceAccountConfig(name, "None", false),
Check: resource.ComposeTestCheckFunc(
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
Expand All @@ -79,6 +85,24 @@ func TestAccServiceAccount_NoneRole(t *testing.T) {
resource.TestMatchResourceAttr("grafana_service_account.test", "id", defaultOrgIDRegexp),
),
},
// Disable the SA
{
Config: testServiceAccountConfig(name, "None", true),
Check: resource.ComposeTestCheckFunc(
serviceAccountCheckExists.exists("grafana_service_account.test", &sa),
resource.TestCheckResourceAttr("grafana_service_account.test", "name", name),
resource.TestCheckResourceAttr("grafana_service_account.test", "org_id", "1"),
resource.TestCheckResourceAttr("grafana_service_account.test", "role", "None"),
resource.TestCheckResourceAttr("grafana_service_account.test", "is_disabled", "true"),
resource.TestMatchResourceAttr("grafana_service_account.test", "id", defaultOrgIDRegexp),
),
},
// Import test
{
ResourceName: "grafana_service_account.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -121,7 +145,7 @@ func TestAccServiceAccount_invalid_role(t *testing.T) {
Steps: []resource.TestStep{
{
ExpectError: regexp.MustCompile(`.*expected role to be one of \[.+\], got InvalidRole`),
Config: testServiceAccountConfig("any", "InvalidRole"),
Config: testServiceAccountConfig("any", "InvalidRole", false),
},
},
})
Expand All @@ -143,11 +167,11 @@ func testManyServiceAccountsConfig(prefix string, count int) string {
return config
}

func testServiceAccountConfig(name, role string) string {
func testServiceAccountConfig(name, role string, disabled bool) string {
return fmt.Sprintf(`
resource "grafana_service_account" "test" {
name = "%[1]s"
role = "%[2]s"
is_disabled = false
}`, name, role)
is_disabled = %[3]t
}`, name, role, disabled)
}

0 comments on commit bd9bdad

Please sign in to comment.