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 delete_floatingip(self, fip_id): fip = db_api.floatingip_get(fip_id) if fip is None: raise manager_ex.FloatingIPNotFound(floatingip=fip_id) allocations = db_api.fip_allocation_get_all_by_values( floatingip_id=fip_id) if allocations: msg = 'Floating IP id %s is allocated by reservations.' % fip_id LOG.info(msg) raise manager_ex.CantDeleteFloatingIP(floatingip=fip_id, msg=msg) try: db_api.floatingip_destroy(fip_id) except db_ex.BlazarDBException as e: raise manager_ex.CantDeleteFloatingIP(floatingip=fip_id, msg=str(e))
def get_floatingip(self, fip_id): fip = db_api.floatingip_get(fip_id) if fip is None: raise manager_ex.FloatingIPNotFound(floatingip=fip_id) return fip