kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/configs/moved.go (about) 1 package configs 2 3 import ( 4 "github.com/hashicorp/hcl/v2" 5 "kubeform.dev/terraform-backend-sdk/addrs" 6 ) 7 8 type Moved struct { 9 From *addrs.MoveEndpoint 10 To *addrs.MoveEndpoint 11 12 DeclRange hcl.Range 13 } 14 15 func decodeMovedBlock(block *hcl.Block) (*Moved, hcl.Diagnostics) { 16 var diags hcl.Diagnostics 17 moved := &Moved{ 18 DeclRange: block.DefRange, 19 } 20 21 content, moreDiags := block.Body.Content(movedBlockSchema) 22 diags = append(diags, moreDiags...) 23 24 if attr, exists := content.Attributes["from"]; exists { 25 from, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr) 26 diags = append(diags, traversalDiags...) 27 if !traversalDiags.HasErrors() { 28 from, fromDiags := addrs.ParseMoveEndpoint(from) 29 diags = append(diags, fromDiags.ToHCL()...) 30 moved.From = from 31 } 32 } 33 34 if attr, exists := content.Attributes["to"]; exists { 35 to, traversalDiags := hcl.AbsTraversalForExpr(attr.Expr) 36 diags = append(diags, traversalDiags...) 37 if !traversalDiags.HasErrors() { 38 to, toDiags := addrs.ParseMoveEndpoint(to) 39 diags = append(diags, toDiags.ToHCL()...) 40 moved.To = to 41 } 42 } 43 44 // we can only move from a module to a module, resource to resource, etc. 45 if !diags.HasErrors() { 46 if !moved.From.MightUnifyWith(moved.To) { 47 // We can catch some obviously-wrong combinations early here, 48 // but we still have other dynamic validation to do at runtime. 49 diags = diags.Append(&hcl.Diagnostic{ 50 Severity: hcl.DiagError, 51 Summary: "Invalid \"moved\" addresses", 52 Detail: "The \"from\" and \"to\" addresses must either both refer to resources or both refer to modules.", 53 Subject: &moved.DeclRange, 54 }) 55 } 56 } 57 58 return moved, diags 59 } 60 61 var movedBlockSchema = &hcl.BodySchema{ 62 Attributes: []hcl.AttributeSchema{ 63 { 64 Name: "from", 65 Required: true, 66 }, 67 { 68 Name: "to", 69 Required: true, 70 }, 71 }, 72 }