Skip to content

Commit 759068c

Browse files
V2: grafana_cloud_api_key ID compatibility from v2.12 to v2.19 (#1731)
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
1 parent 370bf6b commit 759068c

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

Diff for: internal/resources/cloud/resource_cloud_api_key.go

+20-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cloud
33
import (
44
"context"
55
"fmt"
6+
"strings"
67

78
"github.com/grafana/grafana-com-public-clients/go/gcom"
89
"github.com/grafana/terraform-provider-grafana/v2/internal/common"
@@ -115,13 +116,12 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, c *gcom.A
115116
}
116117

117118
func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, c *gcom.APIClient) diag.Diagnostics {
118-
split, err := resourceAPIKeyID.Split(d.Id())
119+
org, name, err := resourceAPIKeySplitID(d.Id())
119120
if err != nil {
120121
return diag.FromErr(err)
121122
}
122-
org, name := split[0], split[1]
123123

124-
resp, _, err := c.OrgsAPI.GetApiKey(ctx, name.(string), org.(string)).Execute()
124+
resp, _, err := c.OrgsAPI.GetApiKey(ctx, name, org).Execute()
125125
if err != nil {
126126
return apiError(err)
127127
}
@@ -135,13 +135,27 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, c *gcom.API
135135
}
136136

137137
func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, c *gcom.APIClient) diag.Diagnostics {
138-
split, err := resourceAPIKeyID.Split(d.Id())
138+
org, name, err := resourceAPIKeySplitID(d.Id())
139139
if err != nil {
140140
return diag.FromErr(err)
141141
}
142-
org, name := split[0], split[1]
143142

144-
_, err = c.OrgsAPI.DelApiKey(ctx, name.(string), org.(string)).XRequestId(ClientRequestID()).Execute()
143+
_, err = c.OrgsAPI.DelApiKey(ctx, name, org).XRequestId(ClientRequestID()).Execute()
145144
d.SetId("")
146145
return apiError(err)
147146
}
147+
148+
func resourceAPIKeySplitID(id string) (string, string, error) {
149+
var org, name string
150+
if strings.Contains(id, common.ResourceIDSeparator) {
151+
split, err := resourceAPIKeyID.Split(id)
152+
if err != nil {
153+
return "", "", err
154+
}
155+
org, name = split[0].(string), split[1].(string)
156+
} else {
157+
splitID := strings.SplitN(id, "-", 2)
158+
org, name = splitID[0], splitID[1]
159+
}
160+
return org, name, nil
161+
}

Diff for: internal/resources/cloud/resource_cloud_api_key_test.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ func TestAccCloudApiKey_Basic(t *testing.T) {
5555
ImportStateVerify: true,
5656
ImportStateVerifyIgnore: []string{"key"},
5757
},
58+
// Test import/read with the ID format from version 2.12.2 and earlier
59+
{
60+
ResourceName: "grafana_cloud_api_key.test",
61+
ImportState: true,
62+
ImportStateVerify: true,
63+
ImportStateVerifyIgnore: []string{"key"},
64+
ImportStateId: fmt.Sprintf("%s-%s", os.Getenv("GRAFANA_CLOUD_ORG"), resourceName),
65+
},
5866
},
5967
})
6068
})
@@ -99,7 +107,7 @@ func testAccDeleteExistingCloudAPIKeys(t *testing.T, prefix string) {
99107

100108
for _, key := range resp.Items {
101109
if strings.HasPrefix(key.Name, prefix) {
102-
_, err := client.OrgsAPI.DelApiKey(context.Background(), org, key.Name).XRequestId(cloud.ClientRequestID()).Execute()
110+
_, err := client.OrgsAPI.DelApiKey(context.Background(), key.Name, org).XRequestId(cloud.ClientRequestID()).Execute()
103111
if err != nil {
104112
t.Error(err)
105113
}

0 commit comments

Comments
 (0)