merged trunk

This commit is contained in:
Vishvananda Ishaya 2011-09-21 02:00:07 -07:00
commit af21c968c0
2 changed files with 84 additions and 84 deletions
nova/db
api.py
sqlalchemy

@ -261,11 +261,13 @@ def floating_ip_disassociate(context, address):
return IMPL.floating_ip_disassociate(context, address)
def floating_ip_fixed_ip_associate(context, floating_address, fixed_address):
def floating_ip_fixed_ip_associate(context, floating_address,
fixed_address, host):
"""Associate an floating ip to a fixed_ip by address."""
return IMPL.floating_ip_fixed_ip_associate(context,
floating_address,
fixed_address)
fixed_address,
host)
def floating_ip_get_all(context):
@ -321,16 +323,23 @@ def migration_get_by_instance_and_status(context, instance_uuid, status):
status)
def migration_get_all_unconfirmed(context, confirm_window):
"""Finds all unconfirmed migrations within the confirmation window."""
return IMPL.migration_get_all_unconfirmed(context, confirm_window)
####################
def fixed_ip_associate(context, address, instance_id, network_id=None):
def fixed_ip_associate(context, address, instance_id, network_id=None,
reserved=False):
"""Associate fixed ip to instance.
Raises if fixed ip is not available.
"""
return IMPL.fixed_ip_associate(context, address, instance_id, network_id)
return IMPL.fixed_ip_associate(context, address, instance_id, network_id,
reserved)
def fixed_ip_associate_pool(context, network_id, instance_id=None, host=None):
@ -365,7 +374,7 @@ def fixed_ip_get_all(context):
def fixed_ip_get_all_by_instance_host(context, host):
"""Get all allocated fixed ips filtered by instance host."""
return IMPL.fixed_ip_get_all_instance_by_host(context, host)
return IMPL.fixed_ip_get_all_by_instance_host(context, host)
def fixed_ip_get_by_address(context, address):
@ -458,6 +467,11 @@ def 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)
####################
@ -602,6 +616,11 @@ def instance_get_actions(context, instance_id):
return IMPL.instance_get_actions(context, instance_id)
def instance_get_id_to_uuid_mapping(context, ids):
"""Return a dictionary containing 'ID: UUID' given the ids"""
return IMPL.instance_get_id_to_uuid_mapping(context, ids)
###################

