- # TODO get a non used vnet ip range /24 and allocate automatically inside the range self.vnet_address_space
- # use netaddr library
- raise vimconn.vimconnException('Azure cannot create VNET with no CIDR')
-
+ # get a non used vnet ip range /24 and allocate automatically inside the range self.vnet_address_space
+ used_subnets = self.get_network_list()
+ for ip_range in netaddr.IPNetwork(self.vnet_address_space).subnet(24):
+ for used_subnet in used_subnets:
+ subnet_range = netaddr.IPNetwork(used_subnet["cidr_block"])
+ if subnet_range in ip_range or ip_range in subnet_range:
+ # this range overlaps with an existing subnet ip range. Breaks and look for another
+ break
+ else:
+ ip_profile = {"subnet_address": str(ip_range)}
+ break
+ else:
+ vimconn.vimconnException("Cannot find a non-used subnet range in {}".format(self.vnet_address_space))