From a0b2f87f2c5efa67a0dc58f61e4478093db92c4b Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Mon, 18 Mar 2024 11:45:56 -0400 Subject: [PATCH] Fix `grafana_cloud_service_account` read (#1424) * 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 * Fix SM http check test that now randomly fails Probably new validation in the API --- docs/resources/synthetic_monitoring_check.md | 4 ++-- .../http_complex.tf | 4 ++-- .../resource_cloud_stack_service_account.go | 24 +++++++++++++++---- ...source_cloud_stack_service_account_test.go | 5 ++++ .../resource_check_test.go | 4 ++-- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/docs/resources/synthetic_monitoring_check.md b/docs/resources/synthetic_monitoring_check.md index f10fbe54d..2916b03f1 100644 --- a/docs/resources/synthetic_monitoring_check.md +++ b/docs/resources/synthetic_monitoring_check.md @@ -203,11 +203,11 @@ EOS ] fail_if_body_matches_regexp = [ - "*bad stuff*", + ".*bad stuff.*", ] fail_if_body_not_matches_regexp = [ - "*good stuff*", + ".*good stuff.*", ] fail_if_header_matches_regexp { diff --git a/examples/resources/grafana_synthetic_monitoring_check/http_complex.tf b/examples/resources/grafana_synthetic_monitoring_check/http_complex.tf index a531a2e43..f1026e76e 100644 --- a/examples/resources/grafana_synthetic_monitoring_check/http_complex.tf +++ b/examples/resources/grafana_synthetic_monitoring_check/http_complex.tf @@ -77,11 +77,11 @@ EOS ] fail_if_body_matches_regexp = [ - "*bad stuff*", + ".*bad stuff.*", ] fail_if_body_not_matches_regexp = [ - "*good stuff*", + ".*good stuff.*", ] fail_if_header_matches_regexp { 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{}), diff --git a/internal/resources/syntheticmonitoring/resource_check_test.go b/internal/resources/syntheticmonitoring/resource_check_test.go index e6e79a6bf..99446fca2 100644 --- a/internal/resources/syntheticmonitoring/resource_check_test.go +++ b/internal/resources/syntheticmonitoring/resource_check_test.go @@ -126,8 +126,8 @@ func TestAccResourceCheck_http(t *testing.T) { resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.0", "HTTP/1.0"), resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.1", "HTTP/1.1"), resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.valid_http_versions.2", "HTTP/2.0"), - resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_matches_regexp.0", "*bad stuff*"), - resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_not_matches_regexp.0", "*good stuff*"), + resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_matches_regexp.0", ".*bad stuff.*"), + resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_body_not_matches_regexp.0", ".*good stuff.*"), resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.header", "Content-Type"), resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.regexp", "application/soap*"), resource.TestCheckResourceAttr("grafana_synthetic_monitoring_check.http", "settings.0.http.0.fail_if_header_matches_regexp.0.allow_missing", "true"),