From 8e9661e83ddd8a3304c5dab0e6b48f5712f07b7b Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Mon, 29 Jul 2024 10:00:19 -0400 Subject: [PATCH] Cloud: Fix stack wake-up function Closes https://github.com/grafana/terraform-provider-grafana/issues/1725 In https://github.com/grafana/terraform-provider-grafana/pull/1545, the stack readiness check was improved by calling the `/api/health` endpoint instead of just the stack URL While this does provide a better indication that the instance is up and running, the `/api/health` subpath does not trigger Grafana Cloud's stack wake-up With this PR, we'll call both the stack's base URL and its health endpoint --- .../resources/cloud/resource_cloud_stack.go | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/internal/resources/cloud/resource_cloud_stack.go b/internal/resources/cloud/resource_cloud_stack.go index e188ee7d2..6ee216591 100644 --- a/internal/resources/cloud/resource_cloud_stack.go +++ b/internal/resources/cloud/resource_cloud_stack.go @@ -445,25 +445,37 @@ func waitForStackReadiness(ctx context.Context, timeout time.Duration, stackURL return diag.FromErr(joinErr) } err := retry.RetryContext(ctx, timeout, func() *retry.RetryError { - req, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil) + // Query the instance URL directly. This makes the stack wake-up if it has been paused. + // The health endpoint is helpful to check that the stack is ready, but it doesn't wake up the stack. + stackReq, err := http.NewRequestWithContext(ctx, http.MethodGet, stackURL, nil) if err != nil { return retry.NonRetryableError(err) } - resp, err := http.DefaultClient.Do(req) + stackResp, err := http.DefaultClient.Do(stackReq) if err != nil { return retry.RetryableError(err) } - defer resp.Body.Close() - if resp.StatusCode != 200 { + defer stackResp.Body.Close() + + healthReq, err := http.NewRequestWithContext(ctx, http.MethodGet, healthURL, nil) + if err != nil { + return retry.NonRetryableError(err) + } + healthResp, err := http.DefaultClient.Do(healthReq) + if err != nil { + return retry.RetryableError(err) + } + defer healthResp.Body.Close() + if healthResp.StatusCode != 200 { buf := new(bytes.Buffer) body := "" - _, err = buf.ReadFrom(resp.Body) + _, err = buf.ReadFrom(healthResp.Body) if err != nil { body = "unable to read response body, error: " + err.Error() } else { body = buf.String() } - return retry.RetryableError(fmt.Errorf("stack was not ready in %s. Status code: %d, Body: %s", timeout, resp.StatusCode, body)) + return retry.RetryableError(fmt.Errorf("stack was not ready in %s. Status code: %d, Body: %s", timeout, healthResp.StatusCode, body)) } return nil