Mastercard / terraform-provider-restapi

A terraform provider to manage objects in a RESTful API
Other
820 stars 219 forks source link

restapi_object data is not optional #260

Open Justin-DynamicD opened 10 months ago

Justin-DynamicD commented 10 months ago

right now data is a required string for the restapi_object, which causes issues with some providers.

Notably CircleCI uses a curl POST with no data to create a new project, with configurations added by a separate call.

(https://circleci.com/docs/api/v2/index.html#operation/createProject) vs. (https://circleci.com/docs/api/v2/index.html#operation/patchProjectSettings)

To clarify, a get will still return a valid object with ID, it's just that initial POST needs to be empty :(

I can easily use update_path to make changes, but I cannot omit a default data variable during the initial create, nor even make them two different resources as the resource requires a data value.

mdepedrof commented 1 month ago

I have the same situation. I have an api that reads and deletes works without receive data. It only works set the id on the path. this is an example

curl --location 'https://xxxxxxxx.com/api/v2/firewall/alias?id=21' \
--header 'x-api-key: xxxxxxxx' \

But when use terraform always send a data and then my API needs a few parameters on payload that I cant send.

2024-10-22T14:25:51.013+0200 [INFO]  provider.terraform-provider-restapi_v1.20.0: 2024/10/22 14:25:51 api_client.go: method='GET', path='/api/v2/firewall/alias?id=18', full uri (derived)='https://xxxxxxx.com/api/v2/firewall/alias?id=18', data='': timestamp="2024-10-22T14:25:51.013+0200"

note the data=''. It seams that the provider always send the data. I think if this parameter is null or empty or we not provide on tf file, the request should not sent the data.

Error: unexpected response code '400': {"code":400,"status":"bad request","response_id":"MODEL_REQUIRES_ID","message":"Field `id` is required.","data":[]}

Maybe the option that sugest Justin-DynamicD in this comment, Adding a few parameters can be a valid solution:

create_include_data   = false
destroy_include_data  = false
read_include_data     = false
mdepedrof commented 1 week ago

I have solved this error by removing the Content-Type: application/json header. As far as I researched if this header is present, the request always has a json data. If none is provided the json that is sent is {}. In my case, when I removed the header, the request was sent without data and the API worked correctly.