Using terraform-plugin-codegen-framework generated code to instantiate nested objects

I’m working on a Terraform resource provider using the T terraform-plugin-codegen-framework (tpcf) for code generation.

Here’s a sample resource configuration to get an idea:

resource "prov_cluster" "example" {
  name = "mycluster"
  addons = [
    {
      name    = "monitoring"
      enabled = true
    },
    {
      name    = "logging"
      enabled = true
    }
  ]
}

In my resource schema, there’s an attribute called addons which is a Set of Objects. The code generated by tpcf includes an AddonsType struct that implements basetypes.ObjectTypable and an AddonsValue struct that implements basetypes.ObjectValuable.

The generated code also includes the following helper functions:

  • NewAddonsValueNull()
  • NewAddonsValueUnknown()
  • NewAddonsValue(attributeTypes map[string]attr.Type, attributes map[string]attr.Value)

However, I’m finding it difficult to construct the addons nested object from the generated code. The main challenges are:

  1. There doesn’t seem to be an easy way to instantiate an AddonsValue{} struct directly, as the state attribute is not exported.
  2. The NewAddonsValue() helper function requires a lot of input, including hardcoded attribute names, which makes it cumbersome to use.

I need an efficient way to construct the AddonsValue{} instance. Once it is constructed, it’s straightforward to create a basetypes.SetValue by passing a slice of AddonsValue{} instances to the types.SetValueFrom() function.

Why there isn’t any generated function like the following:

func NewKnownAddonsValueMust(addonsValue AddonsValue) AddonsValue {
	if addonsValue.Name.IsUnknown() {
		panic("Name attribute is unknown")
	}
	// ... checks on the attributes...
	addonsValue.state = attr.ValueStateKnown
	return addonsValue
}

What are the recommended approaches or best practices for constructing the value of a nested object? Any insights or examples would be greatly appreciated.