Terraform sdk usage, which out of Get, GetOk, GetOkExists with boolean?

I am using “flag_presence” attribute as TypeBool

			"flag_presence": {
				Type:        schema.TypeBool,
				Optional:    true,
				Computed:    true,
				Description: "Indicates whether attribute is present",
			},

Now when I am doing this

d.Get(“flag_presence”) gives false

d.GetOk(“flag_presence”) gives false and false

d.GetOkExists(“flag_presence”) gives false and true

My logic is to check if the attribute is present, if present then set it to the options prototype.

if flagPresenceOk, ok := d.GetOkExists("flag_presence"); ok {
		flagPresence := flagPresenceOk.(bool)
		if flagPresence {
			options.FlagPresence = &flagPresence
		}
	}

GetOkExists works for me for now but I am getting this warning

Deprecated: usage is discouraged due to undefined behaviors and may be
// removed in a future version of the SDK

Please suggest.

HI @ramuklawjju,

Unfortunately with the legacy SDK you cannot reliably determine if a boolean is currently set in the configuration, as opposed to stored in the prior state or set via a default value. The fact that GetOkExists cannot be consistent is why it is deprecated. In order to exactly determine if this is set, you would need upgrade to the newer terraform-plugin-framework

If you have more questions, it’s probably better to ask under the terraform plugin SDK topic where there are more users familiar with the intricacies of writing providers.

I am using this attribute as TypeBool

			"flag_presence": {
				Type:        schema.TypeBool,
				Optional:    true,
				Computed:    true,
				Description: "Indicates whether attribute is present",
			},

Now when I am doing this

d.Get(“flag_presence”) gives false
d.GetOk(“flag_presence”) gives false and false
d.GetOkExists(“flag_presence”) gives false and true

I need some logic where I will check if the attribute is currently provided by the user and based on that I need to set it.
GetOkExists solves my problem but on using it I am getting a warning “Deprecated: usage is discouraged due to undefined behaviors and may be removed in a future version of the SDK”

Please suggest me an alternative. I am currently using this code logic

if flagPresenceOk, ok := d.GetOkExists("flag_presence"); ok {
		flagPresence := flagPresenceOk.(bool)
		if flagPresence {
			options.FlagPresence = &flagPresence
		}
	}

Hi! Just wanted to note that I saw there was a duplicate topic for this message and so I merged them together into this single topic, but that does make it look like @ramuklawjju just posted the same message here twice and so I want to be clear about what happened here, and that this duplicate was actually posted before the one at the top of this topic. :roll_eyes:

I have now moved this topic into the Plugin SDK category, so no need to start a new topic. Thanks!

Hello. You mentioned need to upgrade to newer terraform-plugin-framework, but could you please specify what is the replacement for GetOkExists?
Thanks

The framework provides the configuration values directly in the Create and Update calls. For plan time changes, AttributePlanModifiers can access all of the prior state, the configuration, and the proposed new state.

But how should we check if the boolean value is present in configuration?
To quote OP,
d.Get(“flag_presence”) gives false
d.GetOk(“flag_presence”) gives false and false

both d.Get and d.GetOk give same results both in case when value is present and the value if false and when value is absent from configuration. What is the way to distinguish these two cases?

Sorry @schirevko, I thought you were asking about the terraform-plugin-framework. There is no direct replacement for GetOkExists in the legacy SDK, because the information is not there to get. The most recent SDK version did have an experimental method added called GetRawConfig you can try, which bypasses the SDK internals.