* Remove the foreign key and backrefs tying vif<->instance

* Update instance filtering to pass ip related filters to the network manager
* move/update tests
This commit is contained in:
Jason Koelker 2011-09-19 20:16:49 +00:00 committed by Tarmac
commit a7b3eac4e7
2 changed files with 57 additions and 75 deletions
nova/db
api.py
sqlalchemy

@ -462,6 +462,11 @@ def virtual_interface_delete_by_instance(context, instance_id):
return IMPL.virtual_interface_delete_by_instance(context, instance_id) return IMPL.virtual_interface_delete_by_instance(context, instance_id)
def virtual_interface_get_all(context):
"""Gets all virtual interfaces from the table,"""
return IMPL.virtual_interface_get_all(context)
#################### ####################
@ -500,9 +505,10 @@ def instance_get_all(context):
return IMPL.instance_get_all(context) return IMPL.instance_get_all(context)
def instance_get_all_by_filters(context, filters): def instance_get_all_by_filters(context, filters, instance_uuids=None):
"""Get all instances that match all filters.""" """Get all instances that match all filters."""
return IMPL.instance_get_all_by_filters(context, filters) return IMPL.instance_get_all_by_filters(context, filters,
instance_uuids=instance_uuids)
def instance_get_active_by_window(context, begin, end=None, project_id=None): def instance_get_active_by_window(context, begin, end=None, project_id=None):
@ -606,6 +612,11 @@ def instance_get_actions(context, instance_id):
return IMPL.instance_get_actions(context, instance_id) return IMPL.instance_get_actions(context, instance_id)
def instance_get_uuids_by_ids(context, ids):
"""Return the UUIDs of the instances given the ids"""
return IMPL.instance_get_uuids_by_ids(context, ids)
################### ###################

