terraform-provider-restapi icon indicating copy to clipboard operation
terraform-provider-restapi copied to clipboard

How to skip refresh

Open rngtng opened this issue 1 year ago • 2 comments

Hey, hey

First thanks a lot for this provider! I'm only facing an issue where the return value is available in the create response (on POST), but not on read (GET). As of this the state changes after a second tf apply. So is there an option to skip the refresh/GET?

My current fix/workaround is using the static provider. Here secret value is available on POST but not on GET. But any ideas how I could solve this with the restapi_object resource alone?

resource "restapi_object" "subscription" {
  path = "/v1/webhooks"
  data = jsonencode({
     "url" = "..."
   })
}

resource "static_data" "secret" {
  data = {
    value = sensitive(lookup(restapi_object.subscription.api_data, "secret", ""))
  }

  // optionally you can set the lifecycle.ignore_changes attribute to
  // prevent unnecessary resource changes
  lifecycle {
    ignore_changes = [data]
  }
}

rngtng avatar Apr 06 '23 09:04 rngtng

hey @rngtng , I am facing the same issue. The resource is being recreated as GET does not return the secret. Can you explain more on how you managed to do it via static_data? how does it work?

georgesjamous avatar Apr 24 '23 11:04 georgesjamous

Sure @georgesjamous. With leveraging the static provider the secret is ‘cached’. This means even in a refresh the rest resource will update, the static provider doesn’t.

output "secret" {
  value = static_data.secret.output.value
}

See https://registry.terraform.io/providers/tiwood/static/latest/docs/resources/data

rngtng avatar Apr 24 '23 14:04 rngtng

Hi, @rngtng -- have you tried JSON-decoding the restapi_object resource's create_response attribute to retrieve your secret as it was returned from the original POST? This approach just worked nicely for me and seems to be the intended usage for that attribute, based on the documentation in the associated feature commit:

https://github.com/Mastercard/terraform-provider-restapi/blob/7b0e88c173a14e9c9cc43d70f3122bc3b918bd66/README.md?plain=1#L60

phaseshift42 avatar Jul 17 '24 00:07 phaseshift42

@phaseshift42 nice - this did the job!! Closing the issue. Thanks!

rngtng avatar Jul 17 '24 05:07 rngtng