-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathresource_zerotier_network.go
More file actions
288 lines (258 loc) · 7.41 KB
/
Copy pathresource_zerotier_network.go
File metadata and controls
288 lines (258 loc) · 7.41 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package zerotier
import (
"bytes"
"encoding/json"
"fmt"
"net"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
)
func route() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"target": &schema.Schema{
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: diffSuppress,
},
"via": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: diffSuppress,
},
},
}
}
func resourceZeroTierNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceNetworkCreate,
Read: resourceNetworkRead,
Update: resourceNetworkUpdate,
Delete: resourceNetworkDelete,
Exists: resourceNetworkExists,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "Managed by Terraform",
},
"rules_source": &schema.Schema{
Type: schema.TypeString,
Optional: true,
// pulled from ZT's default
Default: "#\n# Allow only IPv4, IPv4 ARP, and IPv6 Ethernet frames.\n#\ndrop\n\tnot ethertype ipv4\n\tand not ethertype arp\n\tand not ethertype ipv6\n;\n\n#\n# Uncomment to drop non-ZeroTier issued and managed IP addresses.\n#\n# This prevents IP spoofing but also blocks manual IP management at the OS level and\n# bridging unless special rules to exempt certain hosts or traffic are added before\n# this rule.\n#\n#drop\n#\tnot chr ipauth\n#;\n\n# Accept anything else. This is required since default is 'drop'.\naccept;",
Set: stringHash,
},
"private": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"auto_assign_v4": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"route": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: route(),
},
"assignment_pool": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cidr": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"assignment_pool.first", "assignment_pool.last"},
},
"first": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"assignment_pool.cidr"},
},
"last": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"assignment_pool.cidr"},
},
},
},
Set: resourceIpAssignmentHash,
},
},
}
}
func diffSuppress(k, old, new string, d *schema.ResourceData) bool {
return old == new
}
func resourceNetworkExists(d *schema.ResourceData, m interface{}) (b bool, e error) {
client := m.(*ZeroTierClient)
exists, err := client.CheckNetworkExists(d.Id())
if err != nil {
return exists, err
}
if !exists {
d.SetId("")
}
return exists, nil
}
func fromResourceData(d *schema.ResourceData) (*Network, error) {
routesRaw := d.Get("route").(*schema.Set).List()
var routes []Route
for _, raw := range routesRaw {
r := raw.(map[string]interface{})
via := r["via"].(string)
routes = append(routes, Route{
Target: r["target"].(string),
Via: &via,
})
}
var pools []IpRange
for _, raw := range d.Get("assignment_pool").(*schema.Set).List() {
r := raw.(map[string]interface{})
cidr := r["cidr"].(string)
first, last, err := CIDRToRange(cidr)
if err != nil {
first = net.ParseIP(r["first"].(string))
last = net.ParseIP(r["last"].(string))
}
pools = append(pools, IpRange{
First: first.String(),
Last: last.String(),
})
}
n := &Network{
Id: d.Id(),
RulesSource: d.Get("rules_source").(string),
Description: d.Get("description").(string),
Config: &Config{
Name: d.Get("name").(string),
Private: d.Get("private").(bool),
V4AssignMode: V4AssignModeConfig{ZT: true},
Routes: routes,
IpAssignmentPools: pools,
},
}
return n, nil
}
func resourceNetworkCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*ZeroTierClient)
n, err := fromResourceData(d)
if err != nil {
return err
}
created, err := client.CreateNetwork(n)
if err != nil {
return err
}
d.SetId(created.Id)
setAssignmentPools(d, created)
return nil
}
func resourceNetworkRead(d *schema.ResourceData, m interface{}) error {
client := m.(*ZeroTierClient)
// Attempt to read from an upstream API
net, err := client.GetNetwork(d.Id())
// If the resource does not exist, inform Terraform. We want to immediately
// return here to prevent further processing.
if err != nil {
return fmt.Errorf("unable to read network from API: %s", err)
}
if net == nil {
d.SetId("")
return nil
}
d.Set("name", net.Config.Name)
d.Set("description", net.Description)
d.Set("private", net.Config.Private)
d.Set("auto_assign_v4", net.Config.V4AssignMode.ZT)
d.Set("rules_source", net.RulesSource)
setRoutes(d, net)
setAssignmentPools(d, net)
return nil
}
func setAssignmentPools(d *schema.ResourceData, n *Network) {
rawPools := &schema.Set{F: resourceIpAssignmentHash}
for _, p := range n.Config.IpAssignmentPools {
raw := make(map[string]interface{})
// raw["cidr"] = SmallestCIDR(net.ParseIP(p.First), net.ParseIP(p.Last))
raw["first"] = p.First
raw["last"] = p.Last
rawPools.Add(raw)
}
d.Set("assignment_pool", rawPools)
}
func setRoutes(d *schema.ResourceData, n *Network) {
rawRoutes := make([]interface{}, len(n.Config.Routes))
for i, r := range n.Config.Routes {
raw := make(map[string]interface{})
raw["target"] = r.Target
if r.Via != nil {
raw["via"] = *r.Via
}
rawRoutes[i] = raw
}
d.Set("route", rawRoutes)
}
func resourceNetworkUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*ZeroTierClient)
n, err := fromResourceData(d)
if err != nil {
return err
}
updated, err := client.UpdateNetwork(d.Id(), n)
if err != nil {
stringify, _ := json.Marshal(n)
return fmt.Errorf("unable to update network using ZeroTier API: %s\n\n%s", err, stringify)
}
setAssignmentPools(d, updated)
return nil
}
func resourceNetworkDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*ZeroTierClient)
err := client.DeleteNetwork(d.Id())
return err
}
func resourceNetworkRouteHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
if v, ok := m["target"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["via"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
return hashcode.String(buf.String())
}
func resourceIpAssignmentState(v interface{}) string {
var buf bytes.Buffer
m := v.(map[string]interface{})
if v, ok := m["cidr"]; ok && len(v.(string)) > 0 {
if first, last, err := CIDRToRange(v.(string)); err == nil {
buf.WriteString(fmt.Sprintf("%s-", first.String()))
buf.WriteString(fmt.Sprintf("%s", last.String()))
}
} else {
if v, ok := m["first"]; ok {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
if v, ok := m["last"]; ok {
buf.WriteString(fmt.Sprintf("%s", v.(string)))
}
}
return buf.String()
}
func resourceIpAssignmentHash(v interface{}) int {
return hashcode.String(resourceIpAssignmentState(v))
}
func stringHash(v interface{}) int {
s := v.(string)
return hashcode.String(s)
}