Skip to content

Commit 681e299

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 d577564 commit 681e299

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ resource "zerotier_member" "hector" {
252252
# see ZeroTier Manual section on L2/ethernet bridging
253253
allow_ethernet_bridging = true
254254
255+
# Computed properties
256+
257+
# ipv4_assignments: Computed list of IPv4 assigned by ZeroTier controller assignment pool
258+
# ipv6_assignments: Computed list of IPv6 assigned by ZeroTier controller assignment pool.
259+
# Note: Does not include RFC4193 nor 6PLANE addresses, only those from assignment pool or manually provided, similar to ip_assignments.
260+
255261
}
256262
```
257263

zerotier/resource_zerotier_member.go

Lines changed: 39 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,8 +62,25 @@ 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 by ZeroTier controller assignment pool. Does not include RFC4193 nor 6PLANE addresses, only those from assignment pool or manually provided.",
67+
Optional: true,
68+
Elem: &schema.Schema{
69+
Type: schema.TypeString,
70+
},
71+
},
72+
"ipv4_assignments": {
73+
Type: schema.TypeSet,
74+
Description: "Computed list of IPv4 assigned by ZeroTier controller assignment pool.",
75+
Computed: true,
76+
Elem: &schema.Schema{
77+
Type: schema.TypeString,
78+
},
79+
},
80+
"ipv6_assignments": {
81+
Type: schema.TypeSet,
82+
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.",
83+
Computed: true,
6684
Elem: &schema.Schema{
6785
Type: schema.TypeString,
6886
},
@@ -148,7 +166,7 @@ func memberFromResourceData(d *schema.ResourceData) (*Member, error) {
148166
for i := range capsRaw {
149167
caps[i] = capsRaw[i].(int)
150168
}
151-
ipsRaw := d.Get("ip_assignments").([]interface{})
169+
ipsRaw := d.Get("ip_assignments").(*schema.Set).List()
152170
ips := make([]string, len(ipsRaw))
153171
for i := range ipsRaw {
154172
ips[i] = ipsRaw[i].(string)
@@ -172,6 +190,20 @@ func memberFromResourceData(d *schema.ResourceData) (*Member, error) {
172190
}
173191
return n, nil
174192
}
193+
194+
// Split the list of assigned IPs into IPv6 and IPv4 lists
195+
// Does not include 6PLANE or RFC4193, only those from the assignment pool
196+
func assingnedIpsGrouping(ipAssignments []string) (ipv4s []string, ipv6s []string) {
197+
for _, element := range ipAssignments {
198+
if strings.Contains(element, ":") {
199+
ipv6s = append(ipv6s, element)
200+
} else {
201+
ipv4s = append(ipv4s, element)
202+
}
203+
}
204+
return
205+
}
206+
175207
func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
176208
client := m.(*ZeroTierClient)
177209

@@ -190,6 +222,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
190222
return nil
191223
}
192224

225+
ipv4Assignments, ipv6Assignments := assingnedIpsGrouping(member.Config.IpAssignments)
226+
193227
d.SetId(member.Id)
194228
d.Set("name", member.Name)
195229
d.Set("description", member.Description)
@@ -199,6 +233,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
199233
d.Set("allow_ethernet_bridging", member.Config.ActiveBridge)
200234
d.Set("no_auto_assign_ips", member.Config.NoAutoAssignIps)
201235
d.Set("ip_assignments", member.Config.IpAssignments)
236+
d.Set("ipv4_assignments", ipv4Assignments)
237+
d.Set("ipv6_assignments", ipv6Assignments)
202238
d.Set("capabilities", member.Config.Capabilities)
203239
setTags(d, member)
204240

0 commit comments

Comments
 (0)