def allocate_specific_ip(self, ip_address, mac, port_id=None, port_tenant_id=None, device_id=None, device_owner=None): hostname = uuidutils.generate_uuid() port_tenant_name = self.ib_cxt.get_tenant_name(port_tenant_id) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, port_tenant_id, port_tenant_name, self.ib_cxt.network, port_id, device_id, device_owner) dns_view = self.ib_cxt.mapping.dns_view zone_auth = self.pattern_builder.get_zone_name( is_external=self.ib_cxt.network_is_external) ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc if device_owner == n_const.DEVICE_OWNER_DHCP else self.ib_cxt.ip_alloc) allocated_ip = ip_alloc.allocate_given_ip( self.ib_cxt.mapping.network_view, dns_view, zone_auth, hostname, mac, ip_address, ea_ip_address) if allocated_ip: LOG.info('IP address allocated on Infoblox NIOS: %s', allocated_ip) if device_owner == n_const.DEVICE_OWNER_DHCP: self.ib_cxt.update_nameservers(allocated_ip) return allocated_ip
def update_port_sync(self, port): if not port or not port.get('fixed_ips'): return session = self.ib_cxt.context.session for fip in port['fixed_ips']: subnet_id = fip['subnet_id'] ip_address = fip['ip_address'] netview_mappings = dbi.get_network_view_mappings( session, network_id=port['network_id'], subnet_id=subnet_id) if netview_mappings: netview_row = utils.find_one_in_list( 'id', netview_mappings[0].network_view_id, self.ib_cxt.discovered_network_views) network_view = netview_row.network_view port_tenant_name = self.ib_cxt.get_tenant_name( port['tenant_id']) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, port['tenant_id'], port_tenant_name, network_view, port['id'], port['device_id'], port['device_owner']) self.ib_cxt.ibom.update_fixed_address_eas( network_view, ip_address, ea_ip_address)
def update_port_sync(self, port): if not port or not port.get('fixed_ips'): return session = self.ib_cxt.context.session for fip in port['fixed_ips']: subnet_id = fip['subnet_id'] ip_address = fip['ip_address'] netview_mappings = dbi.get_network_view_mappings( session, network_id=port['network_id'], subnet_id=subnet_id) if netview_mappings: netview_row = utils.find_one_in_list( 'id', netview_mappings[0].network_view_id, self.ib_cxt.discovered_network_views) network_view = netview_row.network_view port_tenant_name = self.ib_cxt.get_tenant_name( port['tenant_id']) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, port['tenant_id'], port_tenant_name, network_view, port['id'], port['device_id'], port['device_owner']) self.ib_cxt.ibom.update_fixed_address_eas(network_view, ip_address, ea_ip_address)
def allocate_ip_from_pool(self, subnet_id, allocation_pools, mac, port_id=None, port_tenant_id=None, device_id=None, device_owner=None): hostname = uuidutils.generate_uuid() port_tenant_name = self.ib_cxt.get_tenant_name(port_tenant_id) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, port_tenant_id, port_tenant_name, self.ib_cxt.network, port_id, device_id, device_owner) dns_view = self.ib_cxt.mapping.dns_view zone_auth = self.pattern_builder.get_zone_name( is_external=self.ib_cxt.network_is_external) allocated_ip = None ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc if device_owner == n_const.DEVICE_OWNER_DHCP else self.ib_cxt.ip_alloc) for pool in allocation_pools: first_ip = pool['start'] last_ip = pool['end'] try: allocated_ip = ip_alloc.allocate_ip_from_range( self.ib_cxt.mapping.network_view, dns_view, zone_auth, hostname, mac, first_ip, last_ip, ea_ip_address) if allocated_ip: break except ib_exc.InfobloxCannotAllocateIp as err: LOG.info("Failed to allocate IP from range (%s-%s)" "with error: %r." % (first_ip, last_ip, err)) continue except ib_exc.InfobloxCannotCreateObject as err: LOG.info("Failed to create IP from range (%s-%s)" "with error: %r." % (first_ip, last_ip, err)) continue if allocated_ip: LOG.info('IP address allocated on Infoblox NIOS: %s', allocated_ip) if device_owner == n_const.DEVICE_OWNER_DHCP: self.ib_cxt.update_nameservers(allocated_ip) else: LOG.info("All IPs from subnet %(subnet_id)s allocated", {'subnet_id': subnet_id}) raise ipam_exc.IpAddressGenerationFailure(subnet_id=subnet_id) return allocated_ip
def allocate_ip_from_pool(self, subnet_id, allocation_pools, mac, port_id=None, port_tenant_id=None, device_id=None, device_owner=None): hostname = uuidutils.generate_uuid() port_tenant_name = self.ib_cxt.get_tenant_name(port_tenant_id) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, port_tenant_id, port_tenant_name, self.ib_cxt.network, port_id, device_id, device_owner) dns_view = self.ib_cxt.mapping.dns_view zone_auth = self.pattern_builder.get_zone_name( is_external=self.ib_cxt.network_is_external) allocated_ip = None ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc if device_owner == n_const.DEVICE_OWNER_DHCP else self.ib_cxt.ip_alloc) for pool in allocation_pools: first_ip = pool['start'] last_ip = pool['end'] try: allocated_ip = ip_alloc.allocate_ip_from_range( self.ib_cxt.mapping.network_view, dns_view, zone_auth, hostname, mac, first_ip, last_ip, ea_ip_address) if allocated_ip: break except exc.InfobloxCannotAllocateIp: LOG.info("Failed to allocate IP from range (%s-%s)." % (first_ip, last_ip)) continue if allocated_ip: LOG.info('IP address allocated on Infoblox NIOS: %s', allocated_ip) if device_owner == n_const.DEVICE_OWNER_DHCP: self.ib_cxt.update_nameservers(allocated_ip) else: LOG.info("All IPs from subnet %(subnet_id)s allocated", {'subnet_id': subnet_id}) raise ipam_exc.IpAddressGenerationFailure(subnet_id=subnet_id) return allocated_ip
def test_get_ea_for_ip_with_floatingip_creation(self): network = {'router:external': True, 'shared': False} port_id = mock.Mock() device_id = mock.Mock() device_owner = n_const.DEVICE_OWNER_FLOATINGIP expected_ea = {'Tenant ID': self.tenant_id, 'Tenant Name': self.context.tenant_name, 'Account': self.user_id, 'Port ID': port_id, 'Port Attached Device - Device Owner': device_owner, 'Port Attached Device - Device ID': device_id, 'Cloud API Owned': str(False), 'IP Type': 'Floating', 'VM ID': None} ea = ea_manager.get_ea_for_ip(self.user_id, self.tenant_id, self.tenant_name, network, port_id, device_id, device_owner) for key, value in expected_ea.items(): self.assertEqual(value, ea.get(key))
def test_get_ea_for_ip(self): network = {'router:external': False, 'shared': False} port_id = mock.Mock() device_id = mock.Mock() device_owner = mock.Mock() expected_ea = {'Tenant ID': self.tenant_id, 'Tenant Name': self.context.tenant_name, 'Account': self.user_id, 'Port ID': port_id, 'Port Attached Device - Device Owner': device_owner, 'Port Attached Device - Device ID': device_id, 'Cloud API Owned': str(True), 'IP Type': 'Fixed', 'VM ID': None} ea = ea_manager.get_ea_for_ip(self.user_id, self.tenant_id, self.tenant_name, network, port_id, device_id, device_owner) for key, value in expected_ea.items(): self.assertEqual(value, ea.get(key))
def bind_names(self, ip_address, instance_name=None, port_id=None, port_tenant_id=None, device_id=None, device_owner=None, is_floating_ip=False, port_name=None): if not device_owner: return if device_owner in constants.NEUTRON_DEVICE_OWNER_COMPUTE_LIST: if instance_name is None: instance = dbi.get_instance(self.ib_cxt.context.session, device_id) if instance is not None: instance_name = instance.instance_name tenant_id = port_tenant_id or self.ib_cxt.context.tenant_id tenant_name = self.ib_cxt.get_tenant_name(tenant_id) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, tenant_id, tenant_name, self.ib_cxt.network, port_id, device_id, device_owner, is_floating_ip, instance_name) ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc if device_owner == n_const.DEVICE_OWNER_DHCP else self.ib_cxt.ip_alloc) try: self._bind_names(ip_alloc.bind_names, ip_address, instance_name, port_id, port_tenant_id, device_id, device_owner, ea_ip_address, port_name, tenant_name) except ibc_exc.InfobloxCannotCreateObject: with excutils.save_and_reraise_exception(): self.unbind_names(ip_address, instance_name, port_id, port_tenant_id, device_id, device_owner, port_name)
def test_get_ea_for_ip_with_floatingip_dissociation(self): network = {'router:external': True, 'shared': False} port_id = mock.Mock() device_id = mock.Mock() device_owner = n_const.DEVICE_OWNER_FLOATINGIP expected_ea = { 'Tenant ID': self.tenant_id, 'Tenant Name': self.context.tenant_name, 'Account': self.user_id, 'Port ID': port_id, 'Port Attached Device - Device Owner': device_owner, 'Port Attached Device - Device ID': device_id, 'Cloud API Owned': str(False), 'IP Type': 'Floating', 'VM ID': None } ea = ea_manager.get_ea_for_ip(self.user_id, self.tenant_id, self.tenant_name, network, port_id, device_id, device_owner) for key, value in list(expected_ea.items()): self.assertEqual(value, ea.get(key))
def test_get_ea_for_ip(self): network = {'router:external': False, 'shared': False} port_id = mock.Mock() device_id = mock.Mock() device_owner = mock.Mock() expected_ea = { 'Tenant ID': self.tenant_id, 'Tenant Name': self.context.tenant_name, 'Account': self.user_id, 'Port ID': port_id, 'Port Attached Device - Device Owner': device_owner, 'Port Attached Device - Device ID': device_id, 'Cloud API Owned': str(True), 'IP Type': 'Fixed', 'VM ID': None } ea = ea_manager.get_ea_for_ip(self.user_id, self.tenant_id, self.tenant_name, network, port_id, device_id, device_owner) for key, value in list(expected_ea.items()): self.assertEqual(value, ea.get(key))
def test_get_ea_for_ip_with_floatingip_association(self): network = {'router:external': True, 'shared': False} port_id = mock.Mock() device_id = mock.Mock() device_owner = const.NEUTRON_DEVICE_OWNER_COMPUTE_NOVA is_floating_ip = True expected_ea = { 'Tenant ID': self.tenant_id, 'Tenant Name': self.context.tenant_name, 'Account': self.user_id, 'Port ID': port_id, 'Port Attached Device - Device Owner': device_owner, 'Port Attached Device - Device ID': device_id, 'Cloud API Owned': str(False), 'IP Type': 'Floating', 'VM ID': device_id } ea = ea_manager.get_ea_for_ip(self.user_id, self.tenant_id, self.tenant_name, network, port_id, device_id, device_owner, is_floating_ip) for key, value in expected_ea.items(): self.assertEqual(value, ea.get(key))
def bind_names(self, ip_address, instance_name=None, port_id=None, port_tenant_id=None, device_id=None, device_owner=None, is_floating_ip=False, port_name=None): if not device_owner: return if device_owner in constants.NEUTRON_DEVICE_OWNER_COMPUTE_LIST: if instance_name is None: instance = dbi.get_instance(self.ib_cxt.context.session, device_id) if instance is not None: instance_name = instance.instance_name tenant_id = port_tenant_id or self.ib_cxt.context.tenant_id tenant_name = self.ib_cxt.get_tenant_name(tenant_id) ea_ip_address = eam.get_ea_for_ip(self.ib_cxt.user_id, tenant_id, tenant_name, self.ib_cxt.network, port_id, device_id, device_owner, is_floating_ip, instance_name) ip_alloc = (self.ib_cxt.dhcp_port_ip_alloc if device_owner == n_const.DEVICE_OWNER_DHCP else self.ib_cxt.ip_alloc) try: self._bind_names(ip_alloc.bind_names, ip_address, instance_name, port_id, port_tenant_id, device_id, device_owner, ea_ip_address, port_name) except ibc_exc.InfobloxCannotCreateObject: with excutils.save_and_reraise_exception(): self.unbind_names(ip_address, instance_name, port_id, port_tenant_id, device_id, device_owner, port_name)