@@ -3,10 +3,8 @@ package grafana
33import (
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
7363func 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
9783func 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
133114func 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