mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-28 18:01:01 -06:00
708003b035
Although addrs.Target can in principle capture the information we need to represent move endpoints, it's semantically confusing because addrs.Targetable uses addrs.Abs... types which are typically for absolute addresses, but we were using them for relative addresses here. We now have specialized address types for representing moves and probably other things which have similar requirements later on. These types largely communicate the same information in the end, but aim to do so in a way that's explicit about which addresses are relative and which are absolute, to make it less likely that we'd inadvertently misuse these addresses.
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package configs
|
|
|
|
import (
|
|
"github.com/hashicorp/hcl/v2"
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
)
|
|
|
|
type Moved struct {
|
|
From *addrs.MoveEndpoint
|
|
To *addrs.MoveEndpoint
|
|
|
|
DeclRange hcl.Range
|
|
}
|
|
|
|
func decodeMovedBlock(block *hcl.Block) (*Moved, hcl.Diagnostics) {
|
|
var diags hcl.Diagnostics
|
|
moved := &Moved{
|
|
DeclRange: block.DefRange,
|
|
}
|
|
|
|
content, moreDiags := block.Body.Content(movedBlockSchema)
|
|
diags = append(diags, moreDiags...)
|
|
|
|
if attr, exists := content.Attributes["from"]; exists {
|
|
from, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr)
|
|
diags = append(diags, traversalDiags...)
|
|
if !traversalDiags.HasErrors() {
|
|
from, fromDiags := addrs.ParseMoveEndpoint(from)
|
|
diags = append(diags, fromDiags.ToHCL()...)
|
|
moved.From = from
|
|
}
|
|
}
|
|
|
|
if attr, exists := content.Attributes["to"]; exists {
|
|
to, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr)
|
|
diags = append(diags, traversalDiags...)
|
|
if !traversalDiags.HasErrors() {
|
|
to, toDiags := addrs.ParseMoveEndpoint(to)
|
|
diags = append(diags, toDiags.ToHCL()...)
|
|
moved.To = to
|
|
}
|
|
}
|
|
|
|
// we can only move from a module to a module, resource to resource, etc.
|
|
if !diags.HasErrors() {
|
|
if !moved.From.MightUnifyWith(moved.To) {
|
|
// We can catch some obviously-wrong combinations early here,
|
|
// but we still have other dynamic validation to do at runtime.
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
Severity: hcl.DiagError,
|
|
Summary: "Invalid \"moved\" addresses",
|
|
Detail: "The \"from\" and \"to\" addresses must either both refer to resources or both refer to modules.",
|
|
Subject: &moved.DeclRange,
|
|
})
|
|
}
|
|
}
|
|
|
|
return moved, diags
|
|
}
|
|
|
|
var movedBlockSchema = &hcl.BodySchema{
|
|
Attributes: []hcl.AttributeSchema{
|
|
{
|
|
Name: "from",
|
|
Required: true,
|
|
},
|
|
{
|
|
Name: "to",
|
|
Required: true,
|
|
},
|
|
},
|
|
}
|