Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
785 stars 215 forks source link

Resource not recreated if not found with `read_search` #269

Open mauriceackel opened 2 months ago

mauriceackel commented 2 months ago

When a resource state is refreshed, the provider tries to read the resource from the API.

If the read fails with a 404, we set obj.id = "" causing a recreation of the resource. This works in the following cases:

  1. The read requests returns a 404
  2. The read request returns a 200 but there was read_search properties provided and there was an error while searching the resource in the results (e.g. unknown results_key, unknown search_key, etc.)

However, when the resource was not found with read_search, the resource is not marked for creation!

Expected behaviour: If the resource was not found during state refresh (either because the API returned a 404 or the search did not return a result), the resource should be marked for creation.

Investigation

The issue is that when the object is read in this line, we use the findObject method and check, whether it returns an error. The error is returned in many cases but it is not returned, if the object exists in the state but was not found using in the findObject method. This behaviour can be seen in this line. There, we only return an error if the object id was "", but not if it existed before.

I'm not sure where the best place to fix this issue is without breaking existing functionality. I'd assume that the best place to check would be in the readObject method. Here, we could extend the check to not only set obj.id = "" if there was an error, but also if objFound is undefined.