Skip to content

Commit deb921c

Browse files
authored
Merge pull request #31606 from hashicorp/jbardin/backport-31576
v1.2 backport of 31576
2 parents 248c22d + 4e89fa7 commit deb921c

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

internal/configs/configschema/validate_traversal.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,34 @@ func (b *Block) StaticValidateTraversal(traversal hcl.Traversal) tfdiags.Diagnos
7474
}
7575

7676
if attrS, exists := b.Attributes[name]; exists {
77+
// Check for Deprecated status of this attribute.
78+
// We currently can't provide the user with any useful guidance because
79+
// the deprecation string is not part of the schema, but we can at
80+
// least warn them.
81+
//
82+
// This purposely does not attempt to recurse into nested attribute
83+
// types. Because nested attribute values are often not accessed via a
84+
// direct traversal to the leaf attributes, we cannot reliably detect
85+
// if a nested, deprecated attribute value is actually used from the
86+
// traversal alone. More precise detection of deprecated attributes
87+
// would require adding metadata like marks to the cty value itself, to
88+
// be caught during evaluation.
89+
if attrS.Deprecated {
90+
diags = diags.Append(&hcl.Diagnostic{
91+
Severity: hcl.DiagWarning,
92+
Summary: `Deprecated attribute`,
93+
Detail: fmt.Sprintf(`The attribute %q is deprecated. Refer to the provider documentation for details.`, name),
94+
Subject: next.SourceRange().Ptr(),
95+
})
96+
}
97+
7798
// For attribute validation we will just apply the rest of the
78-
// traversal to an unknown value of the attribute type and pass through
79-
// HCL's own errors, since we don't want to replicate all of HCL's type
80-
// checking rules here.
99+
// traversal to an unknown value of the attribute type and pass
100+
// through HCL's own errors, since we don't want to replicate all
101+
// of HCL's type checking rules here.
81102
val := cty.UnknownVal(attrS.ImpliedType())
82103
_, hclDiags := after.TraverseRel(val)
83-
diags = diags.Append(hclDiags)
84-
return diags
104+
return diags.Append(hclDiags)
85105
}
86106

87107
if blockS, exists := b.BlockTypes[name]; exists {

internal/configs/configschema/validate_traversal_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import (
1010

1111
func TestStaticValidateTraversal(t *testing.T) {
1212
attrs := map[string]*Attribute{
13-
"str": {Type: cty.String, Optional: true},
14-
"list": {Type: cty.List(cty.String), Optional: true},
15-
"dyn": {Type: cty.DynamicPseudoType, Optional: true},
13+
"str": {Type: cty.String, Optional: true},
14+
"list": {Type: cty.List(cty.String), Optional: true},
15+
"dyn": {Type: cty.DynamicPseudoType, Optional: true},
16+
"deprecated": {Type: cty.String, Computed: true, Deprecated: true},
1617
"nested_single": {
1718
Optional: true,
1819
NestedType: &Object{
@@ -220,6 +221,10 @@ func TestStaticValidateTraversal(t *testing.T) {
220221
`obj.nested_map["key"].optional`,
221222
``,
222223
},
224+
{
225+
`obj.deprecated`,
226+
`Deprecated attribute: The attribute "deprecated" is deprecated. Refer to the provider documentation for details.`,
227+
},
223228
}
224229

225230
for _, test := range tests {
@@ -239,8 +244,9 @@ func TestStaticValidateTraversal(t *testing.T) {
239244
t.Errorf("unexpected error: %s", diags.Err().Error())
240245
}
241246
} else {
242-
if diags.HasErrors() {
243-
if got := diags.Err().Error(); got != test.WantError {
247+
err := diags.ErrWithWarnings()
248+
if err != nil {
249+
if got := err.Error(); got != test.WantError {
244250
t.Errorf("wrong error\ngot: %s\nwant: %s", got, test.WantError)
245251
}
246252
} else {

0 commit comments

Comments
 (0)