def create_floatingip(self, values): self.validate_floatingip_params(values) network_id = values.pop('floating_network_id') floatingip_address = values.pop('floating_ip_address') pool = neutron.FloatingIPPool(network_id) # validate the floating ip address is out of allocation_pools and # within its subnet cidr. try: subnet = pool.fetch_subnet(floatingip_address) except exceptions.BlazarException: LOG.info("Floating IP %s in network %s can't be used " "for Blazar's resource.", floatingip_address, network_id) raise floatingip_values = { 'floating_network_id': network_id, 'subnet_id': subnet['id'], 'floating_ip_address': floatingip_address } floatingip = db_api.floatingip_create(floatingip_values) return floatingip
def _update_allocations(self, dates_before, dates_after, reservation_id, reservation_status, fip_reservation, values): amount = int(values.get('amount', fip_reservation['amount'])) fip_allocations = db_api.fip_allocation_get_all_by_values( reservation_id=reservation_id) allocs_to_remove = self._allocations_to_remove( dates_before, dates_after, fip_allocations, amount) if (allocs_to_remove and reservation_status == status.reservation.ACTIVE): raise manager_ex.CantUpdateFloatingIPReservation( msg="Cannot remove allocations from an active reservation") kept_fips = len(fip_allocations) - len(allocs_to_remove) fip_ids_to_add = [] if kept_fips < amount: needed_fips = amount - kept_fips required_fips = values.get( 'required_floatingips', fip_reservation['required_floatingips']) fip_ids_to_add = self._matching_fips( fip_reservation['network_id'], required_fips, needed_fips, dates_after['start_date'], dates_after['end_date']) if len(fip_ids_to_add) < needed_fips: raise manager_ex.NotEnoughFloatingIPAvailable() # Create new floating IPs if reservation is active created_fips = [] if reservation_status == status.reservation.ACTIVE: ctx = context.current() fip_pool = neutron.FloatingIPPool(fip_reservation['network_id']) for fip_id in fip_ids_to_add: try: fip = db_api.floatingip_get(fip_id) LOG.debug( 'Creating floating IP {} for reservation {}'.format( fip['floating_ip_address'], reservation_id)) fip_pool.create_reserved_floatingip( fip['subnet_id'], fip['floating_ip_address'], ctx.project_id, reservation_id) created_fips.append(fip['floating_ip_address']) except Exception as e: for fip_address in created_fips: fip_pool.delete_reserved_floatingip(fip_address) err_msg = 'Failed to create floating IP: {}'.format(str(e)) raise manager_ex.NeutronClientError(err_msg) for fip_id in fip_ids_to_add: LOG.debug('Adding floating IP {} to reservation {}'.format( fip_id, reservation_id)) db_api.fip_allocation_create({ 'floatingip_id': fip_id, 'reservation_id': reservation_id}) for allocation in allocs_to_remove: LOG.debug('Removing floating IP {} from reservation {}'.format( allocation['floatingip_id'], reservation_id)) db_api.fip_allocation_destroy(allocation['id'])
def on_end(self, resource_id): fip_reservation = db_api.fip_reservation_get(resource_id) allocations = db_api.fip_allocation_get_all_by_values( reservation_id=fip_reservation['reservation_id']) fip_pool = neutron.FloatingIPPool(fip_reservation['network_id']) for alloc in allocations: fip = db_api.floatingip_get(alloc['floatingip_id']) fip_pool.delete_reserved_floatingip(fip['floating_ip_address'])
def on_start(self, resource_id): fip_reservation = db_api.fip_reservation_get(resource_id) allocations = db_api.fip_allocation_get_all_by_values( reservation_id=fip_reservation['reservation_id']) ctx = context.current() fip_pool = neutron.FloatingIPPool(fip_reservation['network_id']) for alloc in allocations: fip = db_api.floatingip_get(alloc['floatingip_id']) fip_pool.create_reserved_floatingip( fip['subnet_id'], fip['floating_ip_address'], ctx.project_id, fip_reservation['reservation_id'])
def test_delete_floatingip_with_deleted(self, mock_delete, mock_update, mock_list): mock_list.return_value = {'floatingips': []} client = neutron.FloatingIPPool('net-id') client.delete_reserved_floatingip('172.24.4.200') query = { 'floating_ip_address': '172.24.4.200', 'floating_network_id': 'net-id' } mock_list.assert_called_once_with(**query) mock_update.assert_not_called() mock_delete.assert_not_called()
def test_create_reserved_floatingip(self, mock_tag, mock_fip): mock_fip.return_value = {'floatingip': {'id': 'fip-id'}} client = neutron.FloatingIPPool('net-id') client.create_reserved_floatingip('subnet-id', '172.24.4.200', 'project-id', 'reservation-id') expected_body = { 'floatingip': { 'floating_network_id': 'net-id', 'subnet_id': 'subnet-id', 'floating_ip_address': '172.24.4.200', 'project_id': 'project-id' } } mock_fip.assert_called_once_with(expected_body) mock_tag.assert_called_once_with( 'floatingips', 'fip-id', {'tags': ['blazar', 'reservation:reservation-id']})
def test_init_floatingippool(self): client = neutron.FloatingIPPool('net-id') self.assertEqual('net-id', client.network_id) self.mock_net.assert_called_once_with('net-id')