hashicorp / terraform-plugin-codegen-framework

Terraform Provider Code Generation Specification to Framework
Mozilla Public License 2.0
39 stars 17 forks source link

Nested Maps - plan failure if the attribute is marked optional #154

Closed mdmohan closed 2 months ago

mdmohan commented 3 months ago

tfplugingen-framework CLI version

v0.4.0

Provider Code Spec File

Schema Generated

func TestRscResourceSchema(ctx context.Context) schema.Schema {
  return schema.Schema{
    Attributes: map[string]schema.Attribute{
      "id": schema.StringAttribute{
        Computed:            true,
        Description:         "Unique identifier for the interface",
        MarkdownDescription: "Unique identifier for the interface",
      },
      "parameter_map": schema.MapNestedAttribute{
        NestedObject: schema.NestedAttributeObject{
          Attributes: map[string]schema.Attribute{
            "custom_attributes_nested_map": schema.MapAttribute{
              ElementType:         types.StringType,
              Optional:            true,
              Description:         "Custom attributes",
              MarkdownDescription: "Custom attributes",
            },
            "serial_number": schema.StringAttribute{
              Required:            true,
              Description:         "Serial number of switch to configure",
              MarkdownDescription: "Serial number of switch to configure",
            },
          },
          CustomType: ParameterMapType{
            ObjectType: types.ObjectType{
              AttrTypes: ParameterMapValue{}.AttributeTypes(ctx),
            },
          },
        },
        Required:            true,
        Description:         "interfaces to configure",
        MarkdownDescription: "interfaces to configure",
      },
      "policy": schema.StringAttribute{
        Required:            true,
        Description:         "Name of the policy. Examples: `int_trunk_host`, `int_access_host`",
        MarkdownDescription: "Name of the policy. Examples: `int_trunk_host`, `int_access_host`",
        Validators: []validator.String{
          stringvalidator.OneOf("int_trunk_host", "int_access_host"),
        },
      },
    },
  }
}

Expected Behavior

In this given resource the attribute custom_attributes_nested_map is marked optional as its not used always. Expecting the attribute custom_attributes_nested_map to contain valid key-value pairs or it can be empty. Facing a problem when custom_attributes_nested_map is left empty and not mentioned in config file

Sample config

resource "ndfc_test_rsc" "test" {
    policy = "test_policy"
    parameter_map = {
      "test1" : {
                serial_number = "11212"
       }
   }
}

Expecting the plan to be successful with an empty/null map generated for custom_attributes_nested_map as it is optional

Actual Behavior

Planning failed.

Terraform encountered an error while generating this plan.

│ Error: Provider produced invalid plan
│ 
│ Provider "registry.terraform.io/cisco/ndfc" planned an invalid value for ndfc_test_rsc.test.parameter_map.test1.custom_attributes_nested_map: planned value
│ cty.MapValEmpty(cty.String) for a non-computed attribute.
│ 

Terraform expects MapNull but it gets MapValEmpty (This is coming from ToObjectValue in generated code)

Additional Information

Work around

1) Specify an empty custom_attributes_nested_map

resource "ndfc_test_rsc" "test" {
    policy = "int_trunk_host"
    parameter_map = {
      "test1" : {
                serial_number = "11212"
                custom_attributes_nested_map = {}
       }
   }
}

or 2) Change the attribute custom_attributes_nested_map to optional_computed

Possible Solution:

This can be solved if the generated function func (v ParameterMapValue) ToObjectValue(ctx context.Context) (basetypes.ObjectValue, diag.Diagnostics) {

can set custom_attributes_nested_map to mapNull if the map is empty instead of creating an returning an empty map.

Kindly suggest if ToObjectValue can be modified to return MapNull in this situation or need to flag this to terraform as an inconsistency issue.

Code of Conduct

austinvalle commented 2 months ago

Hey there @mdmohan 👋🏻 , I believe the fixes introduced in #161 should address the behavior you're seeing from ToObjectValue.

Try regenerating your resource code with v0.4.1 and let us know if that fixes your data consistency error.