From ff33ec460685fd176adff93089fd123a1b687655 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Thu, 7 Jul 2022 09:04:35 +0100 Subject: [PATCH 1/2] schema: add 1.2 lifecycle 'replace_triggered_by' attribute --- internal/schema/1.2/root.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/schema/1.2/root.go b/internal/schema/1.2/root.go index 6dbf57b9..0234a454 100644 --- a/internal/schema/1.2/root.go +++ b/internal/schema/1.2/root.go @@ -7,20 +7,19 @@ import ( "github.com/zclconf/go-cty/cty" v1_1_mod "github.com/hashicorp/terraform-schema/internal/schema/1.1" + "github.com/hashicorp/terraform-schema/internal/schema/refscope" ) var v1_2 = version.Must(version.NewVersion("1.2.0")) func ModuleSchema(v *version.Version) *schema.BodySchema { bs := v1_1_mod.ModuleSchema(v) - if v.GreaterThanOrEqual(v1_2) { - bs.Blocks["data"].Body.Blocks = map[string]*schema.BlockSchema{ - "lifecycle": datasourceLifecycleBlock, - } - bs.Blocks["resource"].Body.Blocks["lifecycle"] = resourceLifecycleBlock - bs.Blocks["output"].Body.Blocks = map[string]*schema.BlockSchema{ - "lifecycle": outputLifecycleBlock, - } + bs.Blocks["data"].Body.Blocks = map[string]*schema.BlockSchema{ + "lifecycle": datasourceLifecycleBlock, + } + bs.Blocks["resource"].Body.Blocks["lifecycle"] = resourceLifecycleBlock + bs.Blocks["output"].Body.Blocks = map[string]*schema.BlockSchema{ + "lifecycle": outputLifecycleBlock, } return bs @@ -79,6 +78,19 @@ var resourceLifecycleBlock = &schema.BlockSchema{ IsOptional: true, Description: lang.Markdown("A set of fields (references) of which to ignore changes to, e.g. `tags`"), }, + "replace_triggered_by": { + Expr: schema.ExprConstraints{ + schema.TupleConsExpr{ + Name: "set of references", + AnyElem: schema.ExprConstraints{ + schema.TraversalExpr{OfScopeId: refscope.ResourceScope}, + }, + }, + }, + IsOptional: true, + Description: lang.Markdown("Set of references to any other resources which when changed cause " + + "this resource to be proposed for replacement"), + }, }, Blocks: map[string]*schema.BlockSchema{ "precondition": { From 177b39c4de6e031c910d24fc8ae82cbeb68a74ee Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Fri, 8 Jul 2022 12:46:38 +0100 Subject: [PATCH 2/2] break down code into files by block names --- internal/schema/1.2/data.go | 20 +++++++++ internal/schema/1.2/output.go | 17 +++++++ internal/schema/1.2/resource.go | 61 +++++++++++++++++++++++++ internal/schema/1.2/root.go | 79 --------------------------------- 4 files changed, 98 insertions(+), 79 deletions(-) create mode 100644 internal/schema/1.2/data.go create mode 100644 internal/schema/1.2/output.go create mode 100644 internal/schema/1.2/resource.go diff --git a/internal/schema/1.2/data.go b/internal/schema/1.2/data.go new file mode 100644 index 00000000..d92af58c --- /dev/null +++ b/internal/schema/1.2/data.go @@ -0,0 +1,20 @@ +package schema + +import ( + "github.com/hashicorp/hcl-lang/lang" + "github.com/hashicorp/hcl-lang/schema" +) + +var datasourceLifecycleBlock = &schema.BlockSchema{ + Description: lang.Markdown("Lifecycle customizations to set validity conditions of the datasource"), + Body: &schema.BodySchema{ + Blocks: map[string]*schema.BlockSchema{ + "precondition": { + Body: conditionBody, + }, + "postcondition": { + Body: conditionBody, + }, + }, + }, +} diff --git a/internal/schema/1.2/output.go b/internal/schema/1.2/output.go new file mode 100644 index 00000000..352a51fb --- /dev/null +++ b/internal/schema/1.2/output.go @@ -0,0 +1,17 @@ +package schema + +import ( + "github.com/hashicorp/hcl-lang/lang" + "github.com/hashicorp/hcl-lang/schema" +) + +var outputLifecycleBlock = &schema.BlockSchema{ + Description: lang.Markdown("Lifecycle customizations, to set a validity condition of the output"), + Body: &schema.BodySchema{ + Blocks: map[string]*schema.BlockSchema{ + "precondition": { + Body: conditionBody, + }, + }, + }, +} diff --git a/internal/schema/1.2/resource.go b/internal/schema/1.2/resource.go new file mode 100644 index 00000000..850467c1 --- /dev/null +++ b/internal/schema/1.2/resource.go @@ -0,0 +1,61 @@ +package schema + +import ( + "github.com/hashicorp/hcl-lang/lang" + "github.com/hashicorp/hcl-lang/schema" + "github.com/hashicorp/terraform-schema/internal/schema/refscope" + "github.com/zclconf/go-cty/cty" +) + +var resourceLifecycleBlock = &schema.BlockSchema{ + Description: lang.Markdown("Lifecycle customizations to change default resource behaviours during plan or 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`"), + }, + "replace_triggered_by": { + Expr: schema.ExprConstraints{ + schema.TupleConsExpr{ + Name: "set of references", + AnyElem: schema.ExprConstraints{ + schema.TraversalExpr{OfScopeId: refscope.ResourceScope}, + }, + }, + }, + IsOptional: true, + Description: lang.Markdown("Set of references to any other resources which when changed cause " + + "this resource to be proposed for replacement"), + }, + }, + Blocks: map[string]*schema.BlockSchema{ + "precondition": { + Body: conditionBody, + }, + "postcondition": { + Body: conditionBody, + }, + }, + }, +} diff --git a/internal/schema/1.2/root.go b/internal/schema/1.2/root.go index 0234a454..b9579426 100644 --- a/internal/schema/1.2/root.go +++ b/internal/schema/1.2/root.go @@ -7,7 +7,6 @@ import ( "github.com/zclconf/go-cty/cty" v1_1_mod "github.com/hashicorp/terraform-schema/internal/schema/1.1" - "github.com/hashicorp/terraform-schema/internal/schema/refscope" ) var v1_2 = version.Must(version.NewVersion("1.2.0")) @@ -25,84 +24,6 @@ func ModuleSchema(v *version.Version) *schema.BodySchema { return bs } -var datasourceLifecycleBlock = &schema.BlockSchema{ - Description: lang.Markdown("Lifecycle customizations to set validity conditions of the datasource"), - Body: &schema.BodySchema{ - Blocks: map[string]*schema.BlockSchema{ - "precondition": { - Body: conditionBody, - }, - "postcondition": { - Body: conditionBody, - }, - }, - }, -} - -var outputLifecycleBlock = &schema.BlockSchema{ - Description: lang.Markdown("Lifecycle customizations, to set a validity condition of the output"), - Body: &schema.BodySchema{ - Blocks: map[string]*schema.BlockSchema{ - "precondition": { - Body: conditionBody, - }, - }, - }, -} - -var resourceLifecycleBlock = &schema.BlockSchema{ - Description: lang.Markdown("Lifecycle customizations to change default resource behaviours during plan or 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`"), - }, - "replace_triggered_by": { - Expr: schema.ExprConstraints{ - schema.TupleConsExpr{ - Name: "set of references", - AnyElem: schema.ExprConstraints{ - schema.TraversalExpr{OfScopeId: refscope.ResourceScope}, - }, - }, - }, - IsOptional: true, - Description: lang.Markdown("Set of references to any other resources which when changed cause " + - "this resource to be proposed for replacement"), - }, - }, - Blocks: map[string]*schema.BlockSchema{ - "precondition": { - Body: conditionBody, - }, - "postcondition": { - Body: conditionBody, - }, - }, - }, -} - var conditionBody = &schema.BodySchema{ Attributes: map[string]*schema.AttributeSchema{ "condition": {