You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
terraform/internal/configs/removed.go

83 lines
1.9 KiB

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package configs
import (
"github.com/hashicorp/terraform/internal/addrs"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
)
// Removed describes the contents of a "removed" block in configuration.
type Removed struct {
// From is the address of the configuration object being removed.
From *addrs.RemoveTarget
// Destroy indicates that the resource should be destroyed, not just removed
// from state. Defaults to true.
Destroy bool
DeclRange hcl.Range
}
func decodeRemovedBlock(block *hcl.Block) (*Removed, hcl.Diagnostics) {
var diags hcl.Diagnostics
removed := &Removed{
DeclRange: block.DefRange,
}
content, moreDiags := block.Body.Content(removedBlockSchema)
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.ParseRemoveTarget(from)
diags = append(diags, fromDiags.ToHCL()...)
removed.From = from
}
}
removed.Destroy = true
for _, block := range content.Blocks {
switch block.Type {
case "lifecycle":
lcContent, lcDiags := block.Body.Content(removedLifecycleBlockSchema)
diags = append(diags, lcDiags...)
if attr, exists := lcContent.Attributes["destroy"]; exists {
valDiags := gohcl.DecodeExpression(attr.Expr, nil, &removed.Destroy)
diags = append(diags, valDiags...)
}
}
}
return removed, diags
}
var removedBlockSchema = &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{
Name: "from",
Required: true,
},
},
Blocks: []hcl.BlockHeaderSchema{
{
Type: "lifecycle",
},
},
}
var removedLifecycleBlockSchema = &hcl.BodySchema{
Attributes: []hcl.AttributeSchema{
{
Name: "destroy",
},
},
}