diff --git a/internal/resources/cloud/resource_cloud_api_key.go b/internal/resources/cloud/resource_cloud_api_key.go index 825c2ecc0..9650345b2 100644 --- a/internal/resources/cloud/resource_cloud_api_key.go +++ b/internal/resources/cloud/resource_cloud_api_key.go @@ -3,6 +3,7 @@ package cloud import ( "context" "fmt" + "strings" "github.com/grafana/grafana-com-public-clients/go/gcom" "github.com/grafana/terraform-provider-grafana/v2/internal/common" @@ -115,13 +116,12 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, c *gcom.A } func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, c *gcom.APIClient) diag.Diagnostics { - split, err := resourceAPIKeyID.Split(d.Id()) + org, name, err := resourceAPIKeySplitID(d.Id()) if err != nil { return diag.FromErr(err) } - org, name := split[0], split[1] - resp, _, err := c.OrgsAPI.GetApiKey(ctx, name.(string), org.(string)).Execute() + resp, _, err := c.OrgsAPI.GetApiKey(ctx, name, org).Execute() if err != nil { return apiError(err) } @@ -135,13 +135,27 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, c *gcom.API } func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, c *gcom.APIClient) diag.Diagnostics { - split, err := resourceAPIKeyID.Split(d.Id()) + org, name, err := resourceAPIKeySplitID(d.Id()) if err != nil { return diag.FromErr(err) } - org, name := split[0], split[1] - _, err = c.OrgsAPI.DelApiKey(ctx, name.(string), org.(string)).XRequestId(ClientRequestID()).Execute() + _, err = c.OrgsAPI.DelApiKey(ctx, name, org).XRequestId(ClientRequestID()).Execute() d.SetId("") return apiError(err) } + +func resourceAPIKeySplitID(id string) (string, string, error) { + var org, name string + if strings.Contains(id, common.ResourceIDSeparator) { + split, err := resourceAPIKeyID.Split(id) + if err != nil { + return "", "", err + } + org, name = split[0].(string), split[1].(string) + } else { + splitID := strings.SplitN(id, "-", 2) + org, name = splitID[0], splitID[1] + } + return org, name, nil +} diff --git a/internal/resources/cloud/resource_cloud_api_key_test.go b/internal/resources/cloud/resource_cloud_api_key_test.go index 312f06a6e..6e7a55d20 100644 --- a/internal/resources/cloud/resource_cloud_api_key_test.go +++ b/internal/resources/cloud/resource_cloud_api_key_test.go @@ -55,6 +55,14 @@ func TestAccCloudApiKey_Basic(t *testing.T) { ImportStateVerify: true, ImportStateVerifyIgnore: []string{"key"}, }, + // Test import/read with the ID format from version 2.12.2 and earlier + { + ResourceName: "grafana_cloud_api_key.test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"key"}, + ImportStateId: fmt.Sprintf("%s-%s", os.Getenv("GRAFANA_CLOUD_ORG"), resourceName), + }, }, }) }) @@ -99,7 +107,7 @@ func testAccDeleteExistingCloudAPIKeys(t *testing.T, prefix string) { for _, key := range resp.Items { if strings.HasPrefix(key.Name, prefix) { - _, err := client.OrgsAPI.DelApiKey(context.Background(), org, key.Name).XRequestId(cloud.ClientRequestID()).Execute() + _, err := client.OrgsAPI.DelApiKey(context.Background(), key.Name, org).XRequestId(cloud.ClientRequestID()).Execute() if err != nil { t.Error(err) }