Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
808 stars 217 forks source link

Updating a resource with id in the payload #239

Open amvapor opened 1 year ago

amvapor commented 1 year ago

Built to fix: https://github.com/Mastercard/terraform-provider-restapi/issues/230

The API I am interacting with has a /services endpoint, where updates require an ID in the payload, so with this change, we can inject the id using the keyword {id}. In my case, I inject the id into payload and can make a POST to /services with the id in the body, which meets the criteria of the API in question to update the resource rather than creating another one.

Way I am utilizing in my code:


resource "restapi_object" "services" {
  for_each = {
    simple_email = {
      targetName     = "Simple Email Service the First",
      description    = "Service to manage inbound emails",
    },
    simple_email2 = {
      targetName     = "Simple Email Service the Second",
      description    = "Service to manage inbound emails",
    }
  }

  path = "/services"
  update_path = "/services"
  data = jsonencode(each.value)
  update_data = jsonencode(merge(each.value, {id = "{id}"})) #reuse the same object, but add the id element to it when we're doing updates.
}```

Test of this use case is authored and passes
I added to the Documentation as well.
mrowken commented 9 months ago

This is exactly the solution I'm looking for. It has great potential for many scenarios. Why not being merged ?

ivank commented 7 months ago

I'm also having the exact same issue. Though maybe arbitrary searching for "{id}" is not the best approach, as it can easily lead to false positives. Maybe we need to add a "update_with_object_id = true" parameter?

ivank commented 7 months ago

Actually just discovered that copy_keys provides functionality for exactly this purpose!

ofbjansen commented 5 months ago

Actually just discovered that copy_keys provides functionality for exactly this purpose!

Hi @ivank, can you provide me/us with an example. I am trying to the variable as above, but the body still shows the variable name and not the actual id.

Small detail: we get the id not in the root, but in a object called data and we need to add this to the update and delete body.

Thanks