I am currently working on building a Terraform Provider (doesn’t use the Terraform Plugin Framework, instead the legacy SDK terraform-plugin-sdk/v2 v2.26.1
) in which I would like to fix a problem I’m seeing with a resource.
In the resource, I have a bunch of attributes of the TypeString
datatype. From the customer’s end, when the configuration of the resource is written, these attributes can either be specified as valid non empty strings, or as empty strings ""
, or do not need to be specified at all.
What I would like to achieve is two fold (both of these are needed to flag some cases I need in my project)
- (1) to add a function in the
read
method of the resource (which shall be called uponterraform plan
s subsequently run after the firstterraform apply
) which can identify if these attributes are specified as empty strings in the configuration of the resource, but not when they’re not specified in the configuration at all. - (2) in addition to this, this function would also need to compare how the attributes were specified upon the first
terraform apply
(for example, flag cases in which the attribute was earlier not specified in the configuration at all, and thenterraform apply
was done, after which this attribute was added to the configuration with the value “”).
First Problem (1)
Upon using functions such as d.Get()
, d.GetOk
, d.GetChange
(where d
is an object of type *schema.ResourceData
) in the read
function of the resource, they don’t seem to get the value of this attribute from the configuration of the resource specified by the customer, like they do when we use them in create function with d.Get()
.
However, my requirement is to have a function which can directly get the value of an attribute from the configuration of the resource (and not from the state). I don’t seem to find an exclusive function which can get values of attributes only from the configuration specified by the customer. Such a function will help me find if the current value of the attribute in the configuration is an empty string, or if the attribute does not exist at all.
Second Problem (2)
In order to find the existing value of these attributes in the state, interestingly, what I see is when the attributes are not specified in the configuration of the resource and terraform apply
works, they are saved as null
in the state; however, if they are actually specified as empty strings and terraform apply
works, they are saved as empty strings in the state.
However, when I use functions such as d.GetOk()
or d.Get()
or d.GetChange()
, in all cases, Terraform seems to not be able to identify the difference between both cases; if the attribute in the state has a null value, or if it has an empty string value.
This is understandable, because all of the functions I’ve stated above return interfaces, and for an interface which does not have a rigid type, be it an empty string or null, everything is the same. However, I need something to suffice my requirement here too.
The closest I’ve been able to get to fix the second issue is by using a function called d.State()
. This seems to be printing the state of the resource, but not those attributes which have null values (though it does seem to print attributes which have empty string values), so I might still be able to use this solve my problem, but if there is no other alternative.
Help, please!
Would really appreciate if Terraform experts can help me with this 🙂 thank you in advance!