How to store a computed value of TypeList in terraform state

I have a computed value srm_nodes defined in the resource schema.

“srm_node_ids”: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
“id”: {
Type: schema.TypeString,
Computed: true,
},
“type”: {
Type: schema.TypeString,
Computed: true,
},
“host_name”: {
Type: schema.TypeString,
Computed: true,
},
},
},

The list and its values are all computed. How can I set these computed values in the resourceRead method? I usually set the value of a string computed type using d.Set("item_name",item.Name) . What is the correct way to do the same for each computed value of a TypeList ?

Hi @saloneerege!

For a schema of type TypeList with an Elem of schema.Resource, the d.Set method will expect to receive a value that is a slice containing maps whose elements match the nested resource schema. In this case, a value something like this:

d.Set("srm_node_ids", []interface{}{
  map[string]interface{}{
    "id": "foo1",
    "type": "bar1",
    "host_name": "baz1",
  },
  map[string]interface{}{
    "id": "foo2",
    "type": "bar2",
    "host_name": "baz2",
  },
})

The Set method will automatically traverse that data structure and pass the Terraform type system equivalent of it back to Terraform for storage in the state.

1 Like

Thnaks @apparentlymart. This was helpful