Skip to content

Commit d0852f5

Browse files
authored
schema: add 1.2 lifecycle replace_triggered_by attribute (#123)
* schema: add 1.2 lifecycle 'replace_triggered_by' attribute * break down code into files by block names
1 parent 43f5a81 commit d0852f5

File tree

4 files changed

+104
-73
lines changed

4 files changed

+104
-73
lines changed

internal/schema/1.2/data.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package schema
2+
3+
import (
4+
"github.com/hashicorp/hcl-lang/lang"
5+
"github.com/hashicorp/hcl-lang/schema"
6+
)
7+
8+
var datasourceLifecycleBlock = &schema.BlockSchema{
9+
Description: lang.Markdown("Lifecycle customizations to set validity conditions of the datasource"),
10+
Body: &schema.BodySchema{
11+
Blocks: map[string]*schema.BlockSchema{
12+
"precondition": {
13+
Body: conditionBody,
14+
},
15+
"postcondition": {
16+
Body: conditionBody,
17+
},
18+
},
19+
},
20+
}

internal/schema/1.2/output.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package schema
2+
3+
import (
4+
"github.com/hashicorp/hcl-lang/lang"
5+
"github.com/hashicorp/hcl-lang/schema"
6+
)
7+
8+
var outputLifecycleBlock = &schema.BlockSchema{
9+
Description: lang.Markdown("Lifecycle customizations, to set a validity condition of the output"),
10+
Body: &schema.BodySchema{
11+
Blocks: map[string]*schema.BlockSchema{
12+
"precondition": {
13+
Body: conditionBody,
14+
},
15+
},
16+
},
17+
}

internal/schema/1.2/resource.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package schema
2+
3+
import (
4+
"github.com/hashicorp/hcl-lang/lang"
5+
"github.com/hashicorp/hcl-lang/schema"
6+
"github.com/hashicorp/terraform-schema/internal/schema/refscope"
7+
"github.com/zclconf/go-cty/cty"
8+
)
9+
10+
var resourceLifecycleBlock = &schema.BlockSchema{
11+
Description: lang.Markdown("Lifecycle customizations to change default resource behaviours during plan or apply"),
12+
Body: &schema.BodySchema{
13+
Attributes: map[string]*schema.AttributeSchema{
14+
"create_before_destroy": {
15+
Expr: schema.LiteralTypeOnly(cty.Bool),
16+
IsOptional: true,
17+
Description: lang.Markdown("Whether to reverse the default order of operations (destroy -> create) during apply " +
18+
"when the resource requires replacement (cannot be updated in-place)"),
19+
},
20+
"prevent_destroy": {
21+
Expr: schema.LiteralTypeOnly(cty.Bool),
22+
IsOptional: true,
23+
Description: lang.Markdown("Whether to prevent accidental destruction of the resource and cause Terraform " +
24+
"to reject with an error any plan that would destroy the resource"),
25+
},
26+
"ignore_changes": {
27+
Expr: schema.ExprConstraints{
28+
schema.TupleConsExpr{},
29+
schema.KeywordExpr{
30+
Keyword: "all",
31+
Description: lang.Markdown("Ignore all attributes, which means that Terraform can create" +
32+
" and destroy the remote object but will never propose updates to it"),
33+
},
34+
},
35+
IsOptional: true,
36+
Description: lang.Markdown("A set of fields (references) of which to ignore changes to, e.g. `tags`"),
37+
},
38+
"replace_triggered_by": {
39+
Expr: schema.ExprConstraints{
40+
schema.TupleConsExpr{
41+
Name: "set of references",
42+
AnyElem: schema.ExprConstraints{
43+
schema.TraversalExpr{OfScopeId: refscope.ResourceScope},
44+
},
45+
},
46+
},
47+
IsOptional: true,
48+
Description: lang.Markdown("Set of references to any other resources which when changed cause " +
49+
"this resource to be proposed for replacement"),
50+
},
51+
},
52+
Blocks: map[string]*schema.BlockSchema{
53+
"precondition": {
54+
Body: conditionBody,
55+
},
56+
"postcondition": {
57+
Body: conditionBody,
58+
},
59+
},
60+
},
61+
}

internal/schema/1.2/root.go

+6-73
Original file line numberDiff line numberDiff line change
@@ -13,84 +13,17 @@ var v1_2 = version.Must(version.NewVersion("1.2.0"))
1313

1414
func ModuleSchema(v *version.Version) *schema.BodySchema {
1515
bs := v1_1_mod.ModuleSchema(v)
16-
if v.GreaterThanOrEqual(v1_2) {
17-
bs.Blocks["data"].Body.Blocks = map[string]*schema.BlockSchema{
18-
"lifecycle": datasourceLifecycleBlock,
19-
}
20-
bs.Blocks["resource"].Body.Blocks["lifecycle"] = resourceLifecycleBlock
21-
bs.Blocks["output"].Body.Blocks = map[string]*schema.BlockSchema{
22-
"lifecycle": outputLifecycleBlock,
23-
}
16+
bs.Blocks["data"].Body.Blocks = map[string]*schema.BlockSchema{
17+
"lifecycle": datasourceLifecycleBlock,
18+
}
19+
bs.Blocks["resource"].Body.Blocks["lifecycle"] = resourceLifecycleBlock
20+
bs.Blocks["output"].Body.Blocks = map[string]*schema.BlockSchema{
21+
"lifecycle": outputLifecycleBlock,
2422
}
2523

2624
return bs
2725
}
2826

29-
var datasourceLifecycleBlock = &schema.BlockSchema{
30-
Description: lang.Markdown("Lifecycle customizations to set validity conditions of the datasource"),
31-
Body: &schema.BodySchema{
32-
Blocks: map[string]*schema.BlockSchema{
33-
"precondition": {
34-
Body: conditionBody,
35-
},
36-
"postcondition": {
37-
Body: conditionBody,
38-
},
39-
},
40-
},
41-
}
42-
43-
var outputLifecycleBlock = &schema.BlockSchema{
44-
Description: lang.Markdown("Lifecycle customizations, to set a validity condition of the output"),
45-
Body: &schema.BodySchema{
46-
Blocks: map[string]*schema.BlockSchema{
47-
"precondition": {
48-
Body: conditionBody,
49-
},
50-
},
51-
},
52-
}
53-
54-
var resourceLifecycleBlock = &schema.BlockSchema{
55-
Description: lang.Markdown("Lifecycle customizations to change default resource behaviours during plan or apply"),
56-
Body: &schema.BodySchema{
57-
Attributes: map[string]*schema.AttributeSchema{
58-
"create_before_destroy": {
59-
Expr: schema.LiteralTypeOnly(cty.Bool),
60-
IsOptional: true,
61-
Description: lang.Markdown("Whether to reverse the default order of operations (destroy -> create) during apply " +
62-
"when the resource requires replacement (cannot be updated in-place)"),
63-
},
64-
"prevent_destroy": {
65-
Expr: schema.LiteralTypeOnly(cty.Bool),
66-
IsOptional: true,
67-
Description: lang.Markdown("Whether to prevent accidental destruction of the resource and cause Terraform " +
68-
"to reject with an error any plan that would destroy the resource"),
69-
},
70-
"ignore_changes": {
71-
Expr: schema.ExprConstraints{
72-
schema.TupleConsExpr{},
73-
schema.KeywordExpr{
74-
Keyword: "all",
75-
Description: lang.Markdown("Ignore all attributes, which means that Terraform can create" +
76-
" and destroy the remote object but will never propose updates to it"),
77-
},
78-
},
79-
IsOptional: true,
80-
Description: lang.Markdown("A set of fields (references) of which to ignore changes to, e.g. `tags`"),
81-
},
82-
},
83-
Blocks: map[string]*schema.BlockSchema{
84-
"precondition": {
85-
Body: conditionBody,
86-
},
87-
"postcondition": {
88-
Body: conditionBody,
89-
},
90-
},
91-
},
92-
}
93-
9427
var conditionBody = &schema.BodySchema{
9528
Attributes: map[string]*schema.AttributeSchema{
9629
"condition": {

0 commit comments

Comments
 (0)