terraform/internal/deprecation/deprecation.go
Daniel Schmidt af549ab8c9
Some checks failed
Quick Checks / Unit Tests (push) Has been cancelled
Quick Checks / Race Tests (push) Has been cancelled
Quick Checks / End-to-end Tests (push) Has been cancelled
Quick Checks / Code Consistency Checks (push) Has been cancelled
add deprecation singleton
This struct tracks and validates deprecations in the context of a
graph walk. We need a struct to keep track of the module calls that
opt-out of deprecation warnings.
2026-01-27 13:58:52 +01:00

98 lines
2.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package deprecation
import (
"sync"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/terraform/internal/lang/marks"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/zclconf/go-cty/cty"
)
// Deprecations keeps track of meta-information related to deprecation, e.g. which module calls
// suppress deprecation warnings.
type Deprecations struct {
// Must hold this lock when accessing all fields after this one.
mu sync.Mutex
suppressedModules addrs.Set[addrs.Module]
}
func NewDeprecations() *Deprecations {
return &Deprecations{
suppressedModules: addrs.MakeSet[addrs.Module](),
}
}
func (d *Deprecations) SuppressModuleCallDeprecation(addr addrs.Module) {
d.mu.Lock()
defer d.mu.Unlock()
d.suppressedModules.Add(addr)
}
func (d *Deprecations) Validate(value cty.Value, module addrs.Module, rng *hcl.Range) (cty.Value, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
deprecationMarks := marks.GetDeprecationMarks(value)
if len(deprecationMarks) == 0 {
return value, diags
}
notDeprecatedValue := marks.RemoveDeprecationMarks(value)
// Check if we need to suppress deprecation warnings for this module call.
if d.IsModuleCallDeprecationSuppressed(module) {
return notDeprecatedValue, diags
}
for _, depMark := range deprecationMarks {
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagWarning,
Summary: "Deprecated value used",
Detail: depMark.Message,
Subject: rng,
})
}
return notDeprecatedValue, diags
}
func (d *Deprecations) ValidateAsConfig(value cty.Value, module addrs.Module) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
_, pvms := value.UnmarkDeepWithPaths()
if len(pvms) == 0 || d.IsModuleCallDeprecationSuppressed(module) {
return diags
}
for _, pvm := range pvms {
for m := range pvm.Marks {
if depMark, ok := m.(marks.DeprecationMark); ok {
diags = diags.Append(
tfdiags.AttributeValue(
tfdiags.Warning,
"Deprecated value used",
depMark.Message,
pvm.Path,
),
)
}
}
}
return diags
}
func (d *Deprecations) IsModuleCallDeprecationSuppressed(addr addrs.Module) bool {
d.mu.Lock()
defer d.mu.Unlock()
for _, mod := range d.suppressedModules {
if mod.TargetContains(addr) {
return true
}
}
return false
}