Hello,
I have this imported
resource "my-resource" "test" {
id = 1234
name = "test"
my_list = [
{
id = 5678
sub_obj = {
id = "this-is-id"
}
},
{
id = 9123
sub_obj = {
id = "this-is-id2"
}
},
]
}
I want to be able to remove elements from my_list by writing for example.
resource "my-resource" "test" {
id = 1234
name = "test"
my_list = [
{
id = 5678
sub_obj = {
id = "this-is-id"
}
},
]
}
My schema is
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
...
"my_list": schema.ListNestedAttribute{
Computed: true,
Optional: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Computed: true,
},
"sub_obj": schema.ObjectAttribute{
Computed: true,
AttributeTypes: map[string]attr.Type{
"id": types.StringType,
},
},
},
},
},
},
}
On Update() I started writing this but I’m a bit lost, see comments
func (r *myResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Here I have multiple different update methods depending on what I'm updating.
// So do I have a better choice than getting plan & state and comparing ?
// I saw in tutorial only plan use, but then we provide all the plan values,
// may they be updated or not
var plan, state myResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
// Do I have a better way than comparing the elements like this?
if !plan.MyList.Equal(state.MyList) {
for _, planEl := range plan.MyList.Elements() {
found := false
for _, stateEl := range state.MyList.Elements() {
if planEl.Equal(stateEl) {
found = true
}
}
if !found {
_, errDiag := types.ObjectValueFrom(ctx, MyType, planEl)
resp.Diagnostics.Append(errDiag...)
// How to marshal/map correctly to access
// MyList.ID / MyList.SubObj.ID values here?
}
}
}
resp.Diagnostics.Append(resp.State.Set(ctx, state)...)
}
Always a bit lost with the lists when writing the provider… took me a good while to be able to have this complex list read correctly in resources, and it was with the help of browsing existing complex provider repos on github. I feel the tutorial is a bit light on the complex parts of things
Thank you.