/// <summary> /// Calculates all possible subnets in the (address block + cidr) and returns one not being used at the moment. /// </summary> private async Task <string> InternalGetRange(string addressBlock, byte cidr, string tableName, string vpcName) { if (cidr < 16 || cidr > 28) { throw new ArgumentOutOfRangeException(nameof(cidr), "Cidr must be between /16 and /28"); } var network = IPNetwork.Parse(addressBlock); var subnets = network.Subnet(cidr); var numberOfPossibleSubnets = (int)subnets.Count; IPNetwork validSubnet = null; NetworkRange networkRange = null; var count = 0; var storedRanges = await GetRangesForVpc(vpcName, tableName); do { count++; var possibleSubnet = subnets[_random.Next(numberOfPossibleSubnets)]; networkRange = new NetworkRange { AddressRange = possibleSubnet.ToString(), VpcName = vpcName }; if (CanUseRange(storedRanges, possibleSubnet)) { validSubnet = possibleSubnet; } } while (validSubnet == null && count < numberOfPossibleSubnets); if (validSubnet == null) { throw new Exception("No available ranges for this address block"); } await _context.SaveAsync(networkRange, new DynamoDBOperationConfig { OverrideTableName = tableName }); return(networkRange.AddressRange); }
public bool Equals(NetworkRange other) => other != null && AddressRange == other.AddressRange && VpcName == other.VpcName;