dikhan / terraform-provider-openapi

OpenAPI Terraform Provider that configures itself at runtime with the resources exposed by the service provider (defined in a swagger file)
Apache License 2.0
275 stars 48 forks source link

Support for simple listable data source #328

Open arjunrajinstaclustr opened 2 years ago

arjunrajinstaclustr commented 2 years ago

Is your feature request related to a problem?

As a user of this plugin, I would like to define my APIs to return a single object containing a list of results as my data source so that I do not have to perform any hacky workarounds on my API to conform with the plugin's requirements.

Describe the solution you'd like

Non-CRUD data sources should be created for any GET endpoint that returns a single object, not just ones that return an array.

Acceptance criteria

A data source should be creatable for the following API response for example -

{
  "id": "some_id",
  "results": [
    {
      "myProperty": "myValue"
    }
  ]
}

Describe alternatives you've considered

The alternative has been to return a single item array on my GET list requests.

Example -

[
  {
    "id": "some_kind_of_id",
    "results": [
      {
        "myProperty": "myValue"
      }
    ]
  }
]

As you can see, the above workaround is a bit hacky. Users using my API directly will be fairly confused why I've got such a strange structure - with an array containing a single object containing an array.

If I don't wrap my response in such a manner and directly return my list of results I get the following error - Error: your query returned contains more than one result. Please change your search criteria to make it more specific because the plugin only wants one result (I think it is so that it can set the ID of the data source).

Additional context

Bonus points if an id/x-terraform-id doesn't have to be defined on the API response. I'm not sure Terraform even meaningfully uses the ID of a data source so you could even just set it to some random GUID.

Checklist (for admin only)

Don't forget to go through the checklist to make sure the issue is created properly:

x7airworker commented 2 years ago

Any updates on this issue?

pl4nty commented 1 year ago

Wrapped responses are common, I'm surprised this hasn't been implemented (unless there's a workaround?). I'm using a public API so can't just change its responses:

{
  "success": true,
  "data": [
    {
      "id": 1
    }
  ]
}