hashicorp / hashicorp/terraform-plugin-sdk

DiffSuppressFunc on Optional integer field never sets the value

Open
#806 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Go
Stars
485
Forks
244
Avg merge
19h 57m
Merged PRs (30d)
4

Description

SDK version
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.1
Relevant provider source code
func resourceExample() *schema.Resource {
	return &schema.Resource{
		Create: resourceExampleCreate,
		Update: resourceExampleCreate,
		Read:   resourceExampleRead,
		Delete: resourceExampleDelete,
		Importer: &schema.ResourceImporter{
			State: schema.ImportStatePassthrough,
		},

		Schema: map[string]*schema.Schema{
			"requested_validity": {
				Type:         schema.TypeInt,
				Optional:     true,
				ValidateFunc: validation.IntInSlice([]int{7, 30, 90, 365, 730, 1095, 5475}),
				DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
					return true
				},
			},
		},
	}
}
Terraform Configuration Files
resource "example" "example" {
  requested_validity = 7
}
Debug Output

https://gist.githubusercontent.com/jacobbednarz/f15e55aa25902b275ae3f5bc2c68ad5c/raw/3e9d8f7931fc453de325e0d517e15131cb2a67a4/gistfile1.txt

Expected Behavior

The requested_validity should be passed to the CRUD method as 7

Actual Behavior

The requested_validity value is passed as 0.

Steps to Reproduce

If you have a Cloudflare account you can run the acceptance tests with (email/chat me on Terraform Slack and I can give you one off credentials)

  • git clone https://github.com/cloudflare/terraform-provider-cloudflare.git
  • CLOUDFLARE_API_USER_SERVICE_KEY="xxx" CLOUDFLARE_API_KEY="xxx" CLOUDFLARE_EMAIL="xxxx" CLOUDFLARE_DOMAIN="<your_domain_here>" CLOUDFLARE_ZONE_ID="<your_zone_id_here>" TESTARGS='-run "^TestAccCloudflareOriginCACertificate_Basic" -count 1 -parallel 1' make testacc
  • See the test fail
  • Remove DiffSuppressFunc from the resource
  • Re-run test
  • Test will pass

No Cloudflare account

  • Create a new resource as documented above
  • Attempt to apply the resource
  • See the CRUD method is getting a zero value (not user defined)
  • Remove DiffSuppressFunc attribute
  • Attempt to apply the resource
  • CRUD method gets correct value and works as expected
References

n/a

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided resource schema and trace how the SDK handles DiffSuppressFunc for an optional TypeInt field. Reproduce the issue using the minimal Terraform configuration or the documented acceptance-test command, comparing behavior with and without DiffSuppressFunc. Done means requested_validity reaches the CRUD method as 7 while diff suppression remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, terraform
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.