Skip to content

Commit 7b98b58

Browse files
committed
Expose IPv6 and IPv4 assigned addresses as computed properties
Currently, the `ip_assignments` has a list of the IPs that ZeroTier provided to the member from its assignment pool. It contains a mix of IPv4 and IPv6 addresses. There is some scenarios where having the distiction of wheter it is an IPv4 or IPv6 changes which resource to create. For example using DNS records we are only allowed to create `AAAA` records in the presence of IPv6 addresses, and `A` records to IPv4. Filtering this information on Terraform is cumbersome, using the filter on list operations, while it is much easier to provide this information through the provider. This commit create two extra computed properties, `ipv6_assignments` and `ipv4_assignments`, which separates each address assignment as expected. The `ipv6_assignments` does not include RFC4139 nor 6PLANE addresses as they are always computed on the `member` resource level, even if the `network` is configured to not use those addresses, and their information is not returned by the controller API as an assigned address either.
1 parent 154b3ec commit 7b98b58

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

zerotier/resource_zerotier_member.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,28 @@ func resourceZeroTierMember() *schema.Resource {
6666
},
6767
"ip_assignments": {
6868
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.",
69+
Description: "List of IP routed and assigned by ZeroTier controller assignment pool. Does not include RFC4193 nor 6PLANE addresses, only those from assignment pool or manually provided.",
7070
Optional: true,
7171
Elem: &schema.Schema{
7272
Type: schema.TypeString,
7373
},
7474
},
75+
"ipv4_assignments": {
76+
Type: schema.TypeSet,
77+
Description: "Computed list of IPv4 assigned by ZeroTier controller assignment pool. Does not include RFC4193 nor 6PLANE addresses, only those from assignment pool or manually provided.",
78+
Computed: true,
79+
Elem: &schema.Schema{
80+
Type: schema.TypeString,
81+
},
82+
},
83+
"ipv6_assignments": {
84+
Type: schema.TypeSet,
85+
Description: "Computed list of IPv6 assigned by ZeroTier controller assignment pool. Does not include RFC4193 nor 6PLANE addresses, only those from assignment pool or manually provided.",
86+
Computed: true,
87+
Elem: &schema.Schema{
88+
Type: schema.TypeString,
89+
},
90+
},
7591
"rfc4193_address": {
7692
Type: schema.TypeString,
7793
Description: "Computed RFC4193 (IPv6 /128) address. Always calculated and only actually assigned on the member if RFC4193 is configured on the network.",
@@ -218,11 +234,13 @@ func buildIPV6(data string) (result string) {
218234
return
219235
}
220236

237+
// Calculate 6PLANE address for the member
221238
func sixPlaneAddress(d *schema.ResourceData) string {
222239
nwid, nodeID := resourceNetworkAndNodeIdentifiers(d)
223240
return buildIPV6("fd" + nwid + "9993" + nodeID)
224241
}
225242

243+
// Calculate RFC4193 address for the member
226244
func rfc4193Address(d *schema.ResourceData) string {
227245
nwid, nodeID := resourceNetworkAndNodeIdentifiers(d)
228246
nwidInt, _ := strconv.ParseUint(nwid, 16, 64)
@@ -231,6 +249,19 @@ func rfc4193Address(d *schema.ResourceData) string {
231249
return buildIPV6("fc" + networkPrefix + nodeID + "000000000001")
232250
}
233251

252+
// Split the list of assigned IPs into IPv6 and IPv4 lists
253+
// Does not include 6PLANE or RFC4193, only those from the assignment pool
254+
func assingnedIpsGrouping(ipAssignments []string) (ipv4s []string, ipv6s []string) {
255+
for _, element := range ipAssignments {
256+
if strings.Contains(element, ":") {
257+
ipv6s = append(ipv6s, element)
258+
} else {
259+
ipv4s = append(ipv4s, element)
260+
}
261+
}
262+
return
263+
}
264+
234265
func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
235266
client := m.(*ZeroTierClient)
236267

@@ -248,6 +279,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
248279
return nil
249280
}
250281

282+
ipv4Assignments, ipv6Assignments := assingnedIpsGrouping(member.Config.IpAssignments)
283+
251284
d.SetId(member.Id)
252285
d.Set("name", member.Name)
253286
d.Set("description", member.Description)
@@ -259,6 +292,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
259292
d.Set("allow_ethernet_bridging", member.Config.ActiveBridge)
260293
d.Set("no_auto_assign_ips", member.Config.NoAutoAssignIps)
261294
d.Set("ip_assignments", member.Config.IpAssignments)
295+
d.Set("ipv4_assignments", ipv4Assignments)
296+
d.Set("ipv6_assignments", ipv6Assignments)
262297
d.Set("rfc4193_address", rfc4193Address(d))
263298
d.Set("6plane_address", sixPlaneAddress(d))
264299
d.Set("capabilities", member.Config.Capabilities)

0 commit comments

Comments
 (0)