Skip to content

Commit

Permalink
Add diagnostic for deprecated outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanck committed Nov 15, 2024
1 parent 192ace8 commit 9d96887
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
119 changes: 119 additions & 0 deletions internal/terraform/context_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3094,3 +3094,122 @@ module "child" {
diags := ctx.Validate(m, &ValidateOpts{})
assertNoDiagnostics(t, diags)
}

func TestContext2Validate_deprecated_output(t *testing.T) {
m := testModuleInline(t, map[string]string{
"mod/main.tf": `
output "old" {
deprecated = "Please stop using this"
value = "old"
}
output "old-and-unused" {
deprecated = "This should not show up in the errors, we are not using it"
value = "old"
}
output "new" {
value = "foo"
}
`,
"mod2/main.tf": `
variable "input" {
type = string
}
`,
"main.tf": `
module "mod" {
source = "./mod"
}
resource "test_resource" "test" {
attr = module.mod.old
}
resource "test_resource" "test2" {
attr = module.mod.new
}
resource "test_resource" "test3" {
attr = module.mod.old
}
output "test_output" {
value = module.mod.old
}
module "mod2" {
source = "./mod2"
input = module.mod.old
}
`,
})

p := new(testing_provider.MockProvider)
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
ResourceTypes: map[string]*configschema.Block{
"test_resource": {
Attributes: map[string]*configschema.Attribute{
"attr": {
Type: cty.String,
Computed: true,
},
},
},
},
})

ctx := testContext2(t, &ContextOpts{
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})

diags := ctx.Validate(m, &ValidateOpts{})
var expectedDiags tfdiags.Diagnostics
expectedDiags = expectedDiags.Append(
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 7, Column: 12, Byte: 85},
End: hcl.Pos{Line: 7, Column: 26, Byte: 99},
},
},
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 15, Column: 12, Byte: 213},
End: hcl.Pos{Line: 15, Column: 26, Byte: 227},
},
},
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 19, Column: 10, Byte: 263},
End: hcl.Pos{Line: 19, Column: 24, Byte: 277},
},
},
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 25, Column: 10, Byte: 326},
End: hcl.Pos{Line: 25, Column: 24, Byte: 340},
},
},
)

assertDiagnosticsMatch(t, diags, expectedDiags)
}
11 changes: 11 additions & 0 deletions internal/terraform/node_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,17 @@ func (n *NodeApplyableOutput) References() []*addrs.Reference {

// GraphNodeExecutable
func (n *NodeApplyableOutput) Execute(ctx EvalContext, op walkOperation) (diags tfdiags.Diagnostics) {
if op == walkValidate && n.Config.DeprecatedSet && len(n.Dependants) > 0 {
for _, d := range n.Dependants {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: n.Config.Deprecated,
Subject: d.SourceRange.ToHCL().Ptr(),
})
}
}

state := ctx.State()
if state == nil {
return
Expand Down

0 comments on commit 9d96887

Please sign in to comment.