Skip to content

Commit

Permalink
Expose IPv6 and IPv4 assigned addresses as computed properties
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bltavares committed Mar 17, 2019
1 parent 154b3ec commit 7b98b58
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion zerotier/resource_zerotier_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,28 @@ func resourceZeroTierMember() *schema.Resource {
},
"ip_assignments": {
Type: schema.TypeSet,
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.",
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.",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ipv4_assignments": {
Type: schema.TypeSet,
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.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ipv6_assignments": {
Type: schema.TypeSet,
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.",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"rfc4193_address": {
Type: schema.TypeString,
Description: "Computed RFC4193 (IPv6 /128) address. Always calculated and only actually assigned on the member if RFC4193 is configured on the network.",
Expand Down Expand Up @@ -218,11 +234,13 @@ func buildIPV6(data string) (result string) {
return
}

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

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

// Split the list of assigned IPs into IPv6 and IPv4 lists
// Does not include 6PLANE or RFC4193, only those from the assignment pool
func assingnedIpsGrouping(ipAssignments []string) (ipv4s []string, ipv6s []string) {
for _, element := range ipAssignments {
if strings.Contains(element, ":") {
ipv6s = append(ipv6s, element)
} else {
ipv4s = append(ipv4s, element)
}
}
return
}

func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
client := m.(*ZeroTierClient)

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

ipv4Assignments, ipv6Assignments := assingnedIpsGrouping(member.Config.IpAssignments)

d.SetId(member.Id)
d.Set("name", member.Name)
d.Set("description", member.Description)
Expand All @@ -259,6 +292,8 @@ func resourceMemberRead(d *schema.ResourceData, m interface{}) error {
d.Set("allow_ethernet_bridging", member.Config.ActiveBridge)
d.Set("no_auto_assign_ips", member.Config.NoAutoAssignIps)
d.Set("ip_assignments", member.Config.IpAssignments)
d.Set("ipv4_assignments", ipv4Assignments)
d.Set("ipv6_assignments", ipv6Assignments)
d.Set("rfc4193_address", rfc4193Address(d))
d.Set("6plane_address", sixPlaneAddress(d))
d.Set("capabilities", member.Config.Capabilities)
Expand Down

0 comments on commit 7b98b58

Please sign in to comment.