How to wait between multiple requests

Hi I am new to terraform plugin development. I am trying to write a Create method where I need to first send two requests in order to create a resource; between these requests I need to poll and wait for some state. This looks like a common procedure, may I know what is the best way to do it? Or is there any helper function to wait for a certain state to reach? Or I just need to implement my own polling?

Hi @weilueluo :wave:

Can you expand a little on what the 2 requests are doing? Are they 2 independent API calls that are creating 2 different resources? If so, then a typical Terraform provider would separate the 2 API calls into 2 different resource create functions.

In terms of polling to wait for state, usually the create function would be responsible for waiting/polling to determine whether the resource was successfully created or not. This is described in this comment. So in the case you describe you would need to implement polling.

Hey @bendbennett , thanks for the reply!

So the first requests creates the resource, and the second request authenticate the resource so that it is usable. Without the second request, the resource would not be very useful. It seems like I should just implement the polling then! Thank you!

p.s. I see in sdkv2 there is retry, is there something similar in plugin framework? Or I should just import and use the sdkv2 helper?

It wouldn’t be crazy to create both myprovider_thing and myprovider_thing_authentication resources.

If the usual non-terraform workflow and documentation don’t mention that it’s actually 2 steps, I’d probably lean toward "make both API calls in one Create() for continuity of end user experience.

As @hQnVyLRx suggests, if the API you’re using expects that the requests for resource creation and “authentication” are considered to be a single step, then it might be best to have both requests as part of a single create function, with polling in between the two requests.

The retry that you linked to is specific to SDKv2 and was not ported to the Framework, so you would need to add you’re own logic for polling and handling resource creation failures/errors. You can use any retry library to do the polling, such as retry package - - Go Packages.

@hQnVyLRx @bendbennett Thank you both for the reply! Yea make sense, thanks for the links too :+1: