From 232561e2ba93eff2ab7dbcb08ae38d0441f302f5 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Mon, 18 Mar 2024 10:43:36 -0400 Subject: [PATCH] Fix `grafana_cloud_service_account` read Regression from https://github.com/grafana/terraform-provider-grafana/pull/1412 In that PR, I noticed that the ID format for the stack service account is incomplete It contained only the SA ID, and without the stack slug, it didn't really allow for imports I fixed it, but now read operations from the old ID format didn't work anymore To fix this, we can support the old ID format (just the SA ID) until the next major version This is unfortunately not testable with the acceptance test framework, but I tested it manually --- .../resource_cloud_stack_service_account.go | 24 +++++++++++++++---- ...source_cloud_stack_service_account_test.go | 5 ++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/internal/resources/cloud/resource_cloud_stack_service_account.go b/internal/resources/cloud/resource_cloud_stack_service_account.go index 5442dd3a2..202a2b0f3 100644 --- a/internal/resources/cloud/resource_cloud_stack_service_account.go +++ b/internal/resources/cloud/resource_cloud_stack_service_account.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/url" + "strconv" "time" "github.com/grafana/grafana-com-public-clients/go/gcom" @@ -100,11 +101,21 @@ func createStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou } func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics { - split, err := resourceStackServiceAccountID.Split(d.Id()) - if err != nil { - return diag.FromErr(err) + var stackSlug string + var serviceAccountID int64 + split, splitErr := resourceStackServiceAccountID.Split(d.Id()) + if splitErr != nil { + // ID used to be just the service account ID. + // Even though that's an incomplete ID for imports, we need to handle it for backwards compatibility + // TODO: Remove on next major version + stackSlug = d.Get("stack_slug").(string) + var parseErr error + if serviceAccountID, parseErr = strconv.ParseInt(d.Id(), 10, 64); parseErr != nil { + return diag.Errorf("failed to parse ID (%s) as stackSlug:serviceAccountID: %v and failed to parse as serviceAccountID: %v", d.Id(), splitErr, parseErr) + } + } else { + stackSlug, serviceAccountID = split[0].(string), split[1].(int64) } - stackSlug, serviceAccountID := split[0].(string), split[1].(int64) client, cleanup, err := CreateTemporaryStackGrafanaClient(ctx, cloudClient, stackSlug, "terraform-temp-") if err != nil { @@ -112,6 +123,9 @@ func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudC } defer cleanup() + d.Set("stack_slug", stackSlug) + d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID)) + return readStackServiceAccountWithClient(client, d, serviceAccountID) } @@ -162,6 +176,8 @@ func updateStackServiceAccount(ctx context.Context, d *schema.ResourceData, clou if _, err := client.ServiceAccounts.UpdateServiceAccount(updateRequest); err != nil { return diag.FromErr(err) } + d.Set("stack_slug", stackSlug) + d.SetId(resourceStackServiceAccountID.Make(stackSlug, serviceAccountID)) return readStackServiceAccountWithClient(client, d, serviceAccountID) } diff --git a/internal/resources/cloud/resource_cloud_stack_service_account_test.go b/internal/resources/cloud/resource_cloud_stack_service_account_test.go index 03d87a126..52dde0e18 100644 --- a/internal/resources/cloud/resource_cloud_stack_service_account_test.go +++ b/internal/resources/cloud/resource_cloud_stack_service_account_test.go @@ -47,6 +47,11 @@ func TestAccGrafanaServiceAccountFromCloud(t *testing.T) { resource.TestCheckResourceAttr("grafana_cloud_stack_service_account.management", "is_disabled", "false"), ), }, + { + ImportState: true, + ResourceName: "grafana_cloud_stack_service_account.management", + ImportStateVerify: true, + }, { Config: testAccStackConfigBasic(slug, slug, "description"), Check: testAccGrafanaAuthCheckServiceAccounts(&stack, []string{}),