def test_verifies_that_node_will_reuse_its_ip_address(self): ip_range = models.IPAddrRange(first='10.10.0.3', last='10.10.0.9') self.admin_ng.ip_ranges.append(ip_range) db().flush() manager.NetworkManager.assign_admin_ips(self.env.nodes) for node in self.reuse_nodes: self.assertEqual(len(node.ip_addrs), 1) ip = node.ip_addrs[0] self.assertEqual(node.ip, ip.ip_addr) self.assertEqual(len(self.assign_from_new.ip_addrs), 1) ip = self.assign_from_new.ip_addrs[0] self.assertEqual(ip.ip_addr, '10.10.0.5')
def test_assign_vip_in_correct_node_group(self): cluster = self.env.create( release_kwargs={'version': 'test-8.0'}, cluster_kwargs={'api': False} ) # prepare two nodes that are in different node groups self.env.create_node( cluster_id=cluster.id, pending_roles=['controller']) self.env.create_node( cluster_id=cluster.id, pending_roles=['compute']) group_id = self.env.create_node_group( cluster_id=cluster.id ).json_body['id'] cluster.nodes[1].group_id = group_id # configure management net to have custom range on compute nodes management_net = self.db.query(models.NetworkGroup)\ .filter_by(group_id=group_id, name=consts.NETWORKS.management)\ .first() management_net.ip_ranges = [ models.IPAddrRange(first='10.42.0.2', last='10.42.0.10')] # populate release with a network role that requests a VIP # for compute nodes nrm = { 'id': 'mymgmt/vip', 'default_mapping': consts.NETWORKS.management, 'properties': { 'subnet': True, 'gateway': False, 'vip': [{ 'name': 'my-vip', 'node_roles': ['compute'], }] } } cluster.release.network_roles_metadata.append(nrm) self.db.flush() self.env.neutron_networks_put(cluster.id, {}) nm = objects.Cluster.get_network_manager(cluster) nm.assign_vips_for_net_groups(cluster) resp = self.env.neutron_networks_get(cluster.id) self.assertEqual(200, resp.status_code) ipaddr = resp.json_body['vips']['my-vip']['ipaddr'] self.assertEqual('10.42.0.2', ipaddr)
def _set_ip_ranges(cls, instance, ip_ranges): """Set IP-address ranges. :param instance: NetworkGroup instance being updated :type instance: models.NetworkGroup :param ip_ranges: IP-address ranges sequence :type ip_ranges: iterable of pairs :return: None """ # deleting old ip ranges db().query(models.IPAddrRange).filter_by( network_group_id=instance.id).delete() for r in ip_ranges: new_ip_range = models.IPAddrRange(first=r[0], last=r[1], network_group_id=instance.id) db().add(new_ip_range) db().refresh(instance) db().flush()
def create(cls, data): """Create NetworkGroup instance with specified parameters in DB. Create corresponding IPAddrRange instance with IP range specified in data or calculated from CIDR if not specified. :param data: dictionary of key-value pairs as NetworkGroup fields :returns: instance of new NetworkGroup """ instance = super(NetworkGroup, cls).create(data) notation = instance.meta.get('notation') if notation: ip_range = models.IPAddrRange(network_group_id=instance.id) try: if notation == 'cidr': cidr = IPNetwork(instance.cidr).cidr ip_range.first = str(cidr[2]) ip_range.last = str(cidr[-2]) elif notation == 'ip_ranges' and instance.meta.get('ip_range'): ip_range.first = instance.meta['ip_range'][0] ip_range.last = instance.meta['ip_range'][1] else: raise errors.CannotCreate() except ( errors.CannotCreate, IndexError, TypeError ): raise errors.CannotCreate( "IPAddrRange object cannot be created for network '{0}' " "with notation='{1}', ip_range='{2}'".format( instance.name, instance.meta.get('notation'), instance.meta.get('ip_range')) ) db().add(ip_range) db().flush() return instance