Skip to content

Commit 5d13161

Browse files
Cloud Stacks: Better "wait for readiness" (#1545)
1 parent 46a8ac8 commit 5d13161

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

internal/resources/cloud/resource_cloud_stack.go

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ func createStack(ctx context.Context, d *schema.ResourceData, client *gcom.APICl
263263
return diag
264264
}
265265

266-
return waitForStackReadiness(ctx, d)
266+
if d.Get("wait_for_readiness").(bool) {
267+
timeout := defaultReadinessTimeout
268+
if timeoutVal := d.Get("wait_for_readiness_timeout").(string); timeoutVal != "" {
269+
timeout, _ = time.ParseDuration(timeoutVal)
270+
}
271+
return waitForStackReadiness(ctx, timeout, d.Get("url").(string))
272+
}
273+
return nil
267274
}
268275

269276
func updateStack(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics {
@@ -295,7 +302,14 @@ func updateStack(ctx context.Context, d *schema.ResourceData, client *gcom.APICl
295302
return diag
296303
}
297304

298-
return waitForStackReadiness(ctx, d)
305+
if d.Get("wait_for_readiness").(bool) {
306+
timeout := defaultReadinessTimeout
307+
if timeoutVal := d.Get("wait_for_readiness_timeout").(string); timeoutVal != "" {
308+
timeout, _ = time.ParseDuration(timeoutVal)
309+
}
310+
return waitForStackReadiness(ctx, timeout, d.Get("url").(string))
311+
}
312+
return nil
299313
}
300314

301315
func deleteStack(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics {
@@ -422,17 +436,13 @@ func appendPath(baseURL, path string) (string, error) {
422436
}
423437

424438
// waitForStackReadiness retries until the stack is ready, verified by querying the Grafana URL
425-
func waitForStackReadiness(ctx context.Context, d *schema.ResourceData) diag.Diagnostics {
426-
if wait := d.Get("wait_for_readiness").(bool); !wait {
427-
return nil
428-
}
429-
430-
timeout := defaultReadinessTimeout
431-
if timeoutVal := d.Get("wait_for_readiness_timeout").(string); timeoutVal != "" {
432-
timeout, _ = time.ParseDuration(timeoutVal)
439+
func waitForStackReadiness(ctx context.Context, timeout time.Duration, stackURL string) diag.Diagnostics {
440+
healthURL, joinErr := url.JoinPath(stackURL, "api", "health")
441+
if joinErr != nil {
442+
return diag.FromErr(joinErr)
433443
}
434444
err := retry.RetryContext(ctx, timeout, func() *retry.RetryError {
435-
req, err := http.NewRequestWithContext(ctx, http.MethodHead, d.Get("url").(string), nil)
445+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil)
436446
if err != nil {
437447
return retry.NonRetryableError(err)
438448
}
@@ -456,12 +466,21 @@ func waitForStackReadiness(ctx context.Context, d *schema.ResourceData) diag.Dia
456466
return nil
457467
})
458468
if err != nil {
459-
return diag.Errorf("error waiting for stack to be ready: %v", err)
469+
return diag.Errorf("error waiting for stack (URL: %s) to be ready: %v", healthURL, err)
460470
}
461471

462472
return nil
463473
}
464474

475+
func waitForStackReadinessFromSlug(ctx context.Context, timeout time.Duration, slug string, client *gcom.APIClient) diag.Diagnostics {
476+
stack, _, err := client.InstancesAPI.GetInstance(ctx, slug).Execute()
477+
if err != nil {
478+
return apiError(err)
479+
}
480+
481+
return waitForStackReadiness(ctx, timeout, stack.Url)
482+
}
483+
465484
func defaultStackURL(slug string) string {
466485
return fmt.Sprintf("https://%s.grafana.net", slug)
467486
}

internal/resources/cloud/resource_cloud_stack_service_account.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ Required access policy scopes:
8181
}
8282

8383
func createStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
84+
if err := waitForStackReadinessFromSlug(ctx, 1*time.Minute, d.Get("stack_slug").(string), cloudClient); err != nil {
85+
return err
86+
}
87+
8488
stackSlug := d.Get("stack_slug").(string)
8589
req := gcom.PostInstanceServiceAccountsRequest{
8690
Name: d.Get("name").(string),
@@ -116,6 +120,10 @@ func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudC
116120
stackSlug, serviceAccountID = split[0].(string), split[1].(int64)
117121
}
118122

123+
if err := waitForStackReadinessFromSlug(ctx, 1*time.Minute, stackSlug, cloudClient); err != nil {
124+
return err
125+
}
126+
119127
resp, httpResp, err := cloudClient.InstancesAPI.GetInstanceServiceAccount(ctx, stackSlug, strconv.FormatInt(serviceAccountID, 10)).Execute()
120128
if httpResp != nil && httpResp.StatusCode == 404 {
121129
d.SetId("")
@@ -135,6 +143,10 @@ func readStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudC
135143
}
136144

137145
func deleteStackServiceAccount(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
146+
if err := waitForStackReadinessFromSlug(ctx, 1*time.Minute, d.Get("stack_slug").(string), cloudClient); err != nil {
147+
return err
148+
}
149+
138150
split, err := resourceStackServiceAccountID.Split(d.Id())
139151
if err != nil {
140152
return diag.FromErr(err)

internal/resources/cloud/resource_cloud_stack_service_account_token.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"strconv"
8+
"time"
89

910
"github.com/grafana/grafana-com-public-clients/go/gcom"
1011
"github.com/grafana/terraform-provider-grafana/v2/internal/common"
@@ -81,6 +82,10 @@ Required access policy scopes:
8182
}
8283

8384
func stackServiceAccountTokenCreate(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
85+
if err := waitForStackReadinessFromSlug(ctx, 1*time.Minute, d.Get("stack_slug").(string), cloudClient); err != nil {
86+
return err
87+
}
88+
8489
stackSlug := d.Get("stack_slug").(string)
8590
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
8691
if err != nil {
@@ -117,6 +122,10 @@ func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, c
117122
return diag.FromErr(err)
118123
}
119124

125+
if err := waitForStackReadinessFromSlug(ctx, 1*time.Minute, stackSlug, cloudClient); err != nil {
126+
return err
127+
}
128+
120129
response, _, err := cloudClient.InstancesAPI.GetInstanceServiceAccountTokens(ctx, stackSlug, strconv.FormatInt(serviceAccountID, 10)).Execute()
121130
if err != nil {
122131
return diag.FromErr(err)
@@ -152,6 +161,10 @@ func stackServiceAccountTokenRead(ctx context.Context, d *schema.ResourceData, c
152161
}
153162

154163
func stackServiceAccountTokenDelete(ctx context.Context, d *schema.ResourceData, cloudClient *gcom.APIClient) diag.Diagnostics {
164+
if err := waitForStackReadinessFromSlug(ctx, 1*time.Minute, d.Get("stack_slug").(string), cloudClient); err != nil {
165+
return err
166+
}
167+
155168
stackSlug := d.Get("stack_slug").(string)
156169
serviceAccountID, err := getStackServiceAccountID(d.Get("service_account_id").(string))
157170
if err != nil {

0 commit comments

Comments
 (0)