I am building a Terraform Project that uses the old Terraform Plugin SDK v2 (not the new Plugin Framework). Recently, I've defined a validation function on an attribute in a resource:
"custom_attribute": {
Type: schema.TypeString,
Required: true,
Description: "Some Description",
ValidateFunc: validateCustomAttributeValue,
},
func validateCustomAttributeValue(val interface{}, key string) (warns []string, errs []error) {
customAttributeValue := val.(string)
// listValidCustomAttributeValues() returns a list of valid values of "custom_attribute"
listOfValidCustomAttributeValues := listValidCustomAttributeValues()
isValidCustomAttributeValue := slices.Contains(listOfValidCustomAttributeValues, customAttributeValue)
if !isValidCustomAttributeValue {
errs = append(errs, fmt.Errorf("some custom error"))
}
if customAttributeValue == "SPECIFIC_VALUE" {
warns = append(warns, `some custom warning`)
}
return warns, errs
}
I would like to write a similar function for a different attribute (say X
), but in the ValidateFunc
of X
, I would like the ability to check the values of other attributes in the resource and based on those values, return errors/warnings (basically, validate the value of X
based on values of other attributes in the resource).
To do this, the signature of the ValidateFunc
I've used in the example above does not allow me to use d *schema.ResourceData
as an argument, and any workaround I try in this regard does not work; but I am badly in need of using d *schema.ResourceData
somewhere in the ValidateFunc
.
May I get some ideas on how I can get this working?