Skip to content

Commit 2fa3cef

Browse files
Remove cloud_stack_slug attribute from API key (#965)
This reverts the feature where `grafana_api_key` was used for both OSS and cloud purposes `grafana_cloud_stack_api_key` now serves this purpose
1 parent c966cb0 commit 2fa3cef

File tree

3 files changed

+8
-54
lines changed

3 files changed

+8
-54
lines changed

docs/resources/api_key.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ output "api_key_bar" {
5151

5252
### Optional
5353

54-
- `cloud_stack_slug` (String, Deprecated) Deprecated: Use `grafana_cloud_stack_service_account` and `grafana_cloud_stack_service_account_token` resources instead
5554
- `org_id` (String) The Organization ID. If not set, the Org ID defined in the provider block will be used.
5655
- `seconds_to_live` (Number)
5756

internal/provider/provider.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Provider(version string) func() *schema.Provider {
4747
grafanaClientResources = addResourcesMetadataValidation(grafanaClientPresent, map[string]*schema.Resource{
4848
// Grafana
4949
"grafana_annotation": grafana.ResourceAnnotation(),
50+
"grafana_api_key": grafana.ResourceAPIKey(),
5051
"grafana_contact_point": grafana.ResourceContactPoint(),
5152
"grafana_dashboard": grafana.ResourceDashboard(),
5253
"grafana_dashboard_permission": grafana.ResourceDashboardPermission(),
@@ -278,9 +279,6 @@ func Provider(version string) func() *schema.Provider {
278279

279280
ResourcesMap: mergeResourceMaps(
280281
map[string]*schema.Resource{
281-
// Special case, this resource supports both Grafana and Cloud (depending on context)
282-
// TODO: Move back to Grafana resources once the `cloud_stack_slug` attribute is removed
283-
"grafana_api_key": grafana.ResourceAPIKey(),
284282
// This one installs SM on a cloud instance, everything it needs is in its attributes
285283
"grafana_synthetic_monitoring_installation": cloud.ResourceInstallation(),
286284
},

internal/resources/grafana/resource_api_key.go

Lines changed: 7 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ package grafana
33
import (
44
"context"
55
"strconv"
6-
"time"
76

87
gapi "github.com/grafana/grafana-api-golang-client"
9-
"github.com/grafana/terraform-provider-grafana/internal/common"
108
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
119
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1210
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -45,14 +43,6 @@ Manages Grafana API Keys.
4543
Optional: true,
4644
ForceNew: true,
4745
},
48-
"cloud_stack_slug": {
49-
Type: schema.TypeString,
50-
Optional: true,
51-
ForceNew: true,
52-
Description: "Deprecated: Use `grafana_cloud_stack_service_account` and `grafana_cloud_stack_service_account_token` resources instead",
53-
Deprecated: "Use `grafana_cloud_stack_service_account` and `grafana_cloud_stack_service_account_token` resources instead",
54-
},
55-
5646
"id": {
5747
Type: schema.TypeString,
5848
Computed: true,
@@ -71,17 +61,13 @@ Manages Grafana API Keys.
7161
}
7262

7363
func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
74-
name := d.Get("name").(string)
75-
role := d.Get("role").(string)
76-
ttl := d.Get("seconds_to_live").(int)
64+
c, orgID := ClientFromNewOrgResource(m, d)
7765

78-
c, orgID, cleanup, err := getClientForAPIKeyManagement(d, m)
79-
if err != nil {
80-
return diag.FromErr(err)
66+
request := gapi.CreateAPIKeyRequest{
67+
Name: d.Get("name").(string),
68+
Role: d.Get("role").(string),
69+
SecondsToLive: int64(d.Get("seconds_to_live").(int)),
8170
}
82-
defer cleanup()
83-
84-
request := gapi.CreateAPIKeyRequest{Name: name, Role: role, SecondsToLive: int64(ttl)}
8571
response, err := c.CreateAPIKey(request)
8672
if err != nil {
8773
return diag.FromErr(err)
@@ -95,18 +81,13 @@ func resourceAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m interfa
9581
}
9682

9783
func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
98-
c, _, cleanup, err := getClientForAPIKeyManagement(d, m)
99-
if err != nil {
100-
return diag.FromErr(err)
101-
}
102-
defer cleanup()
84+
c, _, idStr := ClientFromExistingOrgResource(m, d.Id())
10385

10486
response, err := c.GetAPIKeys(true)
10587
if err != nil {
10688
return diag.FromErr(err)
10789
}
10890

109-
_, idStr := SplitOrgResourceID(d.Id())
11091
id, err := strconv.ParseInt(idStr, 10, 64)
11192
if err != nil {
11293
return diag.FromErr(err)
@@ -131,40 +112,16 @@ func resourceAPIKeyRead(ctx context.Context, d *schema.ResourceData, m interface
131112
}
132113

133114
func resourceAPIKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
134-
_, idStr := SplitOrgResourceID(d.Id())
115+
c, _, idStr := ClientFromExistingOrgResource(m, d.Id())
135116
id, err := strconv.ParseInt(idStr, 10, 32)
136117
if err != nil {
137118
return diag.FromErr(err)
138119
}
139120

140-
c, _, cleanup, err := getClientForAPIKeyManagement(d, m)
141-
if err != nil {
142-
return diag.FromErr(err)
143-
}
144-
defer cleanup()
145-
146121
_, err = c.DeleteAPIKey(id)
147122
if err != nil {
148123
return diag.FromErr(err)
149124
}
150125

151126
return nil
152127
}
153-
154-
func getClientForAPIKeyManagement(d *schema.ResourceData, m interface{}) (c *gapi.Client, orgID int64, cleanup func() error, err error) {
155-
// TODO: Remove this client management once `cloud_stack_slug` is removed
156-
if cloudStackSlug, ok := d.GetOk("cloud_stack_slug"); ok && cloudStackSlug.(string) != "" {
157-
cloudClient := m.(*common.Client).GrafanaCloudAPI
158-
c, cleanup, err = cloudClient.CreateTemporaryStackGrafanaClient(cloudStackSlug.(string), "terraform-temp-", 60*time.Second)
159-
return
160-
}
161-
162-
cleanup = func() error { return nil }
163-
if d.Id() != "" {
164-
c, orgID, _ = ClientFromExistingOrgResource(m, d.Id())
165-
} else {
166-
c, orgID = ClientFromNewOrgResource(m, d)
167-
}
168-
169-
return
170-
}

0 commit comments

Comments
 (0)