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 4 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")
}
}
5 changes: 3 additions & 2 deletions internal/plans/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,16 +615,17 @@ func (c *Change) Encode(ty cty.Type) (*ChangeSrc, error) {
unmarkedAfter, marksesAfter := c.After.UnmarkDeepWithPaths()
sensitiveAttrsBefore, unsupportedMarksesBefore := marks.PathsWithMark(marksesBefore, marks.Sensitive)
sensitiveAttrsAfter, unsupportedMarksesAfter := marks.PathsWithMark(marksesAfter, marks.Sensitive)

if len(unsupportedMarksesBefore) != 0 {
return nil, fmt.Errorf(
"prior value %s: can't serialize value marked with %#v (this is a bug in Terraform)",
"prior value %s: cannot serialize value marked with %#v (this is a bug in Terraform)",
tfdiags.FormatCtyPath(unsupportedMarksesBefore[0].Path),
unsupportedMarksesBefore[0].Marks,
)
}
if len(unsupportedMarksesAfter) != 0 {
return nil, fmt.Errorf(
"new value %s: can't serialize value marked with %#v (this is a bug in Terraform)",
"new value %s: cannot serialize value marked with %#v (this is a bug in Terraform)",
tfdiags.FormatCtyPath(unsupportedMarksesAfter[0].Path),
unsupportedMarksesAfter[0].Marks,
)
Expand Down
2 changes: 1 addition & 1 deletion internal/stacks/stackplan/planned_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ func DynamicValueToTerraform1(val cty.Value, ty cty.Type) (*stacks.DynamicValue,
sensitivePaths, withOtherMarks := marks.PathsWithMark(markPaths, marks.Sensitive)
if len(withOtherMarks) != 0 {
return nil, withOtherMarks[0].Path.NewErrorf(
"can't serialize value marked with %#v (this is a bug in Terraform)",
"cannot serialize value marked with %#v (this is a bug in Terraform)",
withOtherMarks[0].Marks,
)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/context_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ The -target option is not for routine use, and is provided only for exceptional
panic("nil plan but no errors")
}

if plan != nil {
if plan != nil && plan.Changes != nil {
relevantAttrs, rDiags := c.relevantResourceAttrsForPlan(config, plan)
diags = diags.Append(rDiags)
plan.RelevantAttributes = relevantAttrs
Expand Down
233 changes: 233 additions & 0 deletions internal/terraform/context_validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3094,3 +3094,236 @@ 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
}

output "test_output_conditional" {
value = false ? module.mod.old : module.mod.new
}

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)
}

func TestContext2Validate_deprecated_output_conflicts(t *testing.T) {
m := testModuleInline(t, map[string]string{
"mod/mod/main.tf": `
output "old" {
deprecated = "mod/mod: Please stop using this"
value = "old"
}
`,
"mod/main.tf": `
output "old" {
deprecated = "mod: Please stop using this"
value = "old"
}

module "mod" {
source = "./mod"
}

output "new" {
value = module.mod.old
}
`,
"mod2/main.tf": `
output "old" {
deprecated = "mod2: Please stop using this"
value = "old"
}

output "new" {
value = "new"
}
`,
"main.tf": `
module "mod" {
source = "./mod"
}

output "test_output" {
value = module.mod.old
}

module "mod2" {
count = 2
source = "./mod2"
}

output "test_output2" {
value = module.mod2[*].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: "mod: Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 7, Column: 10, Byte: 72},
End: hcl.Pos{Line: 7, Column: 24, Byte: 86},
},
},
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "mod2: Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "main.tf"),
Start: hcl.Pos{Line: 15, Column: 10, Byte: 161},
End: hcl.Pos{Line: 15, Column: 24, Byte: 176},
},
},
&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Usage of deprecated output",
Detail: "mod/mod: Please stop using this",
Subject: &hcl.Range{
Filename: filepath.Join(m.Module.SourceDir, "mod", "main.tf"),
Start: hcl.Pos{Line: 12, Column: 10, Byte: 150},
End: hcl.Pos{Line: 12, Column: 24, Byte: 164},
},
},
)

assertDiagnosticsMatch(t, diags, expectedDiags)
}
4 changes: 4 additions & 0 deletions internal/terraform/eval_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ type EvalContext interface {
// Forget if set to true will cause the plan to forget all resources. This is
// only allowed in the context of a destroy plan.
Forget() bool

// ReferencableDeprecationMessage returns the deprecation message for the referencable
ReferencableDeprecationMessage(addrs.Module, addrs.Referenceable) (string, bool)
MarkReferencableAsDeprecated(addrs.ConfigOutputValue, string)
}

func evalContextForModuleInstance(baseCtx EvalContext, addr addrs.ModuleInstance) EvalContext {
Expand Down
26 changes: 26 additions & 0 deletions internal/terraform/eval_context_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type BuiltinEvalContext struct {
InstanceExpanderValue *instances.Expander
MoveResultsValue refactoring.MoveResults
OverrideValues *mocking.Overrides
DeprecatedReferencables map[string]string
}

// BuiltinEvalContext implements EvalContext
Expand Down Expand Up @@ -612,3 +613,28 @@ func (ctx *BuiltinEvalContext) Forget() bool {
func (ctx *BuiltinEvalContext) EphemeralResources() *ephemeral.Resources {
return ctx.EphemeralResourcesValue
}

func (ctx *BuiltinEvalContext) ReferencableDeprecationMessage(m addrs.Module, x addrs.Referenceable) (string, bool) {
// TODO: We want to make this available in node_resource_validation, therefore we want to talk about config objects, the referencable should somehow contain the config object, how do we get it out?
if foo, ok := x.(addrs.ModuleCallInstanceOutput); ok {
cov := addrs.ConfigOutputValue{
Module: m,
OutputValue: addrs.OutputValue{
Name: foo.Name,
},
}

fmt.Printf("\n\t ctx.DeprecatedReferencables --> %#v \n", ctx.DeprecatedReferencables)
fmt.Printf("\n\t cov.String() --> %#v \n", cov.String())
msg, ok := ctx.DeprecatedReferencables[cov.String()]
return msg, ok
}

return "", false
}

func (ctx *BuiltinEvalContext) MarkReferencableAsDeprecated(x addrs.ConfigOutputValue, msg string) {
fmt.Printf("\n\t marking x --> %#v \n", x)
fmt.Printf("\n\t msg --> %#v \n", msg)
ctx.DeprecatedReferencables[x.String()] = msg
}
Loading
Loading