Skip to content

Commit 2902a42

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 d577564 commit 2902a42

File tree

4 files changed

+156
-6
lines changed

4 files changed

+156
-6
lines changed

README.md

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

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

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

zerotier/client.go

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

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

4350
type ConfigReadOnly struct {

zerotier/resource_zerotier_member.go

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"strconv"
6+
"strings"
67

78
"github.com/hashicorp/terraform/helper/schema"
89
)
@@ -61,12 +62,23 @@ func resourceZeroTierMember() *schema.Resource {
6162
Default: false,
6263
},
6364
"ip_assignments": {
64-
Type: schema.TypeList,
65-
Optional: true,
65+
Type: schema.TypeSet,
66+
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.",
67+
Optional: true,
6668
Elem: &schema.Schema{
6769
Type: schema.TypeString,
6870
},
6971
},
72+
"rfc4193_address": {
73+
Type: schema.TypeString,
74+
Description: "Computed RFC4193 (IPv6 /128) address. Always calculated and only actually assigned on the member if RFC4193 is configured on the network.",
75+
Computed: true,
76+
},
77+
"6plane_address": {
78+
Type: schema.TypeString,
79+
Description: "Computed 6PLANE (IPv6 /60) address. Always calculated and only actually assigned on the member if 6PLANE is configured on the network.",
80+
Computed: true,
81+
},
7082
"capabilities": {
7183
Type: schema.TypeList,
7284
Optional: true,
@@ -148,7 +160,7 @@ func memberFromResourceData(d *schema.ResourceData) (*Member, error) {
148160
for i := range capsRaw {
149161
caps[i] = capsRaw[i].(int)
150162
}
151-
ipsRaw := d.Get("ip_assignments").([]interface{})
163+
ipsRaw := d.Get("ip_assignments").(*schema.Set).List()
152164
ips := make([]string, len(ipsRaw))
153165
for i := range ipsRaw {
154166
ips[i] = ipsRaw[i].(string)
@@ -172,6 +184,50 @@ func memberFromResourceData(d *schema.ResourceData) (*Member, error) {
172184
}
173185
return n, nil
174186
}
187+
188+
// Extracts the Network ID and Node ID from the resource definition, or from the id during import
189+
//
190+
// When importing a resource, both the network id and node id writen on the definition will be ignored
191+
// and we could retrieve the network id and node id from parts of the id
192+
// which is formated as <network-id>-<node-id> on zerotier
193+
func resourceNetworkAndNodeIdentifiers(d *schema.ResourceData) (string, string) {
194+
nwid := d.Get("network_id").(string)
195+
nodeID := d.Get("node_id").(string)
196+
197+
if nwid == "" && nodeID == "" {
198+
parts := strings.Split(d.Id(), "-")
199+
nwid, nodeID = parts[0], parts[1]
200+
}
201+
return nwid, nodeID
202+
}
203+
204+
// Receive a string and format every 4th element with a ":"
205+
func buildIPV6(data string) (result string) {
206+
s := strings.SplitAfter(data, "")
207+
end := len(s) - 1
208+
result = ""
209+
for i, s := range s {
210+
result += s
211+
if (i+1)%4 == 0 && i != end {
212+
result += ":"
213+
}
214+
}
215+
return
216+
}
217+
218+
func sixPlaneAddress(d *schema.ResourceData) string {
219+
nwid, nodeID := resourceNetworkAndNodeIdentifiers(d)
220+
return buildIPV6("fd" + nwid + "9993" + nodeID)
221+
}
222+
223+
func rfc4193Address(d *schema.ResourceData) string {
224+
nwid, nodeID := resourceNetworkAndNodeIdentifiers(d)
225+
nwidInt, _ := strconv.ParseUint(nwid, 16, 64)
226+
networkMask := uint32((nwidInt >> 32) ^ nwidInt)
227+
networkPrefix := strconv.FormatUint(uint64(networkMask), 16)
228+
return buildIPV6("fc" + networkPrefix + nodeID + "000000000001")
229+
}
230+
175231
func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
176232
client := m.(*ZeroTierClient)
177233

@@ -199,6 +255,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
199255
d.Set("allow_ethernet_bridging", member.Config.ActiveBridge)
200256
d.Set("no_auto_assign_ips", member.Config.NoAutoAssignIps)
201257
d.Set("ip_assignments", member.Config.IpAssignments)
258+
d.Set("rfc4193_address", rfc4193Address(d))
259+
d.Set("6plane_address", sixPlaneAddress(d))
202260
d.Set("capabilities", member.Config.Capabilities)
203261
setTags(d, member)
204262

zerotier/resource_zerotier_network.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ func resourceZeroTierNetwork() *schema.Resource {
6262
Optional: true,
6363
Default: true,
6464
},
65+
"auto_assign_v6": &schema.Schema{
66+
Type: schema.TypeBool,
67+
Description: "Auto assign IPv6 to members from ZeroTier assignment pool",
68+
Optional: true,
69+
Default: false,
70+
},
71+
"auto_assign_6plane": &schema.Schema{
72+
Type: schema.TypeBool,
73+
Description: "Auto assign IPv6 /60 to members using 6PLANE adressing",
74+
Optional: true,
75+
Default: false,
76+
},
77+
"auto_assign_rfc4193": &schema.Schema{
78+
Type: schema.TypeBool,
79+
Description: "Auto assign IPv6 /128 to members using RFC4193 adressing",
80+
Optional: true,
81+
Default: true,
82+
},
6583
"route": &schema.Schema{
6684
Type: schema.TypeList,
6785
Optional: true,
@@ -142,9 +160,16 @@ func fromResourceData(d *schema.ResourceData) (*Network, error) {
142160
RulesSource: d.Get("rules_source").(string),
143161
Description: d.Get("description").(string),
144162
Config: &Config{
145-
Name: d.Get("name").(string),
146-
Private: d.Get("private").(bool),
147-
V4AssignMode: V4AssignModeConfig{ZT: true},
163+
Name: d.Get("name").(string),
164+
Private: d.Get("private").(bool),
165+
V4AssignMode: V4AssignModeConfig{
166+
ZT: d.Get("auto_assign_v4").(bool),
167+
},
168+
V6AssignMode: V6AssignModeConfig{
169+
ZT: d.Get("auto_assign_v6").(bool),
170+
SixPLANE: d.Get("auto_assign_6plane").(bool),
171+
RFC4193: d.Get("auto_assign_rfc4193").(bool),
172+
},
148173
Routes: routes,
149174
IpAssignmentPools: pools,
150175
},
@@ -187,6 +212,9 @@ func resourceNetworkRead(d *schema.ResourceData, m interface{}) error {
187212
d.Set("description", net.Description)
188213
d.Set("private", net.Config.Private)
189214
d.Set("auto_assign_v4", net.Config.V4AssignMode.ZT)
215+
d.Set("auto_assign_v6", net.Config.V6AssignMode.ZT)
216+
d.Set("auto_assign_6plane", net.Config.V6AssignMode.SixPLANE)
217+
d.Set("auto_assign_rfc4193", net.Config.V6AssignMode.RFC4193)
190218
d.Set("rules_source", net.RulesSource)
191219

192220
setRoutes(d, net)

0 commit comments

Comments
 (0)