Skip to content

Commit b347759

Browse files
diallow root output deprecation
1 parent 4870da4 commit b347759

File tree

3 files changed

+67
-11
lines changed

3 files changed

+67
-11
lines changed

internal/configs/named_values.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ type Output struct {
365365
EphemeralSet bool
366366
DeprecatedSet bool
367367

368-
DeclRange hcl.Range
368+
DeclRange hcl.Range
369+
DeprecatedRange hcl.Range
369370
}
370371

371372
func decodeOutputBlock(block *hcl.Block, override bool) (*Output, hcl.Diagnostics) {
@@ -419,6 +420,7 @@ func decodeOutputBlock(block *hcl.Block, override bool) (*Output, hcl.Diagnostic
419420
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &o.Deprecated)
420421
diags = append(diags, valDiags...)
421422
o.DeprecatedSet = true
423+
o.DeprecatedRange = attr.Range
422424
}
423425

424426
if attr, exists := content.Attributes["depends_on"]; exists {

internal/terraform/context_validate_test.go

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4151,10 +4151,6 @@ resource "test_resource" "test2" {
41514151
output "test_output_deprecated_use" {
41524152
value = module.mod.redeprecated # WARNING
41534153
}
4154-
output "test_output_deprecated_use_with_deprecation" {
4155-
deprecated = "This is displayed in the UI, but does not produce an additional warning"
4156-
value = module.mod.redeprecated # OK
4157-
}
41584154
`,
41594155
})
41604156

@@ -4426,11 +4422,6 @@ resource "test_resource" "test" { # WARNING
44264422
output "a" {
44274423
value = test_resource.test.attr # WARNING
44284424
}
4429-
4430-
output "b" {
4431-
value = test_resource.test.attr # OK
4432-
deprecated = "redeprecated output"
4433-
}
44344425
`,
44354426
})
44364427

@@ -4477,3 +4468,57 @@ output "b" {
44774468
},
44784469
}))
44794470
}
4471+
4472+
func TestContext2Validate_deprecated_root_output(t *testing.T) {
4473+
m := testModuleInline(t, map[string]string{
4474+
"mod/main.tf": `
4475+
output "old" {
4476+
deprecated = "Please stop using this"
4477+
value = "old"
4478+
}
4479+
`,
4480+
"main.tf": `
4481+
module "mod" {
4482+
source = "./mod"
4483+
}
4484+
4485+
output "test_output" {
4486+
deprecated = "Deprecating the root output is not ok" # ERROR
4487+
value = module.mod.old
4488+
}
4489+
`,
4490+
})
4491+
4492+
p := new(testing_provider.MockProvider)
4493+
p.GetProviderSchemaResponse = getProviderSchemaResponseFromProviderSchema(&providerSchema{
4494+
ResourceTypes: map[string]*configschema.Block{
4495+
"test_resource": {
4496+
Attributes: map[string]*configschema.Attribute{
4497+
"attr": {
4498+
Type: cty.String,
4499+
Computed: true,
4500+
},
4501+
},
4502+
},
4503+
},
4504+
})
4505+
4506+
ctx := testContext2(t, &ContextOpts{
4507+
Providers: map[addrs.Provider]providers.Factory{
4508+
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
4509+
},
4510+
})
4511+
4512+
diags := ctx.Validate(m, &ValidateOpts{})
4513+
4514+
tfdiags.AssertDiagnosticsMatch(t, diags, tfdiags.Diagnostics{}.Append(&hcl.Diagnostic{
4515+
Severity: hcl.DiagError,
4516+
Summary: "Root module output deprecated",
4517+
Detail: "Root module outputs cannot be deprecated, as there is no higher-level module to inform of the deprecation.",
4518+
Subject: &hcl.Range{
4519+
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
4520+
Start: hcl.Pos{Line: 7, Column: 5, Byte: 67},
4521+
End: hcl.Pos{Line: 7, Column: 57, Byte: 119},
4522+
},
4523+
}))
4524+
}

internal/terraform/node_output.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,16 @@ If you do intend to export this data, annotate the output value as sensitive by
530530
}
531531

532532
if n.Config.DeprecatedSet {
533-
val = val.Mark(marks.NewDeprecation(n.Config.Deprecated, &n.Config.DeclRange))
533+
if n.Addr.Module.IsRoot() {
534+
diags = diags.Append(&hcl.Diagnostic{
535+
Severity: hcl.DiagError,
536+
Summary: "Root module output deprecated",
537+
Detail: "Root module outputs cannot be deprecated, as there is no higher-level module to inform of the deprecation.",
538+
Subject: n.Config.DeprecatedRange.Ptr(),
539+
})
540+
} else {
541+
val = val.Mark(marks.NewDeprecation(n.Config.Deprecated, &n.Config.DeclRange))
542+
}
534543
}
535544

536545
n.setValue(ctx.NamedValues(), state, changes, ctx.Deferrals(), val)

0 commit comments

Comments
 (0)