Merged from trunk and resolved conflicts

This commit is contained in:
Tushar Patil 2011-09-15 10:43:42 -07:00
commit 8fe228298a
3 changed files with 121 additions and 39 deletions
nova

@ -261,11 +261,13 @@ def floating_ip_disassociate(context, address):
return IMPL.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.""" """Associate an floating ip to a fixed_ip by address."""
return IMPL.floating_ip_fixed_ip_associate(context, return IMPL.floating_ip_fixed_ip_associate(context,
floating_address, floating_address,
fixed_address) fixed_address,
host)
def floating_ip_get_all(context): def floating_ip_get_all(context):
@ -324,13 +326,15 @@ def migration_get_by_instance_and_status(context, instance_uuid, status):
#################### ####################
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. """Associate fixed ip to instance.
Raises if fixed ip is not available. 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): def fixed_ip_associate_pool(context, network_id, instance_id=None, host=None):
@ -365,7 +369,7 @@ def fixed_ip_get_all(context):
def fixed_ip_get_all_by_instance_host(context, host): def fixed_ip_get_all_by_instance_host(context, host):
"""Get all allocated fixed ips filtered by instance 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): def fixed_ip_get_by_address(context, address):
@ -420,6 +424,11 @@ def virtual_interface_get_by_address(context, address):
return IMPL.virtual_interface_get_by_address(context, address) return IMPL.virtual_interface_get_by_address(context, address)
def virtual_interface_get_by_uuid(context, vif_uuid):
"""Gets a virtual interface from the table filtering on vif uuid."""
return IMPL.virtual_interface_get_by_uuid(context, vif_uuid)
def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): def virtual_interface_get_by_fixed_ip(context, fixed_ip_id):
"""Gets the virtual interface fixed_ip is associated with.""" """Gets the virtual interface fixed_ip is associated with."""
return IMPL.virtual_interface_get_by_fixed_ip(context, fixed_ip_id) return IMPL.virtual_interface_get_by_fixed_ip(context, fixed_ip_id)
@ -496,9 +505,20 @@ def instance_get_all_by_filters(context, filters):
return IMPL.instance_get_all_by_filters(context, filters) return IMPL.instance_get_all_by_filters(context, filters)
def instance_get_active_by_window(context, begin, end=None): def instance_get_active_by_window(context, begin, end=None, project_id=None):
"""Get instances active during a certain time window.""" """Get instances active during a certain time window.
return IMPL.instance_get_active_by_window(context, begin, end)
Specifying a project_id will filter for a certain project."""
return IMPL.instance_get_active_by_window(context, begin, end, project_id)
def instance_get_active_by_window_joined(context, begin, end=None,
project_id=None):
"""Get instances and joins active during a certain time window.
Specifying a project_id will filter for a certain project."""
return IMPL.instance_get_active_by_window_joined(context, begin, end,
project_id)
def instance_get_all_by_user(context, user_id): def instance_get_all_by_user(context, user_id):
@ -704,6 +724,11 @@ def network_get_by_bridge(context, bridge):
return IMPL.network_get_by_bridge(context, bridge) return IMPL.network_get_by_bridge(context, bridge)
def network_get_by_uuid(context, uuid):
"""Get a network by uuid or raise if it does not exist."""
return IMPL.network_get_by_uuid(context, uuid)
def network_get_by_cidr(context, cidr): def network_get_by_cidr(context, cidr):
"""Get a network by cidr or raise if it does not exist""" """Get a network by cidr or raise if it does not exist"""
return IMPL.network_get_by_cidr(context, cidr) return IMPL.network_get_by_cidr(context, cidr)

@ -28,6 +28,7 @@ from nova import flags
from nova import ipv6 from nova import ipv6
from nova import utils from nova import utils
from nova import log as logging from nova import log as logging
from nova.compute import vm_states
from nova.db.sqlalchemy import models from nova.db.sqlalchemy import models
from nova.db.sqlalchemy.session import get_session from nova.db.sqlalchemy.session import get_session
from sqlalchemy import or_ from sqlalchemy import or_
@ -35,6 +36,7 @@ from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import joinedload from sqlalchemy.orm import joinedload
from sqlalchemy.orm import joinedload_all from sqlalchemy.orm import joinedload_all
from sqlalchemy.sql import func from sqlalchemy.sql import func
from sqlalchemy.sql.expression import desc
from sqlalchemy.sql.expression import literal_column from sqlalchemy.sql.expression import literal_column
FLAGS = flags.FLAGS FLAGS = flags.FLAGS
@ -527,7 +529,8 @@ def floating_ip_count_by_project(context, project_id):
@require_context @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() session = get_session()
with session.begin(): with session.begin():
# TODO(devcamcar): How to ensure floating_id belongs to user? # TODO(devcamcar): How to ensure floating_id belongs to user?
@ -538,6 +541,7 @@ def floating_ip_fixed_ip_associate(context, floating_address, fixed_address):
fixed_address, fixed_address,
session=session) session=session)
floating_ip_ref.fixed_ip = fixed_ip_ref floating_ip_ref.fixed_ip = fixed_ip_ref
floating_ip_ref.host = host
floating_ip_ref.save(session=session) floating_ip_ref.save(session=session)
@ -581,6 +585,7 @@ def floating_ip_disassociate(context, address):
else: else:
fixed_ip_address = None fixed_ip_address = None
floating_ip_ref.fixed_ip = None floating_ip_ref.fixed_ip = None
floating_ip_ref.host = None
floating_ip_ref.save(session=session) floating_ip_ref.save(session=session)
return fixed_ip_address return fixed_ip_address
@ -667,14 +672,19 @@ def floating_ip_update(context, address, values):
@require_admin_context @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() session = get_session()
with session.begin(): with session.begin():
network_or_none = or_(models.FixedIp.network_id == network_id, network_or_none = or_(models.FixedIp.network_id == network_id,
models.FixedIp.network_id == None) models.FixedIp.network_id == None)
fixed_ip_ref = session.query(models.FixedIp).\ fixed_ip_ref = session.query(models.FixedIp).\
filter(network_or_none).\ filter(network_or_none).\
filter_by(reserved=False).\ filter_by(reserved=reserved).\
filter_by(deleted=False).\ filter_by(deleted=False).\
filter_by(address=address).\ filter_by(address=address).\
with_lockmode('update').\ with_lockmode('update').\
@ -942,6 +952,22 @@ def virtual_interface_get_by_address(context, address):
return vif_ref return vif_ref
@require_context
def virtual_interface_get_by_uuid(context, vif_uuid):
"""Gets a virtual interface from the table.
:param vif_uuid: the uuid of the interface you're looking to get
"""
session = get_session()
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
@require_context @require_context
def virtual_interface_get_by_fixed_ip(context, fixed_ip_id): def virtual_interface_get_by_fixed_ip(context, fixed_ip_id):
"""Gets the virtual interface fixed_ip is associated with. """Gets the virtual interface fixed_ip is associated with.
@ -1102,12 +1128,11 @@ def instance_destroy(context, instance_id):
def instance_stop(context, instance_id): def instance_stop(context, instance_id):
session = get_session() session = get_session()
with session.begin(): with session.begin():
from nova.compute import power_state
session.query(models.Instance).\ session.query(models.Instance).\
filter_by(id=instance_id).\ filter_by(id=instance_id).\
update({'host': None, update({'host': None,
'state': power_state.SHUTOFF, 'vm_state': vm_states.STOPPED,
'state_description': 'stopped', 'task_state': None,
'updated_at': literal_column('updated_at')}) 'updated_at': literal_column('updated_at')})
session.query(models.SecurityGroupInstanceAssociation).\ session.query(models.SecurityGroupInstanceAssociation).\
filter_by(instance_id=instance_id).\ filter_by(instance_id=instance_id).\
@ -1250,12 +1275,17 @@ def instance_get_all_by_filters(context, filters):
options(joinedload_all('fixed_ips.network')).\ options(joinedload_all('fixed_ips.network')).\
options(joinedload('metadata')).\ options(joinedload('metadata')).\
options(joinedload('instance_type')).\ options(joinedload('instance_type')).\
filter_by(deleted=can_read_deleted(context)) order_by(desc(models.Instance.created_at))
# Make a copy of the filters dictionary to use going forward, as we'll # Make a copy of the filters dictionary to use going forward, as we'll
# be modifying it and we shouldn't affect the caller's use of it. # be modifying it and we shouldn't affect the caller's use of it.
filters = filters.copy() filters = filters.copy()
if 'changes-since' in filters:
changes_since = filters['changes-since']
query_prefix = query_prefix.\
filter(models.Instance.updated_at > changes_since)
if not context.is_admin: if not context.is_admin:
# If we're not admin context, add appropriate filter.. # If we're not admin context, add appropriate filter..
if context.project_id: if context.project_id:
@ -1266,7 +1296,7 @@ def instance_get_all_by_filters(context, filters):
# Filters for exact matches that we can do along with the SQL query... # 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 # For other filters that don't match this, we will do regexp matching
exact_match_filter_names = ['project_id', 'user_id', 'image_ref', exact_match_filter_names = ['project_id', 'user_id', 'image_ref',
'state', 'instance_type_id', 'deleted'] 'vm_state', 'instance_type_id', 'deleted']
query_filters = [key for key in filters.iterkeys() query_filters = [key for key in filters.iterkeys()
if key in exact_match_filter_names] if key in exact_match_filter_names]
@ -1277,9 +1307,7 @@ 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.\ instances = query_prefix.all()
filter_by(deleted=can_read_deleted(context)).\
all()
if not instances: if not instances:
return [] return []
@ -1306,21 +1334,40 @@ def instance_get_all_by_filters(context, filters):
return instances return instances
@require_admin_context @require_context
def instance_get_active_by_window(context, begin, end=None): def instance_get_active_by_window(context, begin, end=None, project_id=None):
"""Return instances that were continuously active over the given window""" """Return instances that were continuously active over window."""
session = get_session() session = get_session()
query = session.query(models.Instance).\ query = session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\ filter(models.Instance.launched_at < begin)
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('instance_type')).\
filter(models.Instance.launched_at < begin)
if end: if end:
query = query.filter(or_(models.Instance.terminated_at == None, query = query.filter(or_(models.Instance.terminated_at == None,
models.Instance.terminated_at > end)) models.Instance.terminated_at > end))
else: else:
query = query.filter(models.Instance.terminated_at == None) query = query.filter(models.Instance.terminated_at == None)
if project_id:
query = query.filter_by(project_id=project_id)
return query.all()
@require_admin_context
def instance_get_active_by_window_joined(context, begin, end=None,
project_id=None):
"""Return instances and joins that were continuously active over window."""
session = get_session()
query = session.query(models.Instance).\
options(joinedload_all('fixed_ips.floating_ips')).\
options(joinedload('security_groups')).\
options(joinedload_all('fixed_ips.network')).\
options(joinedload('instance_type')).\
filter(models.Instance.launched_at < begin)
if end:
query = query.filter(or_(models.Instance.terminated_at == None,
models.Instance.terminated_at > end))
else:
query = query.filter(models.Instance.terminated_at == None)
if project_id:
query = query.filter_by(project_id=project_id)
return query.all() return query.all()
@ -1484,18 +1531,6 @@ def instance_get_floating_address(context, instance_id):
return fixed_ip_refs[0].floating_ips[0]['address'] return fixed_ip_refs[0].floating_ips[0]['address']
@require_admin_context
def instance_set_state(context, instance_id, state, description=None):
# TODO(devcamcar): Move this out of models and into driver
from nova.compute import power_state
if not description:
description = power_state.name(state)
db.instance_update(context,
instance_id,
{'state': state,
'state_description': description})
@require_context @require_context
def instance_update(context, instance_id, values): def instance_update(context, instance_id, values):
session = get_session() session = get_session()
@ -1846,6 +1881,19 @@ def network_get_by_bridge(context, bridge):
return result return result
@require_admin_context
def network_get_by_uuid(context, uuid):
session = get_session()
result = session.query(models.Network).\
filter_by(uuid=uuid).\
filter_by(deleted=False).\
first()
if not result:
raise exception.NetworkNotFoundForUUID(uuid=uuid)
return result
@require_admin_context @require_admin_context
def network_get_by_cidr(context, cidr): def network_get_by_cidr(context, cidr):
session = get_session() session = get_session()

@ -901,3 +901,12 @@ def monkey_patch():
func = import_class("%s.%s" % (module, key)) func = import_class("%s.%s" % (module, key))
setattr(sys.modules[module], key,\ setattr(sys.modules[module], key,\
decorator("%s.%s" % (module, key), func)) decorator("%s.%s" % (module, key), func))
def convert_to_list_dict(lst, label):
"""Convert a value or list into a list of dicts"""
if not lst:
return None
if not isinstance(lst, list):
lst = [lst]
return [{label: x} for x in lst]