Skip to content

Commit 6d1e4ee

Browse files
committed
Allow managing IPv6 network settings
ZeroTier has a couple of settings related to IPv6 address assignment options which distribute IPv6 addresses computed based on the network id and the node id. RFC4139 assigns an single IPv6 address for each of the nodes. 6PLANE assigns a whole /80 prefix for each node, which could be redistributed by the member, such as a router or used for Docker containers. Both of them are deterministic values calculated based on the network id and node id, and they are not returned on the response of the controller, given that the client is capable of calculating it itself. IPv6 Assignment distribute IPv6 from the the assignment pool for each member. If there is no IPv6 assignment pool configured, no route will be distributed. It is important to also include the route configuration for that network, so there is traffic through ZeroTier. This commit exposes this information on the member resource, as a computed property, so we could reference this information on other Terraform resources (such as DNS settings or provisioner scripts). Given this is a calculated property that is always present, downstream modules should check if the network has it configured before using it. No errors will be thrown, it would only not route properly if the network has not enabled it. The commit also exposes the settings in the network to enable each of the IPv6 address distribution toggles on the network resource. There is also a bug fix, where the IPv4 configuration toggle was hardcoded and not reading the value from the resource definition.
1 parent 0a8d145 commit 6d1e4ee

File tree

4 files changed

+138
-6
lines changed

4 files changed

