From 1716fe837d5ed48b007095d5ad0cab632d4c1f00 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 1 Mar 2024 16:27:42 -0500 Subject: [PATCH] Further fixes to test --- ...source_cloud_stack_service_account_test.go | 21 +++++-------------- ...ource_cloud_stack_service_account_token.go | 9 +++++--- 2 files changed, 11 insertions(+), 19 deletions(-) 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 49e9a5fdf..4849e01c9 100644 --- a/internal/resources/cloud/resource_cloud_stack_service_account_test.go +++ b/internal/resources/cloud/resource_cloud_stack_service_account_test.go @@ -3,7 +3,6 @@ package cloud_test import ( "context" "fmt" - "strings" "testing" "github.com/grafana/grafana-com-public-clients/go/gcom" @@ -87,24 +86,14 @@ func testAccGrafanaAuthCheckServiceAccounts(stack *gcom.FormattedApiInstance, ex return fmt.Errorf("failed to get service accounts: %w", err) } - var foundSAs []string - for _, sa := range response.Payload.ServiceAccounts { - if !strings.HasPrefix(sa.Name, "test-api-key-") { - foundSAs = append(foundSAs, sa.Name) - if sa.Tokens == 0 { - return fmt.Errorf("expected to find at least one token for service account %s", sa.Name) - } - } - } - - if len(foundSAs) != len(expectedSAs) { - return fmt.Errorf("expected %d keys, got %d", len(expectedSAs), len(foundSAs)) - } for _, expectedSA := range expectedSAs { found := false - for _, foundSA := range foundSAs { - if expectedSA == foundSA { + for _, sa := range response.Payload.ServiceAccounts { + if sa.Name == expectedSA { found = true + if sa.Tokens == 0 { + return fmt.Errorf("expected to find at least one token for service account %s", sa.Name) + } break } } diff --git a/internal/resources/cloud/resource_cloud_stack_service_account_token.go b/internal/resources/cloud/resource_cloud_stack_service_account_token.go index c958adda9..690e68e00 100644 --- a/internal/resources/cloud/resource_cloud_stack_service_account_token.go +++ b/internal/resources/cloud/resource_cloud_stack_service_account_token.go @@ -82,10 +82,11 @@ func stackServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData, } defer cleanup() - serviceAccountID, err := strconv.ParseInt(d.Get("service_account_id").(string), 10, 64) + split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string)) if err != nil { return diag.FromErr(err) } + serviceAccountID := split[1].(int64) name := d.Get("name").(string) ttl := d.Get("seconds_to_live").(int) @@ -121,10 +122,11 @@ func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, c } func stackServiceAccountTokenReadWithClient(c *goapi.GrafanaHTTPAPI, d *schema.ResourceData) diag.Diagnostics { - serviceAccountID, err := strconv.ParseInt(d.Get("service_account_id").(string), 10, 64) + split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string)) if err != nil { return diag.FromErr(err) } + serviceAccountID := split[1].(int64) response, err := c.ServiceAccounts.ListTokens(serviceAccountID) if err != nil { @@ -167,10 +169,11 @@ func stackServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData, } defer cleanup() - serviceAccountID, err := strconv.ParseInt(d.Get("service_account_id").(string), 10, 64) + split, err := resourceStackServiceAccountID.Split(d.Get("service_account_id").(string)) if err != nil { return diag.FromErr(err) } + serviceAccountID := split[1].(int64) id, err := strconv.ParseInt(d.Id(), 10, 32) if err != nil {