terraform-provider-openapi icon indicating copy to clipboard operation
terraform-provider-openapi copied to clipboard

Support for simple listable data source

Open arjunrajinstaclustr opened this issue 3 years ago • 2 comments

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

  • Data sources are created from all GET endpoints that return a single object.
  • A meta attribute is introduced to exclude certain GET endpoints from being converted to data sources.

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:

  • [ ] I have added a corresponding label (feature request) to the issue (right side menu)
  • [ ] I have added this issue to the 'API Terraform Provider' GitHub project (right side menu)

arjunrajinstaclustr avatar Feb 24 '22 07:02 arjunrajinstaclustr

Any updates on this issue?

x7airworker avatar Jul 09 '22 15:07 x7airworker

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
    }
  ]
}

pl4nty avatar Mar 13 '23 03:03 pl4nty