func assertNextSubnetIP(c *gc.C, ipnetString string, inuseStrings []string, expectedString string) { ipnet := parseIPNet(c, ipnetString) inuse := parseIPs(c, inuseStrings...) next, err := iputils.NextSubnetIP(ipnet, inuse) c.Assert(err, jc.ErrorIsNil) c.Assert(next.String(), gc.Equals, expectedString) }
// nextSubnetIPAddress returns the next available IP address in the given subnet. func nextSubnetIPAddress( nicClient network.InterfacesClient, resourceGroup string, subnet *network.Subnet, ) (string, error) { _, ipnet, err := net.ParseCIDR(to.String(subnet.Properties.AddressPrefix)) if err != nil { return "", errors.Annotate(err, "parsing subnet prefix") } results, err := nicClient.List(resourceGroup) if err != nil { return "", errors.Annotate(err, "listing NICs") } // Azure reserves the first 4 addresses in the subnet. var ipsInUse []net.IP if results.Value != nil { ipsInUse = make([]net.IP, 0, len(*results.Value)) for _, item := range *results.Value { if item.Properties.IPConfigurations == nil { continue } for _, ipConfiguration := range *item.Properties.IPConfigurations { if to.String(ipConfiguration.Properties.Subnet.ID) != to.String(subnet.ID) { continue } ip := net.ParseIP(to.String(ipConfiguration.Properties.PrivateIPAddress)) if ip != nil { ipsInUse = append(ipsInUse, ip) } } } } ip, err := iputils.NextSubnetIP(ipnet, ipsInUse) if err != nil { return "", errors.Trace(err) } return ip.String(), nil }
func assertNextSubnetIPError(c *gc.C, ipnetString string, inuseStrings []string, expect string) { ipnet := parseIPNet(c, ipnetString) inuse := parseIPs(c, inuseStrings...) _, err := iputils.NextSubnetIP(ipnet, inuse) c.Assert(err, gc.ErrorMatches, expect) }