Skip to content

Commit

Permalink
explicit error
Browse files Browse the repository at this point in the history
  • Loading branch information
Dean Oren committed Jan 19, 2023
1 parent e25c8be commit b95b808
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp *
time.Sleep(30 * time.Second)

process := res.WaitHandler(ctx, r.client.MongoDBFlex.Instance, plan.ProjectID.ValueString(), plan.ID.ValueString())
process.SetTimeout(1 * time.Hour)
ins, err := process.WaitWithContext(ctx)
if err != nil {
resp.Diagnostics.AddError("failed MongoDB instance creation validation", err.Error())
Expand Down
19 changes: 16 additions & 3 deletions stackit/internal/resources/postgres-flex/instance/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,11 @@ func (r Resource) createUser(ctx context.Context, plan *Instance, d *diag.Diagno
// the default user credentials won't change
username := "stackit"
database := "stackit"
roles := []string{}

for maxTries := 10; maxTries > -1; maxTries-- {
c := r.client.PostgresFlex
body := users.CreateUserJSONRequestBody{
Database: &database,
Roles: &roles,
Username: &username,
}
res, err := c.Users.CreateUserWithResponse(ctx, plan.ProjectID.ValueString(), plan.ID.ValueString(), body)
Expand All @@ -161,13 +159,28 @@ func (r Resource) createUser(ctx context.Context, plan *Instance, d *diag.Diagno
if (res.StatusCode() == http.StatusNotFound ||
res.StatusCode() == http.StatusBadRequest) &&
maxTries > 0 {
time.Sleep(time.Second * 5)
time.Sleep(time.Second * 10)
continue
}
if res.HasError != nil {
d.AddError("failed create user request", res.HasError.Error())
}

if res.JSON400 != nil {
e := ""
if res.JSON400.Message != nil {
e = "message: " + *res.JSON400.Message
}
if res.JSON400.Fields != nil {
for k, v := range *res.JSON400.Fields {
e = e + "\n" + k + ": " + strings.Join(v, ", ")

}
}
d.AddError("response is 400", e)
return
}

if res.JSON200 == nil {
d.AddError("response is nil", "api returned nil response")
return
Expand Down

0 comments on commit b95b808

Please sign in to comment.