Merge trunk
This commit is contained in:
commit
4e1fbd8bfa
@ -420,6 +420,11 @@ def 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):
|
||||
"""Gets the virtual interface fixed_ip is associated with."""
|
||||
return IMPL.virtual_interface_get_by_fixed_ip(context, fixed_ip_id)
|
||||
@ -496,9 +501,20 @@ def 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):
|
||||
"""Get instances active during a certain time window."""
|
||||
return IMPL.instance_get_active_by_window(context, begin, end)
|
||||
def instance_get_active_by_window(context, begin, end=None, project_id=None):
|
||||
"""Get instances active during a certain time window.
|
||||
|
||||
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):
|
||||
@ -704,6 +720,11 @@ def 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):
|
||||
"""Get a network by cidr or raise if it does not exist"""
|
||||
return IMPL.network_get_by_cidr(context, cidr)
|
||||
|
@ -28,6 +28,7 @@ from nova import flags
|
||||
from nova import ipv6
|
||||
from nova import utils
|
||||
from nova import log as logging
|
||||
from nova.compute import vm_states
|
||||
from nova.db.sqlalchemy import models
|
||||
from nova.db.sqlalchemy.session import get_session
|
||||
from sqlalchemy import or_
|
||||
@ -35,6 +36,7 @@ from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import joinedload
|
||||
from sqlalchemy.orm import joinedload_all
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.sql.expression import desc
|
||||
from sqlalchemy.sql.expression import literal_column
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
@ -942,6 +944,22 @@ def virtual_interface_get_by_address(context, address):
|
||||
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
|
||||
def virtual_interface_get_by_fixed_ip(context, fixed_ip_id):
|
||||
"""Gets the virtual interface fixed_ip is associated with.
|
||||
@ -1102,12 +1120,11 @@ def instance_destroy(context, instance_id):
|
||||
def instance_stop(context, instance_id):
|
||||
session = get_session()
|
||||
with session.begin():
|
||||
from nova.compute import power_state
|
||||
session.query(models.Instance).\
|
||||
filter_by(id=instance_id).\
|
||||
update({'host': None,
|
||||
'state': power_state.SHUTOFF,
|
||||
'state_description': 'stopped',
|
||||
'vm_state': vm_states.STOPPED,
|
||||
'task_state': None,
|
||||
'updated_at': literal_column('updated_at')})
|
||||
session.query(models.SecurityGroupInstanceAssociation).\
|
||||
filter_by(instance_id=instance_id).\
|
||||
@ -1250,12 +1267,17 @@ def instance_get_all_by_filters(context, filters):
|
||||
options(joinedload_all('fixed_ips.network')).\
|
||||
options(joinedload('metadata')).\
|
||||
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
|
||||
# be modifying it and we shouldn't affect the caller's use of it.
|
||||
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 we're not admin context, add appropriate filter..
|
||||
if context.project_id:
|
||||
@ -1266,7 +1288,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',
|
||||
'state', 'instance_type_id', 'deleted']
|
||||
'vm_state', 'instance_type_id', 'deleted']
|
||||
|
||||
query_filters = [key for key in filters.iterkeys()
|
||||
if key in exact_match_filter_names]
|
||||
@ -1277,9 +1299,7 @@ def instance_get_all_by_filters(context, filters):
|
||||
query_prefix = _exact_match_filter(query_prefix, filter_name,
|
||||
filters.pop(filter_name))
|
||||
|
||||
instances = query_prefix.\
|
||||
filter_by(deleted=can_read_deleted(context)).\
|
||||
all()
|
||||
instances = query_prefix.all()
|
||||
|
||||
if not instances:
|
||||
return []
|
||||
@ -1306,21 +1326,40 @@ def instance_get_all_by_filters(context, filters):
|
||||
return instances
|
||||
|
||||
|
||||
@require_admin_context
|
||||
def instance_get_active_by_window(context, begin, end=None):
|
||||
"""Return instances that were continuously active over the given window"""
|
||||
@require_context
|
||||
def instance_get_active_by_window(context, begin, end=None, project_id=None):
|
||||
"""Return instances 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)
|
||||
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()
|
||||
|
||||
|
||||
@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()
|
||||
|
||||
|
||||
@ -1484,18 +1523,6 @@ def instance_get_floating_address(context, instance_id):
|
||||
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
|
||||
def instance_update(context, instance_id, values):
|
||||
session = get_session()
|
||||
@ -1846,6 +1873,19 @@ def network_get_by_bridge(context, bridge):
|
||||
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
|
||||
def network_get_by_cidr(context, cidr):
|
||||
session = get_session()
|
||||
|
@ -901,3 +901,12 @@ def monkey_patch():
|
||||
func = import_class("%s.%s" % (module, key))
|
||||
setattr(sys.modules[module], key,\
|
||||
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]
|
||||
|
Loading…
x
Reference in New Issue
Block a user