MCPcopy
hub / github.com/opentofu/opentofu / decodeMovedBlock

Function decodeMovedBlock

internal/configs/moved.go:20–81  ·  view source on GitHub ↗
(block *hcl.Block)

Source from the content-addressed store, hash-verified

18}
19
20func decodeMovedBlock(block *hcl.Block) (*Moved, hcl.Diagnostics) {
21 var diags hcl.Diagnostics
22 moved := &Moved{
23 DeclRange: block.DefRange,
24 }
25
26 content, moreDiags := block.Body.Content(movedBlockSchema)
27 diags = append(diags, moreDiags...)
28
29 if attr, exists := content.Attributes["from"]; exists {
30 from, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr)
31 diags = append(diags, traversalDiags...)
32 if !traversalDiags.HasErrors() {
33 from, fromDiags := addrs.ParseMoveEndpoint(from)
34 diags = append(diags, fromDiags.ToHCL()...)
35 moved.From = from
36 }
37 }
38
39 if attr, exists := content.Attributes["to"]; exists {
40 to, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr)
41 diags = append(diags, traversalDiags...)
42 if !traversalDiags.HasErrors() {
43 to, toDiags := addrs.ParseMoveEndpoint(to)
44 diags = append(diags, toDiags.ToHCL()...)
45 moved.To = to
46 }
47 }
48 // ensure that the moved block is not used against ephemeral resources since there is no use against those
49 if !moved.From.SubjectAllowed() {
50 diags = diags.Append(&hcl.Diagnostic{
51 Severity: hcl.DiagError,
52 Summary: "Invalid \"moved\" address",
53 Detail: "The resource referenced by the \"from\" attribute is not allowed to be moved.",
54 Subject: &moved.DeclRange,
55 })
56 }
57 if !moved.To.SubjectAllowed() {
58 diags = diags.Append(&hcl.Diagnostic{
59 Severity: hcl.DiagError,
60 Summary: "Invalid \"moved\" address",
61 Detail: "The resource referenced by the \"to\" attribute is not allowed to be moved.",
62 Subject: &moved.DeclRange,
63 })
64 }
65
66 // we can only move from a module to a module, resource to resource, etc.
67 if !diags.HasErrors() {
68 if !moved.From.MightUnifyWith(moved.To) {
69 // We can catch some obviously-wrong combinations early here,
70 // but we still have other dynamic validation to do at runtime.
71 diags = diags.Append(&hcl.Diagnostic{
72 Severity: hcl.DiagError,
73 Summary: "Invalid \"moved\" addresses",
74 Detail: "The \"from\" and \"to\" addresses must either both refer to resources or both refer to modules.",
75 Subject: &moved.DeclRange,
76 })
77 }

Callers 2

TestMovedBlock_decodeFunction · 0.85
loadConfigFileBodyFunction · 0.85

Calls 7

HasErrorsMethod · 0.95
AppendMethod · 0.95
ParseMoveEndpointFunction · 0.92
SubjectAllowedMethod · 0.80
MightUnifyWithMethod · 0.80
ContentMethod · 0.45
ToHCLMethod · 0.45

Tested by 1

TestMovedBlock_decodeFunction · 0.68