-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-services.go
142 lines (128 loc) · 4.55 KB
/
create-services.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"os"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
)
func main() {
// TODO: set account owner email to be used retrieve user for defining pagerduty_schedule
ACCOUNT_OWNER_EMAIL := ""
// create new empty hcl file object
f := hclwrite.NewEmptyFile()
// create new file on system
tfFile, err := os.Create("servicelist.tf")
if err != nil {
fmt.Println(err)
return
}
// initialize the body of the new file object
rootBody := f.Body()
// initialize terraform object and set provider version
tfBlock := rootBody.AppendNewBlock("terraform", nil)
tfBlockBody := tfBlock.Body()
reqProvsBlock := tfBlockBody.AppendNewBlock("required_providers",
nil)
reqProvsBlockBody := reqProvsBlock.Body()
reqProvsBlockBody.SetAttributeValue("pagerduty", cty.ObjectVal(map[string]cty.Value{
"source": cty.StringVal("PagerDuty/pagerduty"),
"version": cty.StringVal("2.3.0"),
}))
rootBody.AppendNewline()
// create escalation policy
ep := rootBody.AppendNewBlock("resource",
[]string{"pagerduty_escalation_policy", "ep"})
epBody := ep.Body()
epBody.SetAttributeValue("name", cty.StringVal("Me EP"))
epBody.SetAttributeValue("num_loops", cty.NumberIntVal(3))
epBody.AppendNewline()
// rule block
epRule := epBody.AppendNewBlock("rule", nil)
epRuleBody := epRule.Body()
epRuleBody.SetAttributeValue("escalation_delay_in_minutes", cty.NumberIntVal(30))
epRuleBody.AppendNewline()
// target block
epRuleTarget := epRuleBody.AppendNewBlock("target", nil)
epRuleTargetBody := epRuleTarget.Body()
epRuleTargetBody.SetAttributeValue("type", cty.StringVal("schedule_reference"))
// pagerduty_schedule resource reference using SetAttributeRaw
schedTokens := hclwrite.Tokens{
{Type: hclsyntax.TokenIdent, Bytes: []byte(`pagerduty_schedule.foo.id`)},
}
epRuleTargetBody.SetAttributeRaw("id", schedTokens)
// pagerduty_schedule resource reference using SetAttribute (here for reference)
// epRuleTargetBody.SetAttributeTraversal("id", hcl.Traversal{
// hcl.TraverseRoot{
// Name: "pagerduty_schedule",
// },
// hcl.TraverseAttr{
// Name: "foo",
// },
// hcl.TraverseAttr{
// Name: "id",
// },
// })
rootBody.AppendNewline()
// create schedule
sched := rootBody.AppendNewBlock("resource",
[]string{"pagerduty_schedule", "foo"})
schedBody := sched.Body()
schedBody.SetAttributeValue("name", cty.StringVal("Me Schedule"))
schedBody.SetAttributeValue("time_zone", cty.StringVal("America/Los_Angeles"))
schedBody.AppendNewline()
// layer block
schedLayer := schedBody.AppendNewBlock("layer", nil)
schedLayerBody := schedLayer.Body()
schedLayerBody.SetAttributeValue("name", cty.StringVal("This Shift"))
schedLayerBody.SetAttributeValue("start", cty.StringVal("2022-03-15T20:00:00-08:00"))
schedLayerBody.SetAttributeValue("rotation_virtual_start", cty.StringVal("2022-03-15T20:00:00-08:00"))
schedLayerBody.SetAttributeValue("rotation_turn_length_seconds", cty.NumberIntVal(86400))
// users attribute with hclwrite.Tokens and SetAttributeRaw
userTokens := hclwrite.Tokens{
{
Type: hclsyntax.TokenOBrack,
Bytes: []byte(`[`),
},
{
Type: hclsyntax.TokenIdent,
Bytes: []byte(`data.pagerduty_user.me.id`),
},
{
Type: hclsyntax.TokenCBrack,
Bytes: []byte(`]`),
},
}
schedLayerBody.SetAttributeRaw("users", userTokens)
// add restriction to schedule layer
schedLayerBody.AppendNewline()
restriction := schedLayerBody.AppendNewBlock("restriction", nil)
restBody := restriction.Body()
restBody.SetAttributeValue("type", cty.StringVal("daily_restriction"))
restBody.SetAttributeValue("start_time_of_day", cty.StringVal("17:00:00"))
restBody.SetAttributeValue("duration_seconds", cty.NumberIntVal(54000))
rootBody.AppendNewline()
// get user
me := rootBody.AppendNewBlock("data",
[]string{"pagerduty_user", "me"})
meBody := me.Body()
meBody.SetAttributeValue("email", cty.StringVal(ACCOUNT_OWNER_EMAIL))
rootBody.AppendNewline()
// create a lot of services
for i := 1; i <= 50; i++ {
service := rootBody.AppendNewBlock("resource",
[]string{"pagerduty_service",
fmt.Sprintf("me%v", i)})
serviceBody := service.Body()
serviceBody.SetAttributeValue("name",
cty.StringVal(fmt.Sprintf("Me Service %v", i)))
// defining tokens for pagerduty_escalation_policy resource reference
tokens := hclwrite.Tokens{
{Type: hclsyntax.TokenIdent, Bytes: []byte(`pagerduty_escalation_policy.ep.id`)},
}
serviceBody.SetAttributeRaw("escalation_policy", tokens)
rootBody.AppendNewline()
}
fmt.Printf("%s", f.Bytes())
tfFile.Write(f.Bytes())
}