Skip to content

Fix service_account_id reference in stack_service_account_token resource #1431

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

Merged
merged 3 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
testAccGrafanaAuthCheckServiceAccounts(&stack, []string{"management-sa"}),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "name", "management-sa"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "role", "Admin"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "true"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "false"),
resource.TestCheckResourceAttr("grafana_cloud_stack_service_account_token.management_token", "name", "management-sa-token"),
resource.TestCheckNoResourceAttr("grafana_cloud_stack_service_account_token.management_token", "expiration"),
resource.TestCheckResourceAttrSet("grafana_cloud_stack_service_account_token.management_token", "key"),
Expand All @@ -88,7 +88,7 @@ func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
Steps: []resource.TestStep{
// Apply with 2.13.0 provider
{
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
Config: testAccGrafanaServiceAccountWithStackFolder(slug, slug, true),
ExternalProviders: map[string]resource.ExternalProvider{
"grafana": {
VersionConstraint: "=2.13.0",
Expand All @@ -99,10 +99,15 @@ func TestAccGrafanaServiceAccountFromCloud_MigrateFrom213(t *testing.T) {
},
// Apply with latest provider
{
Config: testAccGrafanaServiceAccountFromCloud(slug, slug, true),
Config: testAccGrafanaServiceAccountWithStackFolder(slug, slug, true),
Check: check,
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
},
// Destroy the folder
{
Config: testAccGrafanaServiceAccountWithStackFolder(slug, slug, false),
ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories,
},
},
})
}
Expand All @@ -124,6 +129,22 @@ func testAccGrafanaServiceAccountFromCloud(name, slug string, disabled bool) str
`, disabled)
}

func testAccGrafanaServiceAccountWithStackFolder(name, slug string, withFolder bool) string {
return testAccGrafanaServiceAccountFromCloud(name, slug, false) + fmt.Sprintf(`
provider "grafana" {
alias = "stack"
auth = grafana_cloud_stack_service_account_token.management_token.key
url = grafana_cloud_stack.test.url
}

resource "grafana_folder" "test" {
count = %t ? 1 : 0
provider = grafana.stack
title = "test"
}
`, withFolder)
}

func testAccGrafanaAuthCheckServiceAccounts(stack *gcom.FormattedApiInstance, expectedSAs []string) resource.TestCheckFunc {
return func(s *terraform.State) error {
cloudClient := testutils.Provider.Meta().(*common.Client).GrafanaCloudAPI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Required access policy scopes:
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
// The service account ID is now possibly a composite ID that includes the stack slug
oldID, _ := getStackServiceAccountID(old)
newID, _ := getStackServiceAccountID(new)
return oldID == newID && oldID != 0 && newID != 0
},
},
"seconds_to_live": {
Type: schema.TypeInt,
Expand Down
20 changes: 19 additions & 1 deletion internal/resources/grafana/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,33 @@ func grafanaOrgIDResourceValidation(d *schema.ResourceData, m interface{}) error

func addValidation(resources map[string]*schema.Resource) map[string]*schema.Resource {
for _, r := range resources {
readFn := r.ReadContext
createFn := r.CreateContext
readFn := r.ReadContext
updateFn := r.UpdateContext
deleteFn := r.DeleteContext

r.ReadContext = func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if err := grafanaClientResourceValidation(d, m); err != nil {
return diag.FromErr(err)
}
return readFn(ctx, d, m)
}
if updateFn != nil {
r.UpdateContext = func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if err := grafanaClientResourceValidation(d, m); err != nil {
return diag.FromErr(err)
}
return updateFn(ctx, d, m)
}
}
if deleteFn != nil {
r.DeleteContext = func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if err := grafanaClientResourceValidation(d, m); err != nil {
return diag.FromErr(err)
}
return deleteFn(ctx, d, m)
}
}
if createFn != nil {
r.CreateContext = func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
if err := grafanaClientResourceValidation(d, m); err != nil {
Expand Down