Set of SetNestedAttributes or MapNestedAttribute

I have to build a resource that has an attribute that is a set of sets of objects or a set of maps of objects. Ideally, it would look like:

resource "my_resource" "slug" {
  filters_v1 = [
    [
      { attribute = "decision", operator = "IN", values = ["un"] },
      { attribute = "process", operator = "NOT_IN", values = ["one", "two"] },
    ],
    [
      { attribute = "decision", operator = "IN", values = ["deux"] },
      { attribute = "process", operator = "NOT_IN", values = ["three"] },
    ],
  ]
}

or with some compromises

resource "my_resource" "slug" {
  filters_v2 = [
    {
      "decision" = { operation = "IN", values = ["un"] },
      "process"  = { operation = "NOT_IN", values = ["one", "two"] }
    },
    {
      "decision" = { operation = "IN", values = ["deux"] },
      "process"  = { operation = "NOT_IN", values = ["three"] }
    },
  ]
}

Can it be built with SetNestedAttribute or MapNestedAttribute without using a custom type? I am stuck because of the top level set.

SetNestedAttribute is always a set of objects (similar for map and the other nested attributes, it’s defined at the protocol level), so any representation of a multi dimensional collection will have to use SetAttribute or MapAttribute, along with the expected element type:

"filters_v1": schema.SetAttribute{
    ElementType: types.SetType{
        ElemType: types.ObjectType{/* filter attr types */},
    },
    Required: true,
    // ... other fields ...
},

Thanks a lot for your help. It works.

What is the recommended way to generate the docs for this attribute ?

Since it can’t be a nested attribute, there isn’t a way to attach any additional metadata to the nested objects or it’s attributes (like required/optional/computed, or documentation fields like description) in a way that Terraform (or downstream tools) can actually read → Consider Exposing Framework Provider Metadata · Issue #1022 · hashicorp/terraform-plugin-framework · GitHub

So unfortunately the only real option you have is to write the documentation manually, which if you’re using something like terraform-plugin-docs, you can do that with templates like:

1 Like