Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating a resource with id in the payload #239

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

amvapor
Copy link

@amvapor amvapor commented Oct 2, 2023

Built to fix:
#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
Copy link

mrowken commented Dec 19, 2023

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

@ivank
Copy link

ivank commented Feb 10, 2024

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
Copy link

ivank commented Feb 10, 2024

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

@ofbjansen
Copy link

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

@mdepedrof
Copy link

I'm in the same situation. Could you do the same in destroy_data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants