Search code examples
goterraformterraform-provider

Itherate through MapNestedAtribute in Terraform


I have list nested attribute spec defined in main.tf and it looks like this:

 di_name = [
    {
      source_name = "test1"
      new_d_name  = "test2"
      new_di_name = "test3"
      di_type     = "test4"
    },
    {
      source_name = "test1"
      new_d_name  = "test2"
      new_di_name = "test3"
      di_type     = "test4"
    },
  ]

It corresponds to the following.

"di_name" : schema.ListNestedAttribute{
                Optional: true,
                NestedObject: schema.NestedAttributeObject{
                    Attributes: map[string]schema.Attribute{
                        "source_name": schema.StringAttribute{
                            Required: true,
                        },
                        "new_d_name" : schema.StringAttribute{
                            Required: true,
                        },
                        "new_di_name" : schema.StringAttribute{
                            Required: true,
                        },
                        "di_type" : schema.StringAttribute{
                            Required: true,
                        },
                    },
                },







  type ModelStruct struct {

DName              []vmDi `tfsdk:"di_name"`
}

type vmDi struct {
    Source Name    types.String `tfsdk:"source_name"`
    NewDName       types.String `tfsdk:"new_d_name"`
    NewDiName      types.String `tfsdk:"new_di_name"`
    DiType         types.String `tfsdk:"di_type"`
}

In new class/package hope that is right therm in GO, I have corresponding struct that I would like to initialize with data.

type VmBi struct {

  GetData [] VmDi
}


 type   VmDi struct {
        Source Name    string
        NewDName       string
        NewDiName      string
        DiType         string
    }

Now I would like to use this create method to get all values from main.tf from di_name list:

func (r *vmResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {

   Initialize struct with di_name values.
newData := newGoPackage.VmDi {


 Put data here from di_name

     }
    
  }

How would I initialize that struct, since I can't access len(di_name) here.

Any help will do, thanks in advance.


Solution

  • I am assuming this is the terraform-framework-plugin since the usage appears consistent although it is not specified. To initialize a struct from the resource model one must declare the struct, and then use State.Get from the request to modify the struct as a reference within the method. The reference will then contain the HCL2 deserialized/unmarshalled values from the Terraform config. It is typical to also append the return to the response diagnostics. The pattern typically appears like:

    func (r *vmResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
      var state ModelStruct
      resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
      if resp.Diagnostics.HasError() {
          return
      }
      // state.DName now resolves as expected as type []vmDi and is iterable
    }