SchwarzIT / terraform-provider-stackit

Community-maintained STACKIT Terraform provider
Apache License 2.0
18 stars 6 forks source link

Other strategy regarding the instance creation for Argus #163

Closed mathias-darscht closed 1 year ago

mathias-darscht commented 1 year ago

Hello,

since the instance creation of Argus is causing some issues we need a quick fix for this. The change will take place at this file actions.go. Currently we just wait a minute and then we say that we're up and running:

func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
   // ...
   time.Sleep(1 * time.Minute)
   // ...
}

Since Argus is a big service with many components the instance creation easily can take longer than one minute.

A solution to that could look like this:

func (r Resource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
   // ...
   requestSendPerTimeFrequency := false // checks if one request is send per three seconds
   requestSendSuccessfully := false 
   for end.Sub(start).Minutes() < 5 { // runs for maximum five minutes
      if int(end.Sub(start).Seconds()) % 3 == 0 && !requestSendPerTimeFrequency { // asking every three seconds if the instance is ready
         resp, err := client.Instances.Get(ctx, plan.ProjectID.String(), plan.ID.String()) // send request to the instance
         if agg := validate.Response(resp, err, "JSON200"); agg != nil { // check in the response if the instance is readygit 
            if resp != nil && resp.JSON200 == nil {
               diags.AddError("failed to create argus instance", agg.Error()) // instance is not ready yet
            } else {
               requestSendSuccessfully = true // instance is ready
               break
            }
         }
         requestSendPerTimeFrequency = true
      } else if int(end.Sub(start).Seconds()) % 3 != 0 && requestSendPerTimeFrequency {
         requestSendPerTimeFrequency = false
      }
      end = time.Now()
   }
   // handle somehow the boolean requestSendSuccessfully
   // ...
}

Thanks in advance.

do87 commented 1 year ago

this wait mechanism is already implemented after creation https://github.com/SchwarzIT/terraform-provider-stackit/blob/54405a41870eaeb3ecf85bb194fe7efe00692f55/stackit/internal/resources/argus/instance/actions.go#L85

as you can see, the client wait handler is doing exactly that, with a timeout of up to 1hr https://github.com/SchwarzIT/community-stackit-go-client/blob/a9f2ae8e2e13b7fcf1a10361deb99c7cf4f1b4d0/pkg/services/argus/v1.0/instances/wait.go#L22

the only reason why there's still a 'fake' time.Sleep(1 * time.Minute) is because i noticed that even after the instance is marked as ready, the grafana and metrics apis are not immediately available

to me this looks like something that can be fixed internally rather than in the provider, as part of the tests to see if an instance is actually ready to be used