Skip to content

Commit

Permalink
V2: grafana_cloud_api_key ID compatibility from v2.12 to v2.19 (#1731)
Browse files Browse the repository at this point in the history
Closes #1730
Even though API keys are deprecated, this allows users to upgrade from v2.12 to v2.19, in order to get other fixes
  • Loading branch information
julienduchesne committed Jul 30, 2024
1 parent 370bf6b commit 759068c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
26 changes: 20 additions & 6 deletions internal/resources/cloud/resource_cloud_api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
10 changes: 9 additions & 1 deletion internal/resources/cloud/resource_cloud_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
})
})
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 759068c

Please sign in to comment.