@@ -3,11 +3,13 @@ package cloud
33import (
44 "context"
55 "strconv"
6+ "strings"
67 "time"
78
89 gapi "github.com/grafana/grafana-api-golang-client"
910 "github.com/grafana/terraform-provider-grafana/internal/common"
1011 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1113 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1214 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1315)
@@ -77,14 +79,25 @@ func resourceStackAPIKeyCreate(ctx context.Context, d *schema.ResourceData, m in
7779 defer cleanup ()
7880
7981 request := gapi.CreateAPIKeyRequest {Name : name , Role : role , SecondsToLive : int64 (ttl )}
80- response , err := c .CreateAPIKey (request )
82+ err = resource .RetryContext (ctx , 2 * time .Minute , func () * resource.RetryError {
83+ response , err := c .CreateAPIKey (request )
84+
85+ if err != nil {
86+ if strings .Contains (err .Error (), "Your instance is loading, and will be ready shortly." ) {
87+ return resource .RetryableError (err )
88+ }
89+ return resource .NonRetryableError (err )
90+ }
91+
92+ d .SetId (strconv .FormatInt (response .ID , 10 ))
93+ d .Set ("key" , response .Key )
94+ return nil
95+ })
96+
8197 if err != nil {
8298 return diag .FromErr (err )
8399 }
84100
85- d .SetId (strconv .FormatInt (response .ID , 10 ))
86- d .Set ("key" , response .Key )
87-
88101 // Fill the true resource's state after a create by performing a read
89102 return resourceStackAPIKeyRead (ctx , d , m )
90103}
@@ -147,5 +160,5 @@ func resourceStackAPIKeyDelete(ctx context.Context, d *schema.ResourceData, m in
147160
148161func getClientForAPIKeyManagement (d * schema.ResourceData , m interface {}) (c * gapi.Client , cleanup func () error , err error ) {
149162 cloudClient := m .(* common.Client ).GrafanaCloudAPI
150- return cloudClient .CreateTemporaryStackGrafanaClient (d .Get ("stack-slug " ).(string ), "terraform-temp-" , 60 * time .Second )
163+ return cloudClient .CreateTemporaryStackGrafanaClient (d .Get ("stack_slug " ).(string ), "terraform-temp-" , 60 * time .Second )
151164}
0 commit comments