You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Technically speaking, implementing the error checking for every attribute should be done to notify operators about the potential loss of drift detection and cover missing acceptance testing data scenarios. However for pragmatic purposes such as code readability and that acceptance testing coverage, many providers consider the error checking as optional for "simple" data types such as TypeString (string), instead suggesting it for "complex" types such as TypeList/TypeMap/TypeSet. The choice of whether or not to apply the error checking can be confusing for developers and the implementations can vary.
Especially since Set() returns an error type instead of one directly append-able to diag.Diagnostics, the ergonomics can be a little awkward and the implementations can very, even across resources within a single provider.
This issue to discuss if there is potential for simplifying this developer experience and then recommending a standardized implementation.
Attempted Solutions
Not performing the error checking or enforcing it via static analysis.
Proposal
Proposal 1
Implementing a diagnostic wrapper function with a consistent diagnostic:
funcSetDiag(errerror) diag.Diagnostic {
iferr==nil {
returnnil
}
return diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to update Terraform State value",
Detail: fmt.Sprintf("For read-only attributes, the value will not be present. For writeable attributes, drift detection for this attribute will not work as expected. This is a bug that should be reported to the Terraform Provider. Error details: %s", err.Error()),
}
}
Taking proposal 1 further and instead create a separate SetWithDiag() (up for bikeshedding the name) receiver method that can be directly used.
func (d*ResourceData) SetWithDiag(keystring, valueinterface{}) diag.Diagnostic {
err:=d.Set(key, value)
iferr==nil {
returnnil
}
return diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to update Terraform State value",
Detail: fmt.Sprintf("For read-only attributes, the value will not be present. For writeable attributes, drift detection for this attribute will not work as expected. This is a bug that should be reported to the Terraform Provider. Attribute %q received error: %s", key, err.Error()),
}
}
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
SDK version
Use-cases
Resources that need to write to the Terraform State use the
(helper/schema.ResourceData).Set()
receiver method. e.g.Technically speaking, implementing the error checking for every attribute should be done to notify operators about the potential loss of drift detection and cover missing acceptance testing data scenarios. However for pragmatic purposes such as code readability and that acceptance testing coverage, many providers consider the error checking as optional for "simple" data types such as
TypeString
(string
), instead suggesting it for "complex" types such asTypeList
/TypeMap
/TypeSet
. The choice of whether or not to apply the error checking can be confusing for developers and the implementations can vary.Especially since
Set()
returns anerror
type instead of one directly append-able todiag.Diagnostics
, the ergonomics can be a little awkward and the implementations can very, even across resources within a single provider.This issue to discuss if there is potential for simplifying this developer experience and then recommending a standardized implementation.
Attempted Solutions
Not performing the error checking or enforcing it via static analysis.
Proposal
Proposal 1
Implementing a diagnostic wrapper function with a consistent diagnostic:
Proposal 2
Taking proposal 1 further and instead create a separate
SetWithDiag()
(up for bikeshedding the name) receiver method that can be directly used.References
The text was updated successfully, but these errors were encountered: