Skip to content

Commit 385d466

Browse files
resource deprecation
1 parent 8563394 commit 385d466

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

internal/configs/configschema/validate_traversal.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ func (b *Block) StaticValidateTraversal(traversal hcl.Traversal) tfdiags.Diagnos
8989
// traversal alone. More precise detection of deprecated attributes
9090
// would require adding metadata like marks to the cty value itself, to
9191
// be caught during evaluation.
92+
//
93+
// For all other kinds of deprecations we have marks.Deprecation, but since
94+
// we return an unknown value here, we can not attach marks to it.
9295
if attrS.Deprecated {
9396
diags = diags.Append(&hcl.Diagnostic{
9497
Severity: hcl.DiagWarning,

internal/terraform/context_validate_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,3 +4415,65 @@ module "sink" {
44154415
},
44164416
}))
44174417
}
4418+
4419+
func TestContext2Validate_deprecated_resource(t *testing.T) {
4420+
m := testModuleInline(t, map[string]string{
4421+
"main.tf": `
4422+
resource "test_resource" "test" { # WARNING
4423+
attr = "value"
4424+
}
4425+
4426+
output "a" {
4427+
value = test_resource.test.attr # WARNING
4428+
}
4429+
4430+
output "b" {
4431+
value = test_resource.test.attr # OK
4432+
deprecated = "redeprecated output"
4433+
}
4434+
`,
4435+
})
4436+
4437+
p := new(testing_provider.MockProvider)
4438+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
4439+
ResourceTypes: map[string]*configschema.Block{
4440+
"test_resource": {
4441+
Deprecated: true,
4442+
Attributes: map[string]*configschema.Attribute{
4443+
"attr": {
4444+
Type: cty.String,
4445+
Computed: true,
4446+
},
4447+
},
4448+
},
4449+
},
4450+
})
4451+
4452+
ctx := testContext2(t, &ContextOpts{
4453+
Providers: map[addrs.Provider]providers.Factory{
4454+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
4455+
},
4456+
})
4457+
4458+
diags := ctx.Validate(m, &ValidateOpts{})
4459+
4460+
tfdiags.AssertDiagnosticsMatch(t, diags, tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{
4461+
Severity: hcl.DiagWarning,
4462+
Summary: `Usage of deprecated resource "test_resource"`,
4463+
Detail: `The resource "test_resource" has been marked as deprecated by its provider. Please check the provider documentation for more information.`,
4464+
Subject: &hcl.Range{
4465+
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
4466+
Start: hcl.Pos{Line: 2, Column: 1, Byte: 1},
4467+
End: hcl.Pos{Line: 2, Column: 32, Byte: 32},
4468+
},
4469+
}).Append(&hcl.Diagnostic{
4470+
Severity: hcl.DiagWarning,
4471+
Summary: "Deprecated value used",
4472+
Detail: `Resource "test_resource" is deprecated`,
4473+
Subject: &hcl.Range{
4474+
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
4475+
Start: hcl.Pos{Line: 7, Column: 13, Byte: 92},
4476+
End: hcl.Pos{Line: 7, Column: 36, Byte: 115},
4477+
},
4478+
}))
4479+
}

internal/terraform/evaluate.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,11 @@ func (d *evaluationStateData) GetResource(addr addrs.Resource, rng tfdiags.Sourc
803803
// We should only end up here during the validate walk (or
804804
// console/eval), since later walks should have at least partial
805805
// states populated for all resources in the configuration.
806-
return cty.DynamicVal, diags
806+
ret := cty.DynamicVal
807+
if schema.Body.Deprecated {
808+
ret = ret.Mark(marks.NewDeprecation(fmt.Sprintf("Resource %q is deprecated", addr.Type), nil))
809+
}
810+
return ret, diags
807811
}
808812
}
809813

@@ -878,6 +882,10 @@ func (d *evaluationStateData) GetResource(addr addrs.Resource, rng tfdiags.Sourc
878882
ret = val
879883
}
880884

885+
if schema.Body.Deprecated {
886+
ret = ret.Mark(marks.NewDeprecation(fmt.Sprintf("Resource %q is deprecated", addr.Type), nil))
887+
}
888+
881889
return ret, diags
882890
}
883891

internal/terraform/node_resource_validate.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ func (n *NodeValidatableResource) Execute(ctx EvalContext, op walkOperation) (di
6969
}
7070
}
7171
}
72+
73+
if n.Schema != nil && n.Schema.Body != nil && n.Schema.Body.Deprecated {
74+
diags = diags.Append(&hcl.Diagnostic{
75+
Severity: hcl.DiagWarning,
76+
Summary: fmt.Sprintf("Usage of deprecated resource %q", n.Addr.Resource.Type),
77+
Detail: fmt.Sprintf("The resource %q has been marked as deprecated by its provider. Please check the provider documentation for more information.", n.Addr.Resource.Type),
78+
Subject: &n.Config.DeclRange,
79+
})
80+
}
81+
7282
return diags
7383
}
7484

0 commit comments

Comments
 (0)