From 3c85d94f707ee574db0819c101a03b0a6271a5e7 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 20 Jun 2022 10:34:22 +0100 Subject: [PATCH] schema: introduce lifecycle conditions --- internal/schema/1.2/root.go | 89 +++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 internal/schema/1.2/root.go diff --git a/internal/schema/1.2/root.go b/internal/schema/1.2/root.go new file mode 100644 index 00000000..b592ddde --- /dev/null +++ b/internal/schema/1.2/root.go @@ -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, + }, + }, + }, + }, + }, + }, +}