+138
-6
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,53 @@ resource "zerotier_network" "your_network" {
114114
If you don't specify either an assignment pool or a managed route, while it's
115115
perfectly valid, your network won't be very useful, so try to do both.
116116

117+
Full list of properties:
118+
119+
```hcl
120+
resource "zerotier_network" "your_network" {
121+
name = "your_network_name"
122+
123+
# Optional values
124+
# description = "Managed by Terraform"
125+
# rules_source = "Default rule pulled from ZeroTier"
126+
127+
# private = true
128+
129+
# Assign IPv4 addresses from the assignment_pool
130+
# auto_assign_v4 = true
131+
132+
# Effectively assign IPv6 RFC4193 (/128) for members of the network
133+
# auto_assign_rfc4193 = true
134+
135+
# Effectively assign IPv6 RFC4193 (/128) for members of the network
136+
# auto_assign_6plane = false
137+
138+
# Assing IPv6 addresses from the assignment_pool
139+
# auto_assign_v6 = false
140+
141+
# Multiple assignment pools allowed
142+
# assignment_pool {
143+
# cidr = "IPv4 or IPv6 CIDR notation"
144+
# }
145+
# assignment_pool {
146+
# first = "IPv4 or IPv6 address" # eg 10.96.0.2
147+
# last = "IPv4 or IPv6 address" # eg 10.96.0.254
148+
# }
149+
150+
# Multiple routes configuration allowed
151+
# route {
152+
# target = "${var.zt_cidr}"
153+
# }
154+
# route {
155+
# target = "${var.other_network}"
156+
# via = "${local.gateway_ip}"
157+
# }
158+
159+
# Computed
160+
# id: Network ID
161+
}
162+
```
163+
117164
#### Multiple routes
118165

119166
You can have more than one assignment pool, and more than one route. Multiple
@@ -251,6 +298,16 @@ resource "zerotier_member" "hector" {
251298
# see ZeroTier Manual section on L2/ethernet bridging
252299
allow_ethernet_bridging = true
253300
301+
# Computed properties available to interpolate
302+
303+
# rfc4193_address
304+
# Computed RFC4193 (IPv6 /128) address based on the network and node id
305+
# Always calculated, and determined if they are used by the network resource
306+
307+
# 6plane_address
308+
# Computed 6PLANE (IPv6 /80) address based on the network and node id
309+
# Always calculated, and determined if they are used by the network resource
310+
254311
}
255312
```
256313

zerotier/client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@ type V4AssignModeConfig struct {
3131
ZT bool `json:"zt"`
3232
}
3333

34+
type V6AssignModeConfig struct {
35+
ZT bool `json:"zt"`
36+
SixPLANE bool `json:"6plane"`
37+
RFC4193 bool `json:"rfc4193"`
38+
}
39+
3440
type Config struct {
3541
Name string `json:"name"`
3642
Private bool `json:"private"`
3743
Routes []Route `json:"routes"`
3844
IpAssignmentPools []IpRange `json:"ipAssignmentPools"`
3945
V4AssignMode V4AssignModeConfig `json:"v4AssignMode"`
46+
V6AssignMode V6AssignModeConfig `json:"v6AssignMode"`
4047
}
4148

4249
type ConfigReadOnly struct {

zerotier/resource_zerotier_member.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,23 @@ func resourceZeroTierMember() *schema.Resource {
6565
Default: false,
6666
},
6767
"ip_assignments": {
68-
Type: schema.TypeList,
69-
Optional: true,
68+
Type: schema.TypeSet,
69+
Description: "List of IP routed and assigned byt ZeroTier controller assignment pool. Does not include RFC4193 nor 6PLANE addresses, only those from assignment pool or manually provided.",
70+
Optional: true,
7071
Elem: &schema.Schema{
7172
Type: schema.TypeString,
7273
},
7374
},
75+
"rfc4193_address": {
76+
Type: schema.TypeString,
77+
Description: "Computed RFC4193 (IPv6 /128) address. Always calculated and only actually assigned on the member if RFC4193 is configured on the network.",
78+
Computed: true,
79+
},
80+
"6plane_address": {
81+
Type: schema.TypeString,
82+
Description: "Computed 6PLANE (IPv6 /60) address. Always calculated and only actually assigned on the member if 6PLANE is configured on the network.",
83+
Computed: true,
84+
},
7485
"capabilities": {
7586
Type: schema.TypeList,
7687
Optional: true,
@@ -152,7 +163,7 @@ func memberFromResourceData(d *schema.ResourceData) (*Member, error) {
152163
for i := range capsRaw {
153164
caps[i] = capsRaw[i].(int)
154165
}
155-
ipsRaw := d.Get("ip_assignments").([]interface{})
166+
ipsRaw := d.Get("ip_assignments").(*schema.Set).List()
156167
ips := make([]string, len(ipsRaw))
157168
for i := range ipsRaw {
158169
ips[i] = ipsRaw[i].(string)
@@ -193,6 +204,33 @@ func resourceNetworkAndNodeIdentifiers(d *schema.ResourceData) (string, string)
193204
return nwid, nodeID
194205
}
195206

207+
// Receive a string and format every 4th element with a ":"
208+
func buildIPV6(data string) (result string) {
209+
s := strings.SplitAfter(data, "")
210+
end := len(s) - 1
211+
result = ""
212+
for i, s := range s {
213+
result += s
214+
if (i+1)%4 == 0 && i != end {
215+
result += ":"
216+
}
217+
}
218+
return
219+
}
220+
221+
func sixPlaneAddress(d *schema.ResourceData) string {
222+
nwid, nodeID := resourceNetworkAndNodeIdentifiers(d)
223+
return buildIPV6("fd" + nwid + "9993" + nodeID)
224+
}
225+
226+
func rfc4193Address(d *schema.ResourceData) string {
227+
nwid, nodeID := resourceNetworkAndNodeIdentifiers(d)
228+
nwidInt, _ := strconv.ParseUint(nwid, 16, 64)
229+
networkMask := uint32((nwidInt >> 32) ^ nwidInt)
230+
networkPrefix := strconv.FormatUint(uint64(networkMask), 16)
231+
return buildIPV6("fc" + networkPrefix + nodeID + "000000000001")
232+
}
233+
196234
func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
197235
client := m.(*ZeroTierClient)
198236

@@ -221,6 +259,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
221259
d.Set("allow_ethernet_bridging", member.Config.ActiveBridge)
222260
d.Set("no_auto_assign_ips", member.Config.NoAutoAssignIps)
223261
d.Set("ip_assignments", member.Config.IpAssignments)
262+
d.Set("rfc4193_address", rfc4193Address(d))
263+
d.Set("6plane_address", sixPlaneAddress(d))
224264
d.Set("capabilities", member.Config.Capabilities)
225265
setTags(d, member)
226266

zerotier/resource_zerotier_network.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ func resourceZeroTierNetwork() *schema.Resource {
6565
Optional: true,
6666
Default: true,
6767
},
68+
"auto_assign_v6": &schema.Schema{
69+
Type: schema.TypeBool,
70+
Description: "Auto assign IPv6 to members from ZeroTier assignment pool",
71+
Optional: true,
72+
Default: false,
73+
},
74+
"auto_assign_6plane": &schema.Schema{
75+
Type: schema.TypeBool,
76+
Description: "Auto assign IPv6 /60 to members using 6PLANE adressing",
77+
Optional: true,
78+
Default: false,
79+
},
80+
"auto_assign_rfc4193": &schema.Schema{
81+
Type: schema.TypeBool,
82+
Description: "Auto assign IPv6 /128 to members using RFC4193 adressing",
83+
Optional: true,
84+
Default: true,
85+
},
6886
"route": &schema.Schema{
6987
Type: schema.TypeList,
7088
Optional: true,
@@ -145,9 +163,16 @@ func fromResourceData(d *schema.ResourceData) (*Network, error) {
145163
RulesSource: d.Get("rules_source").(string),
146164
Description: d.Get("description").(string),
147165
Config: &Config{
148-
Name: d.Get("name").(string),
149-
Private: d.Get("private").(bool),
150-
V4AssignMode: V4AssignModeConfig{ZT: true},
166+
Name: d.Get("name").(string),
167+
Private: d.Get("private").(bool),
168+
V4AssignMode: V4AssignModeConfig{
169+
ZT: d.Get("auto_assign_v4").(bool),
170+
},
171+
V6AssignMode: V6AssignModeConfig{
172+
ZT: d.Get("auto_assign_v6").(bool),
173+
SixPLANE: d.Get("auto_assign_6plane").(bool),
174+
RFC4193: d.Get("auto_assign_rfc4193").(bool),
175+
},
151176
Routes: routes,
152177
IpAssignmentPools: pools,
153178
},
@@ -190,6 +215,9 @@ func resourceNetworkRead(d *schema.ResourceData, m interface{}) error {
190215
d.Set("description", net.Description)
191216
d.Set("private", net.Config.Private)
192217
d.Set("auto_assign_v4", net.Config.V4AssignMode.ZT)
218+
d.Set("auto_assign_v6", net.Config.V6AssignMode.ZT)
219+
d.Set("auto_assign_6plane", net.Config.V6AssignMode.SixPLANE)
220+
d.Set("auto_assign_rfc4193", net.Config.V6AssignMode.RFC4193)
193221
d.Set("rules_source", net.RulesSource)
194222

195223
setRoutes(d, net)

0 commit comments

Comments
 (0)