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
274 stars 48 forks source link

support object without properties #292

Open FalcoSuessgott opened 3 years ago

FalcoSuessgott commented 3 years ago

Is your feature request related to a problem?

We want to use that provider as a netbox terraform provider, which supports OpenAPIv2.

Currently defintions of type objects without any properties are not supported. Netbox offers the possibility to add custom fields to objects. Those custom fields can have various data types (bool, string, date, ...).

Since in the OpenAPI spec those custom_fields are represented as objects without any properties:

    "Secret": {
      "required": [
        "assigned_object_type",
        "assigned_object_id",
        "role",
        "plaintext"
      ],
      "type": "object",
      "properties": {
        "id": {
          "title": "ID",
          "type": "integer",
          "readOnly": true
        },
        [....]
        "custom_fields": {
          "title": "Custom fields",
          "type": "object",
          "default": {}
        },

Currently the provider responds the following:

│ 23 errors occurred:
   ....
│   * resource netbox_secrets: custom_fields: Default is not valid for lists or sets

As a terraform schema, custom_fields are represented as the following

"custom_fields": {
  Type:     schema.TypeMap,
  Optional: true,
},

Describe the solution you'd like

It would be nice to have maps supported or to find a workaround to get that provider working with empty objects.

Describe alternatives you've considered

Maybe you can give us some hints where to start implementing this feature on our own.

Checklist (for admin only)

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

dikhan commented 3 years ago

Hi @FalcoSuessgott ,

Thanks for opening the issue, I was just reading the custom field docs to try and get a bit more context and was wondering about the following:

definitions:
  Secret:
    type: "object"
    required:
      ...
    properties:
...
      custom_fields:
        title: "Custom fields"
        type: "array"
        items:
          type: "object"
          properties:
            type:
              description: "custom property type, must be one of the supported types as specified in the regex"
              regex: "string|integer|boolean|date|url|selection|multiple_selection"
              type: string
            value:
              type: string
{
    "id": 123,
    "url": "http://localhost:8000/api/dcim/sites/123/",
    "name": "Raleigh 42",
    ...
    "custom_fields": {
        "deployed": "2018-06-19",
        "site_code": "US-NC-RAL42"
    },
    ...

Hope that helps!

Thanks Dani

dikhan commented 2 years ago

Hi @FalcoSuessgott ,

Just wanted to follow up on this and see whether the previous response help at all. Reading again the error you got from the terraform provider I wonder if removing the Default from the OpenApi spec for the object property might also unblock you:

  "Secret": {
      "required": [
        "assigned_object_type",
        "assigned_object_id",
        "role",
        "plaintext"
      ],
      "type": "object",
      "properties": {
        "id": {
          "title": "ID",
          "type": "integer",
          "readOnly": true
        },
        [....]
        "custom_fields": {
          "title": "Custom fields",
          "type": "object",
        },

Currently, Terraform does not support setting defaults for fields other than primitives.

Thanks, Dani