Skip to content

Commit e2bed2a

Browse files
Longer Cloud Stack readiness timeout (#574)
* Debug cloud stack test failures Lots of tests have been failing recently. Adding some more info to the error messages * Increase timeout to make sure it always passes * PR comments * Computed: false * Remove ValidateDiagFunc in datasources * Generate docs * Suppress timeout diff * log the wait time * Suppress diff only when the old value isn't empty * Try something different * Oops * Get rid of lint error * I hate terraform * reeeeeee * ugh
1 parent 44e020b commit e2bed2a

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

docs/resources/cloud_stack.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ available at “https://<stack_slug>.grafana.net".
3737
Changing region will destroy the existing stack and create a new one in the desired region
3838
- `url` (String) Custom URL for the Grafana instance. Must have a CNAME setup to point to `.grafana.net` before creating the stack
3939
- `wait_for_readiness` (Boolean) Whether to wait for readiness of the stack after creating it. The check is a HEAD request to the stack URL (Grafana instance). Defaults to `true`.
40+
- `wait_for_readiness_timeout` (String) How long to wait for readiness (if enabled). Defaults to `5m0s`.
4041

4142
### Read-Only
4243

grafana/data_source_cloud_stack.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ available at “https://<stack_slug>.grafana.net".`,
2424
Computed: true,
2525
Description: "The region this stack is deployed to.",
2626
},
27-
"wait_for_readiness": nil,
27+
"wait_for_readiness": nil,
28+
"wait_for_readiness_timeout": nil,
2829
}),
2930
}
3031
}

grafana/resource_cloud_stack.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package grafana
22

33
import (
4+
"bytes"
45
"context"
5-
"errors"
66
"fmt"
77
"log"
88
"net/http"
@@ -13,12 +13,15 @@ import (
1313
"time"
1414

1515
gapi "github.com/grafana/grafana-api-golang-client"
16+
"github.com/hashicorp/go-cty/cty"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1819
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1920
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2021
)
2122

23+
const defaultReadinessTimeout = time.Minute * 5
24+
2225
var stackSlugRegex = regexp.MustCompile("^[a-z][a-z0-9]+$")
2326

2427
func ResourceCloudStack() *schema.Resource {
@@ -84,6 +87,24 @@ Changing region will destroy the existing stack and create a new one in the desi
8487
// If the diff is suppress for a "true" value, the attribute cannot be read at all
8588
DiffSuppressFunc: func(_, _, newValue string, _ *schema.ResourceData) bool { return newValue == "false" },
8689
},
90+
"wait_for_readiness_timeout": {
91+
Type: schema.TypeString,
92+
Optional: true,
93+
Default: defaultReadinessTimeout.String(),
94+
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
95+
v := i.(string)
96+
_, err := time.ParseDuration(v)
97+
if err != nil {
98+
return diag.Errorf("%q is not a valid duration: %s", v, err)
99+
}
100+
return nil
101+
},
102+
// Only used when wait_for_readiness is true
103+
DiffSuppressFunc: func(_, _, newValue string, d *schema.ResourceData) bool {
104+
return newValue == defaultReadinessTimeout.String()
105+
},
106+
Description: "How long to wait for readiness (if enabled).",
107+
},
87108
"org_id": {
88109
Type: schema.TypeInt,
89110
Computed: true,
@@ -338,7 +359,11 @@ func waitForStackReadiness(ctx context.Context, d *schema.ResourceData) diag.Dia
338359
return nil
339360
}
340361

341-
err := resource.RetryContext(ctx, 2*time.Minute, func() *resource.RetryError {
362+
timeout := defaultReadinessTimeout
363+
if timeoutVal := d.Get("wait_for_readiness_timeout").(string); timeoutVal != "" {
364+
timeout, _ = time.ParseDuration(timeoutVal)
365+
}
366+
err := resource.RetryContext(ctx, timeout, func() *resource.RetryError {
342367
req, err := http.NewRequestWithContext(ctx, http.MethodHead, d.Get("url").(string), nil)
343368
if err != nil {
344369
return resource.NonRetryableError(err)
@@ -349,7 +374,15 @@ func waitForStackReadiness(ctx context.Context, d *schema.ResourceData) diag.Dia
349374
}
350375
defer resp.Body.Close()
351376
if resp.StatusCode != 200 {
352-
return resource.RetryableError(errors.New("stack is not ready yet"))
377+
buf := new(bytes.Buffer)
378+
body := ""
379+
_, err = buf.ReadFrom(resp.Body)
380+
if err != nil {
381+
body = "unable to read response body, error: " + err.Error()
382+
} else {
383+
body = buf.String()
384+
}
385+
return resource.RetryableError(fmt.Errorf("stack was not ready in %s. Status code: %d, Body: %s", timeout, resp.StatusCode, body))
353386
}
354387

355388
return nil

grafana/schema.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func cloneResourceSchemaForDatasource(r *schema.Resource, updates map[string]*sc
3131
clone[k].Default = nil
3232
clone[k].StateFunc = nil
3333
clone[k].DiffSuppressFunc = nil
34+
clone[k].ValidateDiagFunc = nil
3435
clone[k].ValidateFunc = nil
3536
}
3637
for k, v := range updates {

0 commit comments

Comments
 (0)