-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock_signature_terraform_helpers_example_test.go
77 lines (65 loc) · 2.01 KB
/
block_signature_terraform_helpers_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package tfsig_test
import (
"fmt"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
"github.com/yoanm/go-tfsig"
)
func ExampleBlockSignature_DependsOn() {
// resource with 'depends_on' directive
sig := tfsig.NewResource("res_name", "res_id")
sig.AppendAttribute("attribute1", cty.StringVal("value1"))
sig.DependsOn([]string{"another_res.res_id", "another_another_res.res_id"})
hclFile := hclwrite.NewEmptyFile()
hclFile.Body().AppendBlock(sig.Build())
fmt.Println(string(hclFile.Bytes()))
// Output:
// resource "res_name" "res_id" {
// attribute1 = "value1"
//
// depends_on = [another_res.res_id, another_another_res.res_id]
// }
}
func ExampleBlockSignature_Lifecycle() {
// resource with 'lifecycle' directive
sig := tfsig.NewResource("res_name", "res_id")
sig.AppendAttribute("attribute1", cty.StringVal("value1"))
config := tfsig.LifecycleConfig{}
config.SetCreateBeforeDestroy(true)
config.SetPreventDestroy(false)
sig.Lifecycle(config)
sig2 := tfsig.NewResource("res2_name", "res2_id")
sig2.AppendAttribute("attribute1", cty.StringVal("value1"))
config2 := tfsig.LifecycleConfig{
IgnoreChanges: []string{"attribute1"},
Postcondition: &tfsig.LifecycleCondition{
Condition: "res_name.res_id.attribute1 != \"value1\"",
ErrorMessage: "res_name.res_id.attribute1 must equal \"value1\"",
},
}
sig2.Lifecycle(config2)
hclFile := hclwrite.NewEmptyFile()
hclFile.Body().AppendBlock(sig.Build())
hclFile.Body().AppendBlock(sig2.Build())
fmt.Println(string(hclFile.Bytes()))
// Output:
// resource "res_name" "res_id" {
// attribute1 = "value1"
//
// lifecycle {
// create_before_destroy = true
// prevent_destroy = false
// }
// }
// resource "res2_name" "res2_id" {
// attribute1 = "value1"
//
// lifecycle {
// ignore_changes = [attribute1]
// postcondition {
// condition = res_name.res_id.attribute1 != "value1"
// error_message = "res_name.res_id.attribute1 must equal \"value1\""
// }
// }
// }
}