Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
mdmohan opened this issue Aug 16, 2024 · 1 comment · Fixed by #161
Closed
1 task done

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

mdmohan opened this issue Aug 16, 2024 · 1 comment · Fixed by #161
Labels
bug Something isn't working

Comments

@mdmohan
Copy link
Contributor

mdmohan commented Aug 16, 2024

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

  • I agree to follow this project's Code of Conduct
@austinvalle
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants