2021-06-21 16:53:16 +02:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Moved struct {
|
2021-06-30 18:22:02 +02:00
|
|
|
From *addrs.MoveEndpoint
|
|
|
|
To *addrs.MoveEndpoint
|
2021-06-21 16:53:16 +02:00
|
|
|
|
|
|
|
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() {
|
2021-06-30 18:22:02 +02:00
|
|
|
from, fromDiags := addrs.ParseMoveEndpoint(from)
|
2021-06-21 16:53:16 +02:00
|
|
|
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() {
|
2021-06-30 18:22:02 +02:00
|
|
|
to, toDiags := addrs.ParseMoveEndpoint(to)
|
2021-06-21 16:53:16 +02:00
|
|
|
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() {
|
2021-06-30 18:22:02 +02:00
|
|
|
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.
|
2021-06-21 16:53:16 +02:00
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2021-06-30 18:22:02 +02:00
|
|
|
Summary: "Invalid \"moved\" addresses",
|
|
|
|
Detail: "The \"from\" and \"to\" addresses must either both refer to resources or both refer to modules.",
|
2021-06-21 16:53:16 +02:00
|
|
|
Subject: &moved.DeclRange,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return moved, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
var movedBlockSchema = &hcl.BodySchema{
|
|
|
|
Attributes: []hcl.AttributeSchema{
|
|
|
|
{
|
|
|
|
Name: "from",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "to",
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|