0
votes

I am trying to build a Terraform Provider and there is a field from the external API that can return a type of list or string. What would be the best way of defining the schema for an API with this behavior.

I read through the Terraform Provider docs: https://www.terraform.io/docs/extend/schemas/schema-types.html and was unable to find a way to solving this.

1

1 Answers

0
votes

At the time of writing, there is no way to achieve this with the Terraform SDK. The SDK is currently supporting the common features of both Terraform 0.11 and 0.12 in order to retain 0.11 compatibility, and since dynamic types were introduced only in Terraform 0.12 that feature is not yet available in the SDK.

A common workaround for providers at the moment has been to define the attribute in question as being a string and add _json to the end of the name, and then have the provider write a JSON-encoded value into the attribute. The calling configuration can then use jsondecode to extract the value.

The reason for the _json suffix here is that it's planned to support dynamic typing like this in a future SDK revision (once Terraform 0.11 compatibility is dropped) and so having a name like foo_json today leaves the name foo available for use later in a deprecation cycle moving away from foo_json.


Internally Terraform 0.12's provider protocol does already support this possibility by marking a particular attribute as having a dynamic type. The SDK just doesn't have an interface to select that, for the reasons given above. In principle it is therefore possible to write a provider directly against the raw protocol which would support only Terraform 0.12 and could use dynamic typing, but that's a lot of work and is unprecedented so far outside of prototypes and experiments. I would not recommend it, but am mentioning it only for completeness.