Skip to content

Commit

Permalink
schema: introduce lifecycle conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jun 20, 2022
1 parent 9aece33 commit 3c85d94
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions internal/schema/1.2/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package schema

import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/schema"
"github.com/zclconf/go-cty/cty"

v015_mod "github.com/hashicorp/terraform-schema/internal/schema/0.15"
)

var v1_2 = version.Must(version.NewVersion("1.2.0"))

func ModuleSchema(v *version.Version) *schema.BodySchema {
bs := v015_mod.ModuleSchema(v)
if v.GreaterThanOrEqual(v1_2) {
bs.Blocks["resource"].Body.Blocks["lifecycle"] = lifecycleBlock
}

return bs
}

var lifecycleBlock = &schema.BlockSchema{
Description: lang.Markdown("Lifecycle customizations to change default resource behaviours during apply"),
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"create_before_destroy": {
Expr: schema.LiteralTypeOnly(cty.Bool),
IsOptional: true,
Description: lang.Markdown("Whether to reverse the default order of operations (destroy -> create) during apply " +
"when the resource requires replacement (cannot be updated in-place)"),
},
"prevent_destroy": {
Expr: schema.LiteralTypeOnly(cty.Bool),
IsOptional: true,
Description: lang.Markdown("Whether to prevent accidental destruction of the resource and cause Terraform " +
"to reject with an error any plan that would destroy the resource"),
},
"ignore_changes": {
Expr: schema.ExprConstraints{
schema.TupleConsExpr{},
schema.KeywordExpr{
Keyword: "all",
Description: lang.Markdown("Ignore all attributes, which means that Terraform can create" +
" and destroy the remote object but will never propose updates to it"),
},
},
IsOptional: true,
Description: lang.Markdown("A set of fields (references) of which to ignore changes to, e.g. `tags`"),
},
},
Blocks: map[string]*schema.BlockSchema{
"precondition": {
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"condition": {
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Bool},
schema.LiteralTypeExpr{Type: cty.Bool},
},
IsRequired: true,
},
"error_message": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
},
},
},
},
"postcondition": {
Body: &schema.BodySchema{
Attributes: map[string]*schema.AttributeSchema{
"condition": {
Expr: schema.ExprConstraints{
schema.TraversalExpr{OfType: cty.Bool},
schema.LiteralTypeExpr{Type: cty.Bool},
},
IsRequired: true,
},
"error_message": {
Expr: schema.LiteralTypeOnly(cty.String),
IsRequired: true,
},
},
},
},
},
},
}

0 comments on commit 3c85d94

Please sign in to comment.