Handling of a union type

I’m writing my first Terraform resource, aws_securityhub_configuration_policy, which has a rather complex schema that includes two objects of a union type - Policy and ParameterValue. I used skaff to generate a template which is great but there is no mention of the union type. I couldn’t find anything in the docs either. I’m really struggling to get my head around it, especially when it comes to flatten and expand functions as it uses an interface type instead of the usual struct type. I’d appreciate any hints!

Hi @JakubBiel :wave:

Thanks for your interest in contributing to the AWS Provider! To get some assumptions out of the way, I’ll base the rest of this response on the idea that the following APIs will be used for this resource. Please let me know if I’ve misunderstood anything.

In the root of the Create and Update API’s, the object denoted as a union type (ConfigurationPolicy) actually only has one possible field in the underlying object (SecurityHub). Since there is only one nested element, you can treat this the same way any other nested block would be implemented with no special validation.

Several blocks down the ConfigurationPolicy policy object, the ParameterConfiguration object has a union type, Value. Unlike the first example, this attribute truly has several nested fields, only one of which can be set at a time. For this case we can use Terraform Plugin Framework ExactlyOneOf resource validator to ensure only one of the nested elements inside the block is set. You can read more about resource validation in the Terraform Plugin Framework documentation here.

We also have a dedicated Slack channel for AWS provider contributors which you can request access to using the form linked here. This is a great option for faster feedback and detailed discussions on specific APIs.

Hope this helps!

2 Likes

Thanks, that’s very helpful!