Merge "enginefacade: 'floating_ip'"

This commit is contained in:
Jenkins 2016-01-27 12:48:41 +00:00 committed by Gerrit Code Review
commit fa14511fa3

View File

@ -350,7 +350,7 @@ def _sync_instances(context, project_id, user_id):
def _sync_floating_ips(context, project_id, user_id): def _sync_floating_ips(context, project_id, user_id):
return dict(floating_ips=_floating_ip_count_by_project( return dict(floating_ips=_floating_ip_count_by_project(
context, project_id, context.session)) context, project_id))
def _sync_fixed_ips(context, project_id, user_id): def _sync_fixed_ips(context, project_id, user_id):
@ -742,6 +742,7 @@ def certificate_get_all_by_user_and_project(context, user_id, project_id):
@require_context @require_context
@main_context_manager.reader
def floating_ip_get(context, id): def floating_ip_get(context, id):
try: try:
result = model_query(context, models.FloatingIp, project_only=True).\ result = model_query(context, models.FloatingIp, project_only=True).\
@ -759,6 +760,7 @@ def floating_ip_get(context, id):
@require_context @require_context
@main_context_manager.reader
def floating_ip_get_pools(context): def floating_ip_get_pools(context):
pools = [] pools = []
for result in model_query(context, models.FloatingIp, for result in model_query(context, models.FloatingIp,
@ -770,54 +772,50 @@ def floating_ip_get_pools(context):
@require_context @require_context
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True, @oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True,
retry_on_request=True) retry_on_request=True)
@main_context_manager.writer
def floating_ip_allocate_address(context, project_id, pool, def floating_ip_allocate_address(context, project_id, pool,
auto_assigned=False): auto_assigned=False):
nova.context.authorize_project_context(context, project_id) nova.context.authorize_project_context(context, project_id)
session = get_session() floating_ip_ref = model_query(context, models.FloatingIp,
with session.begin(): read_deleted="no").\
floating_ip_ref = model_query(context, models.FloatingIp, filter_by(fixed_ip_id=None).\
session=session, read_deleted="no").\ filter_by(project_id=None).\
filter_by(fixed_ip_id=None).\ filter_by(pool=pool).\
filter_by(project_id=None).\ first()
filter_by(pool=pool).\
first()
if not floating_ip_ref: if not floating_ip_ref:
raise exception.NoMoreFloatingIps() raise exception.NoMoreFloatingIps()
params = {'project_id': project_id, 'auto_assigned': auto_assigned} params = {'project_id': project_id, 'auto_assigned': auto_assigned}
rows_update = model_query(context, models.FloatingIp, rows_update = model_query(context, models.FloatingIp, read_deleted="no").\
session=session, read_deleted="no").\ filter_by(id=floating_ip_ref['id']).\
filter_by(id=floating_ip_ref['id']).\ filter_by(fixed_ip_id=None).\
filter_by(fixed_ip_id=None).\ filter_by(project_id=None).\
filter_by(project_id=None).\ filter_by(pool=pool).\
filter_by(pool=pool).\ update(params, synchronize_session='evaluate')
update(params, synchronize_session='evaluate')
if not rows_update: if not rows_update:
LOG.debug('The row was updated in a concurrent transaction, ' LOG.debug('The row was updated in a concurrent transaction, '
'we will fetch another one') 'we will fetch another one')
raise db_exc.RetryRequest(exception.FloatingIpAllocateFailed()) raise db_exc.RetryRequest(exception.FloatingIpAllocateFailed())
return floating_ip_ref['address'] return floating_ip_ref['address']
@require_context @require_context
@main_context_manager.writer
def floating_ip_bulk_create(context, ips, want_result=True): def floating_ip_bulk_create(context, ips, want_result=True):
session = get_session() try:
with session.begin(): tab = models.FloatingIp().__table__
try: context.session.execute(tab.insert(), ips)
tab = models.FloatingIp().__table__ except db_exc.DBDuplicateEntry as e:
session.execute(tab.insert(), ips) raise exception.FloatingIpExists(address=e.value)
except db_exc.DBDuplicateEntry as e:
raise exception.FloatingIpExists(address=e.value)
if want_result: if want_result:
return model_query( return model_query(context, models.FloatingIp).filter(
context, models.FloatingIp, session=session).filter( models.FloatingIp.address.in_(
models.FloatingIp.address.in_( [ip['address'] for ip in ips])).all()
[ip['address'] for ip in ips])).all()
def _ip_range_splitter(ips, block_size=256): def _ip_range_splitter(ips, block_size=256):
@ -838,24 +836,23 @@ def _ip_range_splitter(ips, block_size=256):
@require_context @require_context
@main_context_manager.writer
def floating_ip_bulk_destroy(context, ips): def floating_ip_bulk_destroy(context, ips):
session = get_session() project_id_to_quota_count = collections.defaultdict(int)
with session.begin(): for ip_block in _ip_range_splitter(ips):
project_id_to_quota_count = collections.defaultdict(int) # Find any floating IPs that were not auto_assigned and
for ip_block in _ip_range_splitter(ips): # thus need quota released.
# Find any floating IPs that were not auto_assigned and query = model_query(context, models.FloatingIp).\
# thus need quota released. filter(models.FloatingIp.address.in_(ip_block)).\
query = model_query(context, models.FloatingIp, session=session).\ filter_by(auto_assigned=False)
filter(models.FloatingIp.address.in_(ip_block)).\ for row in query.all():
filter_by(auto_assigned=False) # The count is negative since we release quota by
for row in query.all(): # reserving negative quota.
# The count is negative since we release quota by project_id_to_quota_count[row['project_id']] -= 1
# reserving negative quota. # Delete the floating IPs.
project_id_to_quota_count[row['project_id']] -= 1 model_query(context, models.FloatingIp).\
# Delete the floating IPs. filter(models.FloatingIp.address.in_(ip_block)).\
model_query(context, models.FloatingIp, session=session).\ soft_delete(synchronize_session='fetch')
filter(models.FloatingIp.address.in_(ip_block)).\
soft_delete(synchronize_session='fetch')
# Delete the quotas, if needed. # Delete the quotas, if needed.
# Quota update happens in a separate transaction, so previous must have # Quota update happens in a separate transaction, so previous must have
@ -873,21 +870,21 @@ def floating_ip_bulk_destroy(context, ips):
@require_context @require_context
@main_context_manager.writer
def floating_ip_create(context, values): def floating_ip_create(context, values):
floating_ip_ref = models.FloatingIp() floating_ip_ref = models.FloatingIp()
floating_ip_ref.update(values) floating_ip_ref.update(values)
try: try:
floating_ip_ref.save() floating_ip_ref.save(context.session)
except db_exc.DBDuplicateEntry: except db_exc.DBDuplicateEntry:
raise exception.FloatingIpExists(address=values['address']) raise exception.FloatingIpExists(address=values['address'])
return floating_ip_ref return floating_ip_ref
def _floating_ip_count_by_project(context, project_id, session=None): def _floating_ip_count_by_project(context, project_id):
nova.context.authorize_project_context(context, project_id) nova.context.authorize_project_context(context, project_id)
# TODO(tr3buchet): why leave auto_assigned floating IPs out? # TODO(tr3buchet): why leave auto_assigned floating IPs out?
return model_query(context, models.FloatingIp, read_deleted="no", return model_query(context, models.FloatingIp, read_deleted="no").\
session=session).\
filter_by(project_id=project_id).\ filter_by(project_id=project_id).\
filter_by(auto_assigned=False).\ filter_by(auto_assigned=False).\
count() count()
@ -895,33 +892,33 @@ def _floating_ip_count_by_project(context, project_id, session=None):
@require_context @require_context
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True) @oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
@main_context_manager.writer
def floating_ip_fixed_ip_associate(context, floating_address, def floating_ip_fixed_ip_associate(context, floating_address,
fixed_address, host): fixed_address, host):
session = get_session() fixed_ip_ref = model_query(context, models.FixedIp).\
with session.begin(): filter_by(address=fixed_address).\
fixed_ip_ref = model_query(context, models.FixedIp, session=session).\ options(joinedload('network')).\
filter_by(address=fixed_address).\ first()
options(joinedload('network')).\ if not fixed_ip_ref:
first() raise exception.FixedIpNotFoundForAddress(address=fixed_address)
if not fixed_ip_ref: rows = model_query(context, models.FloatingIp).\
raise exception.FixedIpNotFoundForAddress(address=fixed_address) filter_by(address=floating_address).\
rows = model_query(context, models.FloatingIp, session=session).\ filter(models.FloatingIp.project_id ==
filter_by(address=floating_address).\ context.project_id).\
filter(models.FloatingIp.project_id == filter(or_(models.FloatingIp.fixed_ip_id ==
context.project_id).\ fixed_ip_ref['id'],
filter(or_(models.FloatingIp.fixed_ip_id == models.FloatingIp.fixed_ip_id.is_(None))).\
fixed_ip_ref['id'], update({'fixed_ip_id': fixed_ip_ref['id'], 'host': host})
models.FloatingIp.fixed_ip_id.is_(None))).\
update({'fixed_ip_id': fixed_ip_ref['id'], 'host': host})
if not rows: if not rows:
raise exception.FloatingIpAssociateFailed(address=floating_address) raise exception.FloatingIpAssociateFailed(address=floating_address)
return fixed_ip_ref return fixed_ip_ref
@require_context @require_context
@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True) @oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
@main_context_manager.writer
def floating_ip_deallocate(context, address): def floating_ip_deallocate(context, address):
return model_query(context, models.FloatingIp).\ return model_query(context, models.FloatingIp).\
filter_by(address=address).\ filter_by(address=address).\
@ -934,6 +931,7 @@ def floating_ip_deallocate(context, address):
@require_context @require_context
@main_context_manager.writer
def floating_ip_destroy(context, address): def floating_ip_destroy(context, address):
model_query(context, models.FloatingIp).\ model_query(context, models.FloatingIp).\
filter_by(address=address).\ filter_by(address=address).\
@ -941,32 +939,30 @@ def floating_ip_destroy(context, address):
@require_context @require_context
@main_context_manager.writer
def floating_ip_disassociate(context, address): def floating_ip_disassociate(context, address):
session = get_session() floating_ip_ref = model_query(context,
with session.begin(): models.FloatingIp).\
floating_ip_ref = model_query(context, filter_by(address=address).\
models.FloatingIp, first()
session=session).\ if not floating_ip_ref:
filter_by(address=address).\ raise exception.FloatingIpNotFoundForAddress(address=address)
first()
if not floating_ip_ref:
raise exception.FloatingIpNotFoundForAddress(address=address)
fixed_ip_ref = model_query(context, models.FixedIp, session=session).\ fixed_ip_ref = model_query(context, models.FixedIp).\
filter_by(id=floating_ip_ref['fixed_ip_id']).\ filter_by(id=floating_ip_ref['fixed_ip_id']).\
options(joinedload('network')).\ options(joinedload('network')).\
first() first()
floating_ip_ref.fixed_ip_id = None floating_ip_ref.fixed_ip_id = None
floating_ip_ref.host = None floating_ip_ref.host = None
return fixed_ip_ref return fixed_ip_ref
def _floating_ip_get_all(context, session=None): def _floating_ip_get_all(context):
return model_query(context, models.FloatingIp, read_deleted="no", return model_query(context, models.FloatingIp, read_deleted="no")
session=session)
@main_context_manager.reader
def floating_ip_get_all(context): def floating_ip_get_all(context):
floating_ip_refs = _floating_ip_get_all(context).\ floating_ip_refs = _floating_ip_get_all(context).\
options(joinedload('fixed_ip')).\ options(joinedload('fixed_ip')).\
@ -976,6 +972,7 @@ def floating_ip_get_all(context):
return floating_ip_refs return floating_ip_refs
@main_context_manager.reader
def floating_ip_get_all_by_host(context, host): def floating_ip_get_all_by_host(context, host):
floating_ip_refs = _floating_ip_get_all(context).\ floating_ip_refs = _floating_ip_get_all(context).\
filter_by(host=host).\ filter_by(host=host).\
@ -987,6 +984,7 @@ def floating_ip_get_all_by_host(context, host):
@require_context @require_context
@main_context_manager.reader
def floating_ip_get_all_by_project(context, project_id): def floating_ip_get_all_by_project(context, project_id):
nova.context.authorize_project_context(context, project_id) nova.context.authorize_project_context(context, project_id)
# TODO(tr3buchet): why do we not want auto_assigned floating IPs here? # TODO(tr3buchet): why do we not want auto_assigned floating IPs here?
@ -998,17 +996,18 @@ def floating_ip_get_all_by_project(context, project_id):
@require_context @require_context
@main_context_manager.reader
def floating_ip_get_by_address(context, address): def floating_ip_get_by_address(context, address):
return _floating_ip_get_by_address(context, address) return _floating_ip_get_by_address(context, address)
def _floating_ip_get_by_address(context, address, session=None): def _floating_ip_get_by_address(context, address):
# if address string is empty explicitly set it to None # if address string is empty explicitly set it to None
if not address: if not address:
address = None address = None
try: try:
result = model_query(context, models.FloatingIp, session=session).\ result = model_query(context, models.FloatingIp).\
filter_by(address=address).\ filter_by(address=address).\
options(joinedload_all('fixed_ip.instance')).\ options(joinedload_all('fixed_ip.instance')).\
first() first()
@ -1029,6 +1028,7 @@ def _floating_ip_get_by_address(context, address, session=None):
@require_context @require_context
@main_context_manager.reader
def floating_ip_get_by_fixed_address(context, fixed_address): def floating_ip_get_by_fixed_address(context, fixed_address):
return model_query(context, models.FloatingIp).\ return model_query(context, models.FloatingIp).\
outerjoin(models.FixedIp, outerjoin(models.FixedIp,
@ -1039,6 +1039,7 @@ def floating_ip_get_by_fixed_address(context, fixed_address):
@require_context @require_context
@main_context_manager.reader
def floating_ip_get_by_fixed_ip_id(context, fixed_ip_id): def floating_ip_get_by_fixed_ip_id(context, fixed_ip_id):
return model_query(context, models.FloatingIp).\ return model_query(context, models.FloatingIp).\
filter_by(fixed_ip_id=fixed_ip_id).\ filter_by(fixed_ip_id=fixed_ip_id).\
@ -1046,16 +1047,15 @@ def floating_ip_get_by_fixed_ip_id(context, fixed_ip_id):
@require_context @require_context
@main_context_manager.writer
def floating_ip_update(context, address, values): def floating_ip_update(context, address, values):
session = get_session() float_ip_ref = _floating_ip_get_by_address(context, address)
with session.begin(): float_ip_ref.update(values)
float_ip_ref = _floating_ip_get_by_address(context, address, session) try:
float_ip_ref.update(values) float_ip_ref.save(context.session)
try: except db_exc.DBDuplicateEntry:
float_ip_ref.save(session=session) raise exception.FloatingIpExists(address=values['address'])
except db_exc.DBDuplicateEntry: return float_ip_ref
raise exception.FloatingIpExists(address=values['address'])
return float_ip_ref
################### ###################