d.IsNewResource() use case. Its not working in the DiffSuppressFunc

We have this schema attribute

"user_info": {
				Type:             schema.TypeString,
				ForceNew:         true,
				DiffSuppressFunc: suppressUserInfo,
				Optional:         true,
				Description:      "User information",
			},

and the suppressUserInfo looks like this

func suppressUserInfo(k, old, new string, d *schema.ResourceData) bool {
	// During import
	if old == "" && !d.IsNewResource() {
		return true
	}
	return false
}

where we are suppressing this for old imported resources.

d.IsNewResource() is returning false for new resource

Hi @ramuklawjju,

According to the source code of the SDK, IsNewResource has meaning only in the resource Create function during the apply step:

That method will always return false in all other situations.

However, we can see there that all this thing really does is remembers whether the resource had an ID prior to running Create, so that the resource type implementation can get that answer even if it already called d.SetId to assign a new ID. Given that, I would expect you could get essentially the same effect by writing d.Id() == "" in your own logic here, because during the planning phase we haven’t run Create yet anyway, and so there’s no risk that the create step already assigned the new object an id.

1 Like