@ -15,9 +15,10 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Implementation of SQLAlchemy backend.
"""
"""Implementation of SQLAlchemy backend."""
import datetime
import re
import warnings
@ -529,7 +530,8 @@ def floating_ip_count_by_project(context, project_id):
@require_context
def floating_ip_fixed_ip_associate(context, floating_address, fixed_address):
def floating_ip_fixed_ip_associate(context, floating_address,
fixed_address, host):
session = get_session()
with session.begin():
# TODO(devcamcar): How to ensure floating_id belongs to user?
@ -540,6 +542,7 @@ def floating_ip_fixed_ip_associate(context, floating_address, fixed_address):
fixed_address,
session=session)
floating_ip_ref.fixed_ip = fixed_ip_ref
floating_ip_ref.host = host
floating_ip_ref.save(session=session)
@ -583,6 +586,7 @@ def floating_ip_disassociate(context, address):
else:
fixed_ip_address = None
floating_ip_ref.fixed_ip = None
floating_ip_ref.host = None
floating_ip_ref.save(session=session)
return fixed_ip_address
@ -669,14 +673,19 @@ def floating_ip_update(context, address, values):
@require_admin_context
def fixed_ip_associate(context, address, instance_id, network_id=None):
def fixed_ip_associate(context, address, instance_id, network_id=None,
reserved=False):
"""Keyword arguments:
reserved -- should be a boolean value(True or False), exact value will be
used to filter on the fixed ip address
"""
session = get_session()
with session.begin():
network_or_none = or_(models.FixedIp.network_id == network_id,
models.FixedIp.network_id == None)
fixed_ip_ref = session.query(models.FixedIp).\
filter(network_or_none).\
filter_by(reserved=False).\
filter_by(reserved=reserved).\
filter_by(deleted=False).\
filter_by(address=address).\
with_lockmode('update').\
@ -922,7 +931,6 @@ def virtual_interface_get(context, vif_id, session=None):
vif_ref = session.query(models.VirtualInterface).\
filter_by(id=vif_id).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
first()
return vif_ref
@ -938,7 +946,6 @@ def virtual_interface_get_by_address(context, address):
vif_ref = session.query(models.VirtualInterface).\
filter_by(address=address).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
first()
return vif_ref
@ -954,7 +961,6 @@ def virtual_interface_get_by_uuid(context, vif_uuid):
vif_ref = session.query(models.VirtualInterface).\
filter_by(uuid=vif_uuid).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
first()
return vif_ref
@ -970,7 +976,6 @@ def virtual_interface_get_by_fixed_ip(context, fixed_ip_id):
vif_ref = session.query(models.VirtualInterface).\
filter_by(fixed_ip_id=fixed_ip_id).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
first()
return vif_ref
@ -987,7 +992,6 @@ def virtual_interface_get_by_instance(context, instance_id):
vif_refs = session.query(models.VirtualInterface).\
filter_by(instance_id=instance_id).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
all()
return vif_refs
@ -1002,7 +1006,6 @@ def virtual_interface_get_by_instance_and_network(context, instance_id,
filter_by(instance_id=instance_id).\
filter_by(network_id=network_id).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
first()
return vif_ref
@ -1018,7 +1021,6 @@ def virtual_interface_get_by_network(context, network_id):
vif_refs = session.query(models.VirtualInterface).\
filter_by(network_id=network_id).\
options(joinedload('network')).\
options(joinedload('instance')).\
options(joinedload('fixed_ips')).\
all()
return vif_refs
@ -1048,6 +1050,17 @@ def virtual_interface_delete_by_instance(context, instance_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
###################
@ -1164,7 +1177,6 @@ def _build_instance_get(context, session=None):
partial = session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('virtual_interfaces')).\
options(joinedload_all('security_groups.rules')).\
options(joinedload('volumes')).\
options(joinedload('metadata')).\
@ -1183,10 +1195,6 @@ def instance_get_all(context):
session = get_session()
return 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_all('fixed_ips.network')).\
options(joinedload('metadata')).\
@ -1201,27 +1209,6 @@ def instance_get_all_by_filters(context, filters):
will be returned by default, unless there's a filter that says
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):
inst_metadata = [{node['key']: node['value']} \
for node in instance['metadata']]
@ -1248,7 +1235,7 @@ def instance_get_all_by_filters(context, filters):
"""Do exact match against a column. value to match can be a list
so you can match any value in the list.
"""
if isinstance(value, list):
if isinstance(value, list) or isinstance(value, set):
column_attr = getattr(models.Instance, column)
return query.filter(column_attr.in_(value))
else:
@ -1258,13 +1245,7 @@ def instance_get_all_by_filters(context, filters):
session = get_session()
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_all('fixed_ips.network')).\
options(joinedload('metadata')).\
options(joinedload('instance_type')).\
order_by(desc(models.Instance.created_at))
@ -1288,7 +1269,7 @@ def instance_get_all_by_filters(context, filters):
# Filters for exact matches that we can do along with the SQL query...
# For other filters that don't match this, we will do regexp matching
exact_match_filter_names = ['project_id', 'user_id', 'image_ref',
'vm_state', 'instance_type_id', 'deleted']
'vm_state', 'instance_type_id', 'deleted', 'uuid']
query_filters = [key for key in filters.iterkeys()
if key in exact_match_filter_names]
@ -1300,15 +1281,13 @@ def instance_get_all_by_filters(context, filters):
filters.pop(filter_name))
instances = query_prefix.all()
if not instances:
return []
# Now filter on everything else for regexp matching..
# For filters not in the list, we'll attempt to use the filter_name
# as a column name in Instance..
regexp_filter_funcs = {'ip6': _regexp_filter_by_ipv6,
'ip': _regexp_filter_by_ip}
regexp_filter_funcs = {}
for filter_name in filters.iterkeys():
filter_func = regexp_filter_funcs.get(filter_name, None)
@ -1322,6 +1301,8 @@ def instance_get_all_by_filters(context, filters):
filter_l = lambda instance: _regexp_filter_by_column(instance,
filter_name, filter_re)
instances = filter(filter_l, instances)
if not instances:
break
return instances
@ -1368,7 +1349,6 @@ def instance_get_all_by_user(context, user_id):
session = get_session()
return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\
@ -1383,7 +1363,6 @@ def instance_get_all_by_host(context, host):
session = get_session()
return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\
@ -1400,7 +1379,6 @@ def instance_get_all_by_project(context, project_id):
session = get_session()
return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\
@ -1416,7 +1394,6 @@ def instance_get_all_by_reservation(context, reservation_id):
query = session.query(models.Instance).\
filter_by(reservation_id=reservation_id).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\
@ -1433,36 +1410,11 @@ def instance_get_all_by_reservation(context, reservation_id):
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
def instance_get_project_vpn(context, project_id):
session = get_session()
return session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('virtual_interfaces')).\
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\
@ -1594,6 +1546,18 @@ def instance_get_actions(context, instance_id):
all()
@require_context
def instance_get_id_to_uuid_mapping(context, ids):
session = get_session()
instances = session.query(models.Instance).\
filter(models.Instance.id.in_(ids)).\
all()
mapping = {}
for instance in instances:
mapping[instance['id']] = instance['uuid']
return mapping
###################
@ -2819,12 +2783,14 @@ def security_group_rule_get_by_security_group(context, security_group_id,
result = session.query(models.SecurityGroupIngressRule).\
filter_by(deleted=can_read_deleted(context)).\
filter_by(parent_group_id=security_group_id).\
options(joinedload_all('grantee_group')).\
all()
else:
# TODO(vish): Join to group and check for project_id
result = session.query(models.SecurityGroupIngressRule).\
filter_by(deleted=False).\
filter_by(parent_group_id=security_group_id).\
options(joinedload_all('grantee_group')).\
all()
return result
@ -3189,6 +3155,21 @@ def migration_get_by_instance_and_status(context, instance_uuid, status):
return result
@require_admin_context
def migration_get_all_unconfirmed(context, confirm_window, session=None):
confirm_window = datetime.datetime.utcnow() - datetime.timedelta(
seconds=confirm_window)
if not session:
session = get_session()
results = session.query(models.Migration).\
filter(models.Migration.updated_at <= confirm_window).\
filter_by(status="FINISHED").all()
return results
##################