@ -930,7 +930,6 @@ def virtual_interface_get(context, vif_id, session=None):
vif_ref = session.query(models.VirtualInterface).\ vif_ref = session.query(models.VirtualInterface).\
filter_by(id=vif_id).\ filter_by(id=vif_id).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
first() first()
return vif_ref return vif_ref
@ -946,7 +945,6 @@ def virtual_interface_get_by_address(context, address):
vif_ref = session.query(models.VirtualInterface).\ vif_ref = session.query(models.VirtualInterface).\
filter_by(address=address).\ filter_by(address=address).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
first() first()
return vif_ref return vif_ref
@ -962,7 +960,6 @@ def virtual_interface_get_by_uuid(context, vif_uuid):
vif_ref = session.query(models.VirtualInterface).\ vif_ref = session.query(models.VirtualInterface).\
filter_by(uuid=vif_uuid).\ filter_by(uuid=vif_uuid).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
first() first()
return vif_ref return vif_ref
@ -978,7 +975,6 @@ def virtual_interface_get_by_fixed_ip(context, fixed_ip_id):
vif_ref = session.query(models.VirtualInterface).\ vif_ref = session.query(models.VirtualInterface).\
filter_by(fixed_ip_id=fixed_ip_id).\ filter_by(fixed_ip_id=fixed_ip_id).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
first() first()
return vif_ref return vif_ref
@ -995,7 +991,6 @@ def virtual_interface_get_by_instance(context, instance_id):
vif_refs = session.query(models.VirtualInterface).\ vif_refs = session.query(models.VirtualInterface).\
filter_by(instance_id=instance_id).\ filter_by(instance_id=instance_id).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
all() all()
return vif_refs return vif_refs
@ -1010,7 +1005,6 @@ def virtual_interface_get_by_instance_and_network(context, instance_id,
filter_by(instance_id=instance_id).\ filter_by(instance_id=instance_id).\
filter_by(network_id=network_id).\ filter_by(network_id=network_id).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
first() first()
return vif_ref return vif_ref
@ -1026,7 +1020,6 @@ def virtual_interface_get_by_network(context, network_id):
vif_refs = session.query(models.VirtualInterface).\ vif_refs = session.query(models.VirtualInterface).\
filter_by(network_id=network_id).\ filter_by(network_id=network_id).\
options(joinedload('network')).\ options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\ options(joinedload('fixed_ips')).\
all() all()
return vif_refs return vif_refs
@ -1056,6 +1049,17 @@ def virtual_interface_delete_by_instance(context, instance_id):
virtual_interface_delete(context, vif_ref['id']) virtual_interface_delete(context, vif_ref['id'])
@require_context
def virtual_interface_get_all(context):
"""Get all vifs"""
session = get_session()
vif_refs = session.query(models.VirtualInterface).\
options(joinedload('network')).\
options(joinedload('fixed_ips')).\
all()
return vif_refs
################### ###################
@ -1172,7 +1176,6 @@ def _build_instance_get(context, session=None):
partial = session.query(models.Instance).\ partial = session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('virtual_interfaces')).\
options(joinedload_all('security_groups.rules')).\ options(joinedload_all('security_groups.rules')).\
options(joinedload('volumes')).\ options(joinedload('volumes')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1191,10 +1194,6 @@ def instance_get_all(context):
session = get_session() session = get_session()
return session.query(models.Instance).\ return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload_all('virtual_interfaces.network')).\
options(joinedload_all(
'virtual_interfaces.fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces.instance')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1204,32 +1203,11 @@ def instance_get_all(context):
@require_context @require_context
def instance_get_all_by_filters(context, filters): def instance_get_all_by_filters(context, filters, instance_uuids=None):
"""Return instances that match all filters. Deleted instances """Return instances that match all filters. Deleted instances
will be returned by default, unless there's a filter that says will be returned by default, unless there's a filter that says
otherwise""" otherwise"""
def _regexp_filter_by_ipv6(instance, filter_re):
for interface in instance['virtual_interfaces']:
fixed_ipv6 = interface.get('fixed_ipv6')
if fixed_ipv6 and filter_re.match(fixed_ipv6):
return True
return False
def _regexp_filter_by_ip(instance, filter_re):
for interface in instance['virtual_interfaces']:
for fixed_ip in interface['fixed_ips']:
if not fixed_ip or not fixed_ip['address']:
continue
if filter_re.match(fixed_ip['address']):
return True
for floating_ip in fixed_ip.get('floating_ips', []):
if not floating_ip or not floating_ip['address']:
continue
if filter_re.match(floating_ip['address']):
return True
return False
def _regexp_filter_by_metadata(instance, meta): def _regexp_filter_by_metadata(instance, meta):
inst_metadata = [{node['key']: node['value']} \ inst_metadata = [{node['key']: node['value']} \
for node in instance['metadata']] for node in instance['metadata']]
@ -1266,13 +1244,7 @@ def instance_get_all_by_filters(context, filters):
session = get_session() session = get_session()
query_prefix = session.query(models.Instance).\ query_prefix = session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload_all('virtual_interfaces.network')).\
options(joinedload_all(
'virtual_interfaces.fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces.instance')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
options(joinedload('instance_type')).\ options(joinedload('instance_type')).\
order_by(desc(models.Instance.created_at)) order_by(desc(models.Instance.created_at))
@ -1307,16 +1279,34 @@ def instance_get_all_by_filters(context, filters):
query_prefix = _exact_match_filter(query_prefix, filter_name, query_prefix = _exact_match_filter(query_prefix, filter_name,
filters.pop(filter_name)) filters.pop(filter_name))
instances = query_prefix.all() db_instances = query_prefix.all()
db_uuids = [instance['uuid'] for instance in db_instances \
if instance['uuid']]
if not instances: if instance_uuids is None:
instance_uuids = []
uuids = []
# NOTE(jkoelker): String UUIDs only!
if not instance_uuids:
uuids = db_uuids
elif not db_instances:
uuids = instance_uuids
else:
uuids = list(set(instance_uuids) & set(db_uuids))
if not uuids:
return [] return []
instances = session.query(models.Instance).\
filter(models.Instance.uuid.in_(uuids)).\
all()
# Now filter on everything else for regexp matching.. # Now filter on everything else for regexp matching..
# For filters not in the list, we'll attempt to use the filter_name # For filters not in the list, we'll attempt to use the filter_name
# as a column name in Instance.. # as a column name in Instance..
regexp_filter_funcs = {'ip6': _regexp_filter_by_ipv6, regexp_filter_funcs = {}
'ip': _regexp_filter_by_ip}
for filter_name in filters.iterkeys(): for filter_name in filters.iterkeys():
filter_func = regexp_filter_funcs.get(filter_name, None) filter_func = regexp_filter_funcs.get(filter_name, None)
@ -1376,7 +1366,6 @@ def instance_get_all_by_user(context, user_id):
session = get_session() session = get_session()
return session.query(models.Instance).\ return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1391,7 +1380,6 @@ def instance_get_all_by_host(context, host):
session = get_session() session = get_session()
return session.query(models.Instance).\ return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1408,7 +1396,6 @@ def instance_get_all_by_project(context, project_id):
session = get_session() session = get_session()
return session.query(models.Instance).\ return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1424,7 +1411,6 @@ def instance_get_all_by_reservation(context, reservation_id):
query = session.query(models.Instance).\ query = session.query(models.Instance).\
filter_by(reservation_id=reservation_id).\ filter_by(reservation_id=reservation_id).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1441,36 +1427,11 @@ def instance_get_all_by_reservation(context, reservation_id):
all() all()
@require_context
def instance_get_by_fixed_ip(context, address):
"""Return instance ref by exact match of FixedIP"""
fixed_ip_ref = fixed_ip_get_by_address(context, address)
return fixed_ip_ref.instance
@require_context
def instance_get_by_fixed_ipv6(context, address):
"""Return instance ref by exact match of IPv6"""
session = get_session()
# convert IPv6 address to mac
mac = ipv6.to_mac(address)
# get virtual interface
vif_ref = virtual_interface_get_by_address(context, mac)
# look up instance based on instance_id from vif row
result = session.query(models.Instance).\
filter_by(id=vif_ref['instance_id'])
return result
@require_admin_context @require_admin_context
def instance_get_project_vpn(context, project_id): def instance_get_project_vpn(context, project_id):
session = get_session() session = get_session()
return session.query(models.Instance).\ return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\ options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
@ -1602,6 +1563,16 @@ def instance_get_actions(context, instance_id):
all() all()
@require_context
def instance_get_uuids_by_ids(context, ids):
session = get_session()
instances = session.query(models.Instance).\
filter(models.Instance.id.in_(ids)).\
all()
return [{'uuid': instance['uuid'],
'id': instance['id']} for instance in instances]
################### ###################