Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecation Warning For Outputs #36016

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/configs/named_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,14 @@ type Output struct {
DependsOn []hcl.Traversal
Sensitive bool
Ephemeral bool
Deprecated string

Preconditions []*CheckRule

DescriptionSet bool
SensitiveSet bool
EphemeralSet bool
DeprecatedSet bool

DeclRange hcl.Range
}
Expand Down Expand Up @@ -402,6 +404,12 @@ func decodeOutputBlock(block *hcl.Block, override bool) (*Output, hcl.Diagnostic
o.EphemeralSet = true
}

if attr, exists := content.Attributes["deprecated"]; exists {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &o.Deprecated)
diags = append(diags, valDiags...)
o.DeprecatedSet = true
}

if attr, exists := content.Attributes["depends_on"]; exists {
deps, depsDiags := DecodeDependsOn(attr)
diags = append(diags, depsDiags...)
Expand Down Expand Up @@ -525,6 +533,9 @@ var outputBlockSchema = &hcl.BodySchema{
{
Name: "ephemeral",
},
{
Name: "deprecated",
},
},
Blocks: []hcl.BlockHeaderSchema{
{Type: "precondition"},
Expand Down
27 changes: 27 additions & 0 deletions internal/configs/named_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,30 @@ func TestVariableInvalidDefault(t *testing.T) {
}
}
}

func TestOutputDeprecation(t *testing.T) {
src := `
output "foo" {
value = "bar"
deprecated = "This output is deprecated"
}
`

hclF, diags := hclsyntax.ParseConfig([]byte(src), "test.tf", hcl.InitialPos)
if diags.HasErrors() {
t.Fatal(diags.Error())
}

b, diags := parseConfigFile(hclF.Body, nil, false, false)
if diags.HasErrors() {
t.Fatalf("unexpected error: %q", diags)
}

if !b.Outputs[0].DeprecatedSet {
t.Fatalf("expected output to be deprecated")
}

if b.Outputs[0].Deprecated != "This output is deprecated" {
t.Fatalf("expected output to have deprecation message")
}
}
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)
}
2 changes: 2 additions & 0 deletions internal/terraform/graph_builder_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ func (b *ApplyGraphBuilder) Steps() []GraphTransformer {
&ReferenceTransformer{},
&AttachDependenciesTransformer{},

&OutputReferencesTransformer{},

// Nested data blocks should be loaded after every other resource has
// done its thing.
&checkStartTransformer{Config: b.Config, Operation: b.Operation},
Expand Down
2 changes: 2 additions & 0 deletions internal/terraform/graph_builder_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func (b *PlanGraphBuilder) Steps() []GraphTransformer {

&ReferenceTransformer{},

&OutputReferencesTransformer{},

&AttachDependenciesTransformer{},

// Make sure data sources are aware of any depends_on from the
Expand Down
16 changes: 16 additions & 0 deletions internal/terraform/node_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type nodeExpandOutput struct {
Overrides *mocking.Overrides

Dependencies []addrs.ConfigResource

Dependants []*addrs.Reference
}

var (
Expand Down Expand Up @@ -136,6 +138,7 @@ func (n *nodeExpandOutput) DynamicExpand(ctx EvalContext) (*Graph, tfdiags.Diagn
Planning: n.Planning,
Override: n.getOverrideValue(absAddr.Module),
Dependencies: n.Dependencies,
Dependants: n.Dependants,
}
}

Expand Down Expand Up @@ -283,6 +286,8 @@ type NodeApplyableOutput struct {
// Dependencies is the full set of resources that are referenced by this
// output.
Dependencies []addrs.ConfigResource

Dependants []*addrs.Reference
}

var (
Expand Down Expand Up @@ -379,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
1 change: 1 addition & 0 deletions internal/terraform/transform_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func (t *OutputTransformer) transform(g *Graph, c *configs.Config) error {
RefreshOnly: t.RefreshOnly,
Planning: t.Planning,
Overrides: t.Overrides,
Dependants: []*addrs.Reference{},
}

log.Printf("[TRACE] OutputTransformer: adding %s as %T", o.Name, node)
Expand Down
30 changes: 30 additions & 0 deletions internal/terraform/transform_output_references.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package terraform

// OutputReferencesTransformer is a GraphTransformer that adds meta-information
// about references to output values in the configuration. This is used to
// determine when deprecated outputs are used.
type OutputReferencesTransformer struct{}

func (t *OutputReferencesTransformer) Transform(g *Graph) error {
// Build a reference map so we can efficiently look up the references
vs := g.Vertices()
m := NewReferenceMap(vs)

// Find the things that reference things and connect them
for _, v := range vs {
if dependant, ok := v.(GraphNodeReferencer); ok {
parents := m.References(v)

for _, parent := range parents {
if output, ok := parent.(*nodeExpandOutput); ok {
output.Dependants = append(output.Dependants, dependant.References()...)
}
}
}
}

return nil
}
Loading