terraform-plugin-sdk icon indicating copy to clipboard operation
terraform-plugin-sdk copied to clipboard

Remove empty string value in schema.Schema

Open odg0318 opened this issue 4 years ago • 1 comments

SDK version

github.com/hashicorp/terraform-plugin-sdk v1.14.0

Relevant provider source code

case1

https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/autoscaling_tags.go#L17

func autoscalingTagSchema() *schema.Schema {
	return &schema.Schema{
		Type:     schema.TypeSet,
		Optional: true,
		Elem: &schema.Resource{
			Schema: map[string]*schema.Schema{
				"key": {
					Type:     schema.TypeString,
					Required: true,
				},

				"value": {
					Type:     schema.TypeString,
					Required: true,
				},

				"propagate_at_launch": {
					Type:     schema.TypeBool,
					Required: true,
				},
			},
		},
		Set: autoscalingTagToHash,
	}
}

case2

https://github.com/terraform-providers/terraform-provider-aws/blob/master/aws/resource_aws_autoscaling_group.go#L392

                        "tags": {
                                Type:     schema.TypeSet,
                                Optional: true,
                                Elem: &schema.Schema{
                                        Type: schema.TypeMap,
                                        Elem: &schema.Schema{Type: schema.TypeString},
                                },
                                ConflictsWith: []string{"tag"},
                        },

Terraform Configuration Files

case1

resource "aws_autoscaling_group" "this" {
  tag {
    key                 = "foo"
    value               = "foo"
    propagate_at_launch = true
  }
  tag {
    key                 = "bar"
    value               = ""
    propagate_at_launch = true
  }
}

case2

resource "aws_autoscaling_group" "this" {
  tags = [{
    key                 = "foo"
    value               = "foo"
    propagate_at_launch = true
  },{
    key                 = "bar"
    value               = ""
    propagate_at_launch = true
  }]
}

Debug Output

terraform plan

value is dropped at the first element.

      + tag {
          + key                 = "bar"
          + propagate_at_launch = true
        }
      + tag {
          + key                 = "foo"
          + propagate_at_launch = true
          + value               = "foo"
        }

Expected Behavior

value should be set even if it is empty string, "".

      + tag {
          + key                 = "bar"
          + propagate_at_launch = true
          + value               = ""
        }
      + tag {
          + key                 = "foo"
          + propagate_at_launch = true
          + value               = "foo"
        }

Actual Behavior

value key is removed if it is empty string.

Steps to Reproduce

This issue can be reproduced using aws_autoscaling_group resource. Just write a code using it and terraform plan. And then you can see that the empty string value is removed in the output.

References

  • https://github.com/terraform-providers/terraform-provider-aws/issues/9049

odg0318 avatar Jun 30 '20 15:06 odg0318

Additionally, I share debug message with TF_LOG=DEBUG environment.

terraform debug log

2020/07/01 20:35:41 [WARN] Provider "registry.terraform.io/-/aws" produced an invalid plan for aws_autoscaling_group.this, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .protect_from_scale_in: planned value cty.False does not match config value cty.NullVal(cty.Bool)
      - .tags: planned value cty.SetVal([]cty.Value{cty.MapVal(map[string]cty.Value{"key":cty.StringVal("bar"), "propagate_at_launch":cty.StringVal("true")})}) does not match config value cty.SetVal([]cty.Value{cty.MapVal(map[string]cty.Value{"key":cty.StringVal("bar"), "propagate_at_launch":cty.StringVal("true"), "value":cty.StringVal("")})})

req.PlannedState.Msgpack

When I debug plannedStateVal variable, empty string value is already removed in map. https://github.com/hashicorp/terraform-plugin-sdk/blob/master/internal/helper/plugin/grpc_provider.go#L852

PlanResourceChange

In this line, plannedAttrs returns a map that empty string value is removed. https://github.com/hashicorp/terraform-plugin-sdk/blob/v1.14.0/internal/helper/plugin/grpc_provider.go#L665

odg0318 avatar Jul 01 '20 11:07 odg0318