Here is the link to repo: terraform-provider-netscalersdx/internal/mpsuser/resource_schema.go at master · netscaler/terraform-provider-netscalersdx · GitHub
Here is my config:
Schema:
"external_authentication": schema.BoolAttribute{
Optional: true,
Computed: true,
Description: "Enable external authentication.",
MarkdownDescription: "Enable external authentication.",
},
json-model(struct)
ExternalAuthentication *bool `json:"external_authentication,omitempty"`
While constructing the payload
ExternalAuthentication: data.ExternalAuthentication.ValueBoolPointer(),
If the attribute is not computed then it works fine, but if it is not, then it passes false to the payload.
How can I deal with the bool attribute that is computed and I don’t want it to be passed to payload if it is not specified in the terraform configuration.
Check out the basetypes.BoolValue
struct and its ValueBoolPointer()
function:
// BoolValue represents a boolean value.
type BoolValue struct {
// state represents whether the value is null, unknown, or known. The
// zero-value is null.
state attr.ValueState
// value contains the known value, if not null or unknown.
value bool
}
// ...snip...
// ValueBoolPointer returns a pointer to the known bool value, nil for a null
// value, or a pointer to false for an unknown value.
func (b BoolValue) ValueBoolPointer() *bool {
if b.IsNull() {
return nil
}
return &b.value
}
You’ll only get nil
when data.ExternalAuthentication.IsNull()
. Otherwise you get the contents of the struct’s value
attribute.
In your case, I think you’ll find that IsNull()
returns false but IsUnknown()
returns true, so you’re getting the zero value for the value
struct element.
It sounds like you want to check that !data.ExternalAuthentication.IsUnknown()
before reaching for the value inside.
1 Like