2018-02-03 02:22:25 +01:00
|
|
|
package configs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2018-02-03 02:22:25 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Provisioner represents a "provisioner" block when used within a
|
|
|
|
// "resource" block in a module or file.
|
|
|
|
type Provisioner struct {
|
|
|
|
Type string
|
|
|
|
Config hcl.Body
|
|
|
|
Connection *Connection
|
|
|
|
When ProvisionerWhen
|
|
|
|
OnFailure ProvisionerOnFailure
|
|
|
|
|
|
|
|
DeclRange hcl.Range
|
|
|
|
TypeRange hcl.Range
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeProvisionerBlock(block *hcl.Block) (*Provisioner, hcl.Diagnostics) {
|
|
|
|
pv := &Provisioner{
|
|
|
|
Type: block.Labels[0],
|
|
|
|
TypeRange: block.LabelRanges[0],
|
|
|
|
DeclRange: block.DefRange,
|
|
|
|
When: ProvisionerWhenCreate,
|
|
|
|
OnFailure: ProvisionerOnFailureFail,
|
|
|
|
}
|
|
|
|
|
|
|
|
content, config, diags := block.Body.PartialContent(provisionerBlockSchema)
|
|
|
|
pv.Config = config
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["when"]; exists {
|
2018-02-15 19:17:36 +01:00
|
|
|
expr, shimDiags := shimTraversalInString(attr.Expr, true)
|
|
|
|
diags = append(diags, shimDiags...)
|
|
|
|
|
|
|
|
switch hcl.ExprAsKeyword(expr) {
|
2018-02-03 02:22:25 +01:00
|
|
|
case "create":
|
|
|
|
pv.When = ProvisionerWhenCreate
|
|
|
|
case "destroy":
|
|
|
|
pv.When = ProvisionerWhenDestroy
|
|
|
|
default:
|
2018-02-15 19:17:36 +01:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid \"when\" keyword",
|
|
|
|
Detail: "The \"when\" argument requires one of the following keywords: create or destroy.",
|
|
|
|
Subject: expr.Range().Ptr(),
|
|
|
|
})
|
2018-02-03 02:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, exists := content.Attributes["on_failure"]; exists {
|
2018-02-15 19:17:36 +01:00
|
|
|
expr, shimDiags := shimTraversalInString(attr.Expr, true)
|
|
|
|
diags = append(diags, shimDiags...)
|
|
|
|
|
|
|
|
switch hcl.ExprAsKeyword(expr) {
|
2018-02-03 02:22:25 +01:00
|
|
|
case "continue":
|
|
|
|
pv.OnFailure = ProvisionerOnFailureContinue
|
|
|
|
case "fail":
|
|
|
|
pv.OnFailure = ProvisionerOnFailureFail
|
|
|
|
default:
|
2018-02-15 19:17:36 +01:00
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Invalid \"on_failure\" keyword",
|
|
|
|
Detail: "The \"on_failure\" argument requires one of the following keywords: continue or fail.",
|
|
|
|
Subject: attr.Expr.Range().Ptr(),
|
|
|
|
})
|
2018-02-03 02:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var seenConnection *hcl.Block
|
|
|
|
for _, block := range content.Blocks {
|
|
|
|
switch block.Type {
|
|
|
|
|
|
|
|
case "connection":
|
|
|
|
if seenConnection != nil {
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Duplicate connection block",
|
|
|
|
Detail: fmt.Sprintf("This provisioner already has a connection block at %s.", seenConnection.DefRange),
|
|
|
|
Subject: &block.DefRange,
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seenConnection = block
|
|
|
|
|
2018-05-29 20:58:28 +02:00
|
|
|
//conn, connDiags := decodeConnectionBlock(block)
|
|
|
|
//diags = append(diags, connDiags...)
|
|
|
|
pv.Connection = &Connection{
|
|
|
|
Config: block.Body,
|
|
|
|
DeclRange: block.DefRange,
|
|
|
|
}
|
2018-02-03 02:22:25 +01:00
|
|
|
|
|
|
|
default:
|
2018-11-20 20:53:45 +01:00
|
|
|
// Any other block types are ones we've reserved for future use,
|
|
|
|
// so they get a generic message.
|
|
|
|
diags = append(diags, &hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Reserved block type name in provisioner block",
|
|
|
|
Detail: fmt.Sprintf("The block type name %q is reserved for use by Terraform in a future version.", block.Type),
|
|
|
|
Subject: &block.TypeRange,
|
|
|
|
})
|
2018-02-03 02:22:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pv, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection represents a "connection" block when used within either a
|
|
|
|
// "resource" or "provisioner" block in a module or file.
|
|
|
|
type Connection struct {
|
|
|
|
Config hcl.Body
|
|
|
|
|
|
|
|
DeclRange hcl.Range
|
|
|
|
}
|
|
|
|
|
|
|
|
// ProvisionerWhen is an enum for valid values for when to run provisioners.
|
|
|
|
type ProvisionerWhen int
|
|
|
|
|
2019-10-17 22:17:23 +02:00
|
|
|
//go:generate go run golang.org/x/tools/cmd/stringer -type ProvisionerWhen
|
2018-02-03 02:22:25 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
ProvisionerWhenInvalid ProvisionerWhen = iota
|
|
|
|
ProvisionerWhenCreate
|
|
|
|
ProvisionerWhenDestroy
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProvisionerOnFailure is an enum for valid values for on_failure options
|
|
|
|
// for provisioners.
|
|
|
|
type ProvisionerOnFailure int
|
|
|
|
|
2019-10-17 22:17:23 +02:00
|
|
|
//go:generate go run golang.org/x/tools/cmd/stringer -type ProvisionerOnFailure
|
2018-02-03 02:22:25 +01:00
|
|
|
|
|
|
|
const (
|
|
|
|
ProvisionerOnFailureInvalid ProvisionerOnFailure = iota
|
|
|
|
ProvisionerOnFailureContinue
|
|
|
|
ProvisionerOnFailureFail
|
|
|
|
)
|
|
|
|
|
|
|
|
var provisionerBlockSchema = &hcl.BodySchema{
|
|
|
|
Attributes: []hcl.AttributeSchema{
|
2018-11-20 20:53:45 +01:00
|
|
|
{Name: "when"},
|
|
|
|
{Name: "on_failure"},
|
2018-02-03 02:22:25 +01:00
|
|
|
},
|
|
|
|
Blocks: []hcl.BlockHeaderSchema{
|
2018-11-20 20:53:45 +01:00
|
|
|
{Type: "connection"},
|
|
|
|
{Type: "lifecycle"}, // reserved for future use
|
2018-02-03 02:22:25 +01:00
|
|
|
},
|
|
|
|
}
|