Skip to content

Commit aa32d25

Browse files
Cloud: Fix flaky stack creation (#1033)
When creating lots of cloud stacks, the API can sometimes return timeouts However, the stack may still be creating in the background Instead of failing immediately, the stack create method can instead try to read the stack before failing
1 parent 571c36c commit aa32d25

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

internal/resources/cloud/resource_cloud_stack.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/url"
1010
"regexp"
1111
"strconv"
12+
"strings"
1213
"time"
1314

1415
gapi "github.com/grafana/grafana-api-golang-client"
@@ -238,16 +239,21 @@ func CreateStack(ctx context.Context, d *schema.ResourceData, meta interface{})
238239
}
239240

240241
stackID, err := client.NewStack(stack)
241-
if err != nil && err.Error() == "409 Conflict" {
242+
switch {
243+
case err != nil && strings.Contains(err.Error(), "409"):
242244
return diag.Errorf("Error: A Grafana stack with the name '%s' already exists.", stack.Name)
245+
case err != nil:
246+
// If we had an error that isn't a 409 (already exists), try to read the stack
247+
// Sometimes, the stack is created but the API returns an error (e.g. 504)
248+
readStack, readErr := client.StackBySlug(stack.Slug)
249+
if readErr != nil {
250+
return diag.Errorf("Failed to create stack: %v", err)
251+
}
252+
d.SetId(strconv.FormatInt(readStack.ID, 10))
253+
default:
254+
d.SetId(strconv.FormatInt(stackID, 10))
243255
}
244256

245-
if err != nil {
246-
return diag.FromErr(err)
247-
}
248-
249-
d.SetId(strconv.FormatInt(stackID, 10))
250-
251257
if diag := ReadStack(ctx, d, meta); diag != nil {
252258
return diag
253259
}

0 commit comments

Comments
 (0)