diff options
| author | Eric Day <eday@oddments.org> | 2010-10-21 16:15:26 -0700 |
|---|---|---|
| committer | Eric Day <eday@oddments.org> | 2010-10-21 16:15:26 -0700 |
| commit | 7af25f1476f6a30cb45b4a1a990efecc9239169e (patch) | |
| tree | 974c6d504cbd3074bb070410c7a179371a3a0503 | |
| parent | 777b14c27ba9bd5ee298f62323b2170023e44ca6 (diff) | |
PEP8 cleanup in nova/db. There should be no functional changes here, just style changes to get violations down.
| -rw-r--r-- | nova/db/api.py | 12 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/api.py | 735 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/models.py | 44 | ||||
| -rw-r--r-- | nova/db/sqlalchemy/session.py | 3 |
4 files changed, 408 insertions, 386 deletions
diff --git a/nova/db/api.py b/nova/db/api.py index 6dbf3b809..0731e2e05 100644 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -256,10 +256,12 @@ def instance_get_all(context): """Get all instances.""" return IMPL.instance_get_all(context) + def instance_get_all_by_user(context, user_id): """Get all instances.""" return IMPL.instance_get_all_by_user(context, user_id) + def instance_get_all_by_project(context, project_id): """Get all instance belonging to a project.""" return IMPL.instance_get_all_by_project(context, project_id) @@ -306,7 +308,8 @@ def instance_update(context, instance_id, values): def instance_add_security_group(context, instance_id, security_group_id): """Associate the given security group with the given instance""" - return IMPL.instance_add_security_group(context, instance_id, security_group_id) + return IMPL.instance_add_security_group(context, instance_id, + security_group_id) ################### @@ -482,10 +485,12 @@ def auth_destroy_token(context, token): """Destroy an auth token""" return IMPL.auth_destroy_token(context, token) + def auth_get_token(context, token_hash): """Retrieves a token given the hash representing it""" return IMPL.auth_get_token(context, token_hash) + def auth_create_token(context, token): """Creates a new token""" return IMPL.auth_create_token(context, token) @@ -644,7 +649,9 @@ def security_group_rule_create(context, values): def security_group_rule_get_by_security_group(context, security_group_id): """Get all rules for a a given security group""" - return IMPL.security_group_rule_get_by_security_group(context, security_group_id) + return IMPL.security_group_rule_get_by_security_group(context, + security_group_id) + def security_group_rule_destroy(context, security_group_rule_id): """Deletes a security group rule""" @@ -767,4 +774,3 @@ def host_get_networks(context, host): network host """ return IMPL.host_get_networks(context, host) - diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 209d6e51f..0cbe56499 100644 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -124,10 +124,10 @@ def service_get(context, service_id, session=None): if not session: session = get_session() - result = session.query(models.Service - ).filter_by(id=service_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Service).\ + filter_by(id=service_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('No service for id %s' % service_id) @@ -138,23 +138,23 @@ def service_get(context, service_id, session=None): @require_admin_context def service_get_all_by_topic(context, topic): session = get_session() - return session.query(models.Service - ).filter_by(deleted=False - ).filter_by(disabled=False - ).filter_by(topic=topic - ).all() + return session.query(models.Service).\ + filter_by(deleted=False).\ + filter_by(disabled=False).\ + filter_by(topic=topic).\ + all() @require_admin_context def _service_get_all_topic_subquery(context, session, topic, subq, label): sort_value = getattr(subq.c, label) - return session.query(models.Service, func.coalesce(sort_value, 0) - ).filter_by(topic=topic - ).filter_by(deleted=False - ).filter_by(disabled=False - ).outerjoin((subq, models.Service.host == subq.c.host) - ).order_by(sort_value - ).all() + return session.query(models.Service, func.coalesce(sort_value, 0)).\ + filter_by(topic=topic).\ + filter_by(deleted=False).\ + filter_by(disabled=False).\ + outerjoin((subq, models.Service.host == subq.c.host)).\ + order_by(sort_value).\ + all() @require_admin_context @@ -171,10 +171,10 @@ def service_get_all_compute_sorted(context): topic = 'compute' label = 'instance_cores' subq = session.query(models.Instance.host, - func.sum(models.Instance.vcpus).label(label) - ).filter_by(deleted=False - ).group_by(models.Instance.host - ).subquery() + func.sum(models.Instance.vcpus).label(label)).\ + filter_by(deleted=False).\ + group_by(models.Instance.host).\ + subquery() return _service_get_all_topic_subquery(context, session, topic, @@ -189,10 +189,10 @@ def service_get_all_network_sorted(context): topic = 'network' label = 'network_count' subq = session.query(models.Network.host, - func.count(models.Network.id).label(label) - ).filter_by(deleted=False - ).group_by(models.Network.host - ).subquery() + func.count(models.Network.id).label(label)).\ + filter_by(deleted=False).\ + group_by(models.Network.host).\ + subquery() return _service_get_all_topic_subquery(context, session, topic, @@ -207,10 +207,10 @@ def service_get_all_volume_sorted(context): topic = 'volume' label = 'volume_gigabytes' subq = session.query(models.Volume.host, - func.sum(models.Volume.size).label(label) - ).filter_by(deleted=False - ).group_by(models.Volume.host - ).subquery() + func.sum(models.Volume.size).label(label)).\ + filter_by(deleted=False).\ + group_by(models.Volume.host).\ + subquery() return _service_get_all_topic_subquery(context, session, topic, @@ -221,11 +221,11 @@ def service_get_all_volume_sorted(context): @require_admin_context def service_get_by_args(context, host, binary): session = get_session() - result = session.query(models.Service - ).filter_by(host=host - ).filter_by(binary=binary - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Service).\ + filter_by(host=host).\ + filter_by(binary=binary).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('No service for %s, %s' % (host, binary)) @@ -259,13 +259,13 @@ def floating_ip_allocate_address(context, host, project_id): authorize_project_context(context, project_id) session = get_session() with session.begin(): - floating_ip_ref = session.query(models.FloatingIp - ).filter_by(host=host - ).filter_by(fixed_ip_id=None - ).filter_by(project_id=None - ).filter_by(deleted=False - ).with_lockmode('update' - ).first() + floating_ip_ref = session.query(models.FloatingIp).\ + filter_by(host=host).\ + filter_by(fixed_ip_id=None).\ + filter_by(project_id=None).\ + filter_by(deleted=False).\ + with_lockmode('update').\ + first() # NOTE(vish): if with_lockmode isn't supported, as in sqlite, # then this has concurrency issues if not floating_ip_ref: @@ -288,10 +288,10 @@ def floating_ip_create(context, values): def floating_ip_count_by_project(context, project_id): authorize_project_context(context, project_id) session = get_session() - return session.query(models.FloatingIp - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).count() + return session.query(models.FloatingIp).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + count() @require_context @@ -354,31 +354,31 @@ def floating_ip_disassociate(context, address): @require_admin_context def floating_ip_get_all(context): session = get_session() - return session.query(models.FloatingIp - ).options(joinedload_all('fixed_ip.instance') - ).filter_by(deleted=False - ).all() + return session.query(models.FloatingIp).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(deleted=False).\ + all() @require_admin_context def floating_ip_get_all_by_host(context, host): session = get_session() - return session.query(models.FloatingIp - ).options(joinedload_all('fixed_ip.instance') - ).filter_by(host=host - ).filter_by(deleted=False - ).all() + return session.query(models.FloatingIp).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(host=host).\ + filter_by(deleted=False).\ + all() @require_context def floating_ip_get_all_by_project(context, project_id): authorize_project_context(context, project_id) session = get_session() - return session.query(models.FloatingIp - ).options(joinedload_all('fixed_ip.instance') - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).all() + return session.query(models.FloatingIp).\ + options(joinedload_all('fixed_ip.instance')).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + all() @require_context @@ -387,10 +387,10 @@ def floating_ip_get_by_address(context, address, session=None): if not session: session = get_session() - result = session.query(models.FloatingIp - ).filter_by(address=address - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.FloatingIp).\ + filter_by(address=address).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('No fixed ip for address %s' % address) @@ -405,12 +405,12 @@ def fixed_ip_associate(context, address, instance_id): session = get_session() with session.begin(): instance = instance_get(context, instance_id, session=session) - fixed_ip_ref = session.query(models.FixedIp - ).filter_by(address=address - ).filter_by(deleted=False - ).filter_by(instance=None - ).with_lockmode('update' - ).first() + fixed_ip_ref = session.query(models.FixedIp).\ + filter_by(address=address).\ + filter_by(deleted=False).\ + filter_by(instance=None).\ + with_lockmode('update').\ + first() # NOTE(vish): if with_lockmode isn't supported, as in sqlite, # then this has concurrency issues if not fixed_ip_ref: @@ -425,13 +425,13 @@ def fixed_ip_associate_pool(context, network_id, instance_id): 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(deleted=False - ).filter_by(instance=None - ).with_lockmode('update' - ).first() + fixed_ip_ref = session.query(models.FixedIp).\ + filter(network_or_none).\ + filter_by(reserved=False).\ + filter_by(deleted=False).\ + filter_by(instance=None).\ + with_lockmode('update').\ + first() # NOTE(vish): if with_lockmode isn't supported, as in sqlite, # then this has concurrency issues if not fixed_ip_ref: @@ -455,6 +455,7 @@ def fixed_ip_create(_context, values): fixed_ip_ref.save() return fixed_ip_ref['address'] + @require_context def fixed_ip_disassociate(context, address): session = get_session() @@ -465,6 +466,7 @@ def fixed_ip_disassociate(context, address): fixed_ip_ref.instance = None fixed_ip_ref.save(session=session) + @require_admin_context def fixed_ip_disassociate_all_by_timeout(_context, host, time): session = get_session() @@ -486,12 +488,12 @@ def fixed_ip_disassociate_all_by_timeout(_context, host, time): def fixed_ip_get_by_address(context, address, session=None): if not session: session = get_session() - result = session.query(models.FixedIp - ).filter_by(address=address - ).filter_by(deleted=can_read_deleted(context) - ).options(joinedload('network') - ).options(joinedload('instance') - ).first() + result = session.query(models.FixedIp).\ + filter_by(address=address).\ + filter_by(deleted=can_read_deleted(context)).\ + options(joinedload('network')).\ + options(joinedload('instance')).\ + first() if not result: raise exception.NotFound('No floating ip for address %s' % address) @@ -552,10 +554,10 @@ def instance_create(context, values): def instance_data_get_for_project(context, project_id): session = get_session() result = session.query(func.count(models.Instance.id), - func.sum(models.Instance.vcpus) - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).first() + func.sum(models.Instance.vcpus)).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + first() # NOTE(vish): convert None to 0 return (result[0] or 0, result[1] or 0) @@ -575,18 +577,18 @@ def instance_get(context, instance_id, session=None): result = None if is_admin_context(context): - result = session.query(models.Instance - ).options(joinedload('security_groups') - ).filter_by(id=instance_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Instance).\ + options(joinedload('security_groups')).\ + filter_by(id=instance_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() elif is_user_context(context): - result = session.query(models.Instance - ).options(joinedload('security_groups') - ).filter_by(project_id=context.project_id - ).filter_by(id=instance_id - ).filter_by(deleted=False - ).first() + result = session.query(models.Instance).\ + options(joinedload('security_groups')).\ + filter_by(project_id=context.project_id).\ + filter_by(id=instance_id).\ + filter_by(deleted=False).\ + first() if not result: raise exception.NotFound('No instance for id %s' % instance_id) @@ -596,22 +598,22 @@ def instance_get(context, instance_id, session=None): @require_admin_context def instance_get_all(context): session = get_session() - return session.query(models.Instance - ).options(joinedload_all('fixed_ip.floating_ips') - ).options(joinedload('security_groups') - ).filter_by(deleted=can_read_deleted(context) - ).all() + return session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload('security_groups')).\ + filter_by(deleted=can_read_deleted(context)).\ + all() @require_admin_context def instance_get_all_by_user(context, user_id): session = get_session() - return session.query(models.Instance - ).options(joinedload_all('fixed_ip.floating_ips') - ).options(joinedload('security_groups') - ).filter_by(deleted=can_read_deleted(context) - ).filter_by(user_id=user_id - ).all() + return session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload('security_groups')).\ + filter_by(deleted=can_read_deleted(context)).\ + filter_by(user_id=user_id).\ + all() @require_context @@ -619,12 +621,12 @@ def instance_get_all_by_project(context, project_id): authorize_project_context(context, project_id) session = get_session() - return session.query(models.Instance - ).options(joinedload_all('fixed_ip.floating_ips') - ).options(joinedload('security_groups') - ).filter_by(project_id=project_id - ).filter_by(deleted=can_read_deleted(context) - ).all() + return session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload('security_groups')).\ + filter_by(project_id=project_id).\ + filter_by(deleted=can_read_deleted(context)).\ + all() @require_context @@ -632,20 +634,20 @@ def instance_get_all_by_reservation(context, reservation_id): session = get_session() if is_admin_context(context): - return session.query(models.Instance - ).options(joinedload_all('fixed_ip.floating_ips') - ).options(joinedload('security_groups') - ).filter_by(reservation_id=reservation_id - ).filter_by(deleted=can_read_deleted(context) - ).all() + return session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload('security_groups')).\ + filter_by(reservation_id=reservation_id).\ + filter_by(deleted=can_read_deleted(context)).\ + all() elif is_user_context(context): - return session.query(models.Instance - ).options(joinedload_all('fixed_ip.floating_ips') - ).options(joinedload('security_groups') - ).filter_by(project_id=context.project_id - ).filter_by(reservation_id=reservation_id - ).filter_by(deleted=False - ).all() + return session.query(models.Instance).\ + options(joinedload_all('fixed_ip.floating_ips')).\ + options(joinedload('security_groups')).\ + filter_by(project_id=context.project_id).\ + filter_by(reservation_id=reservation_id).\ + filter_by(deleted=False).\ + all() @require_context @@ -653,18 +655,18 @@ def instance_get_by_internal_id(context, internal_id): session = get_session() if is_admin_context(context): - result = session.query(models.Instance - ).options(joinedload('security_groups') - ).filter_by(internal_id=internal_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Instance).\ + options(joinedload('security_groups')).\ + filter_by(internal_id=internal_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() elif is_user_context(context): - result = session.query(models.Instance - ).options(joinedload('security_groups') - ).filter_by(project_id=context.project_id - ).filter_by(internal_id=internal_id - ).filter_by(deleted=False - ).first() + result = session.query(models.Instance).\ + options(joinedload('security_groups')).\ + filter_by(project_id=context.project_id).\ + filter_by(internal_id=internal_id).\ + filter_by(deleted=False).\ + first() if not result: raise exception.NotFound('Instance %s not found' % (internal_id)) @@ -675,9 +677,9 @@ def instance_get_by_internal_id(context, internal_id): def instance_internal_id_exists(context, internal_id, session=None): if not session: session = get_session() - return session.query( - exists().where(models.Instance.internal_id==internal_id) - ).one()[0] + return session.query(exists().\ + where(models.Instance.internal_id == internal_id)).\ + one()[0] @require_context @@ -782,11 +784,11 @@ def key_pair_get(context, user_id, name, session=None): if not session: session = get_session() - result = session.query(models.KeyPair - ).filter_by(user_id=user_id - ).filter_by(name=name - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.KeyPair).\ + filter_by(user_id=user_id).\ + filter_by(name=name).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('no keypair for user %s, name %s' % (user_id, name)) @@ -797,10 +799,10 @@ def key_pair_get(context, user_id, name, session=None): def key_pair_get_all_by_user(context, user_id): authorize_user_context(context, user_id) session = get_session() - return session.query(models.KeyPair - ).filter_by(user_id=user_id - ).filter_by(deleted=False - ).all() + return session.query(models.KeyPair).\ + filter_by(user_id=user_id).\ + filter_by(deleted=False).\ + all() ################### @@ -810,11 +812,11 @@ def key_pair_get_all_by_user(context, user_id): def network_associate(context, project_id): session = get_session() with session.begin(): - network_ref = session.query(models.Network - ).filter_by(deleted=False - ).filter_by(project_id=None - ).with_lockmode('update' - ).first() + network_ref = session.query(models.Network).\ + filter_by(deleted=False).\ + filter_by(project_id=None).\ + with_lockmode('update').\ + first() # NOTE(vish): if with_lockmode isn't supported, as in sqlite, # then this has concurrency issues if not network_ref: @@ -827,40 +829,40 @@ def network_associate(context, project_id): @require_admin_context def network_count(context): session = get_session() - return session.query(models.Network - ).filter_by(deleted=can_read_deleted(context) - ).count() + return session.query(models.Network).\ + filter_by(deleted=can_read_deleted(context)).\ + count() @require_admin_context def network_count_allocated_ips(context, network_id): session = get_session() - return session.query(models.FixedIp - ).filter_by(network_id=network_id - ).filter_by(allocated=True - ).filter_by(deleted=False - ).count() + return session.query(models.FixedIp).\ + filter_by(network_id=network_id).\ + filter_by(allocated=True).\ + filter_by(deleted=False).\ + count() @require_admin_context def network_count_available_ips(context, network_id): session = get_session() - return session.query(models.FixedIp - ).filter_by(network_id=network_id - ).filter_by(allocated=False - ).filter_by(reserved=False - ).filter_by(deleted=False - ).count() + return session.query(models.FixedIp).\ + filter_by(network_id=network_id).\ + filter_by(allocated=False).\ + filter_by(reserved=False).\ + filter_by(deleted=False).\ + count() @require_admin_context def network_count_reserved_ips(context, network_id): session = get_session() - return session.query(models.FixedIp - ).filter_by(network_id=network_id - ).filter_by(reserved=True - ).filter_by(deleted=False - ).count() + return session.query(models.FixedIp).\ + filter_by(network_id=network_id).\ + filter_by(reserved=True).\ + filter_by(deleted=False).\ + count() @require_admin_context @@ -893,16 +895,16 @@ def network_get(context, network_id, session=None): result = None if is_admin_context(context): - result = session.query(models.Network - ).filter_by(id=network_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Network).\ + filter_by(id=network_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() elif is_user_context(context): - result = session.query(models.Network - ).filter_by(project_id=context.project_id - ).filter_by(id=network_id - ).filter_by(deleted=False - ).first() + result = session.query(models.Network).\ + filter_by(project_id=context.project_id).\ + filter_by(id=network_id).\ + filter_by(deleted=False).\ + first() if not result: raise exception.NotFound('No network for id %s' % network_id) @@ -915,21 +917,21 @@ def network_get(context, network_id, session=None): @require_admin_context def network_get_associated_fixed_ips(context, network_id): session = get_session() - return session.query(models.FixedIp - ).options(joinedload_all('instance') - ).filter_by(network_id=network_id - ).filter(models.FixedIp.instance_id != None - ).filter_by(deleted=False - ).all() + return session.query(models.FixedIp).\ + options(joinedload_all('instance')).\ + filter_by(network_id=network_id).\ + filter(models.FixedIp.instance_id != None).\ + filter_by(deleted=False).\ + all() @require_admin_context def network_get_by_bridge(context, bridge): session = get_session() - result = session.query(models.Network - ).filter_by(bridge=bridge - ).filter_by(deleted=False - ).first() + result = session.query(models.Network).\ + filter_by(bridge=bridge).\ + filter_by(deleted=False).\ + first() if not result: raise exception.NotFound('No network for bridge %s' % bridge) @@ -939,12 +941,12 @@ def network_get_by_bridge(context, bridge): @require_admin_context def network_get_by_instance(_context, instance_id): session = get_session() - rv = session.query(models.Network - ).filter_by(deleted=False - ).join(models.Network.fixed_ips - ).filter_by(instance_id=instance_id - ).filter_by(deleted=False - ).first() + rv = session.query(models.Network).\ + filter_by(deleted=False).\ + join(models.Network.fixed_ips).\ + filter_by(instance_id=instance_id).\ + filter_by(deleted=False).\ + first() if not rv: raise exception.NotFound('No network for instance %s' % instance_id) return rv @@ -954,11 +956,11 @@ def network_get_by_instance(_context, instance_id): def network_set_host(context, network_id, host_id): session = get_session() with session.begin(): - network_ref = session.query(models.Network - ).filter_by(id=network_id - ).filter_by(deleted=False - ).with_lockmode('update' - ).first() + network_ref = session.query(models.Network).\ + filter_by(id=network_id).\ + filter_by(deleted=False).\ + with_lockmode('update').\ + first() if not network_ref: raise exception.NotFound('No network for id %s' % network_id) @@ -987,10 +989,10 @@ def network_update(context, network_id, values): @require_context def project_get_network(context, project_id): session = get_session() - rv = session.query(models.Network - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).first() + rv = session.query(models.Network).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + first() if not rv: try: return network_associate(context, project_id) @@ -998,10 +1000,10 @@ def project_get_network(context, project_id): # NOTE(vish): We hit this if there is a race and two # processes are attempting to allocate the # network at the same time - rv = session.query(models.Network - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).first() + rv = session.query(models.Network).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + first() return rv @@ -1019,9 +1021,9 @@ def queue_get_for(_context, topic, physical_node_id): @require_admin_context def export_device_count(context): session = get_session() - return session.query(models.ExportDevice - ).filter_by(deleted=can_read_deleted(context) - ).count() + return session.query(models.ExportDevice).\ + filter_by(deleted=can_read_deleted(context)).\ + count() @require_admin_context @@ -1043,18 +1045,20 @@ def auth_destroy_token(_context, token): session = get_session() session.delete(token) + def auth_get_token(_context, token_hash): session = get_session() - tk = session.query(models.AuthToken - ).filter_by(token_hash=token_hash - ).first() + tk = session.query(models.AuthToken).\ + filter_by(token_hash=token_hash).\ + first() if not tk: raise exception.NotFound('Token %s does not exist' % token_hash) return tk + def auth_create_token(_context, token): tk = models.AuthToken() - for k,v in token.iteritems(): + for k, v in token.iteritems(): tk[k] = v tk.save() return tk @@ -1068,10 +1072,10 @@ def quota_get(context, project_id, session=None): if not session: session = get_session() - result = session.query(models.Quota - ).filter_by(project_id=project_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Quota).\ + filter_by(project_id=project_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('No quota for project_id %s' % project_id) @@ -1112,11 +1116,11 @@ def quota_destroy(context, project_id): def volume_allocate_shelf_and_blade(context, volume_id): session = get_session() with session.begin(): - export_device = session.query(models.ExportDevice - ).filter_by(volume=None - ).filter_by(deleted=False - ).with_lockmode('update' - ).first() + export_device = session.query(models.ExportDevice).\ + filter_by(volume=None).\ + filter_by(deleted=False).\ + with_lockmode('update').\ + first() # NOTE(vish): if with_lockmode isn't supported, as in sqlite, # then this has concurrency issues if not export_device: @@ -1134,7 +1138,8 @@ def volume_attached(context, volume_id, instance_id, mountpoint): volume_ref['status'] = 'in-use' volume_ref['mountpoint'] = mountpoint volume_ref['attach_status'] = 'attached' - volume_ref.instance = instance_get(context, instance_id, session=session) + volume_ref.instance = instance_get(context, instance_id, + session=session) volume_ref.save(session=session) @@ -1158,10 +1163,10 @@ def volume_create(context, values): def volume_data_get_for_project(context, project_id): session = get_session() result = session.query(func.count(models.Volume.id), - func.sum(models.Volume.size) - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).first() + func.sum(models.Volume.size)).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + first() # NOTE(vish): convert None to 0 return (result[0] or 0, result[1] or 0) @@ -1197,16 +1202,16 @@ def volume_get(context, volume_id, session=None): result = None if is_admin_context(context): - result = session.query(models.Volume - ).filter_by(id=volume_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Volume).\ + filter_by(id=volume_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() elif is_user_context(context): - result = session.query(models.Volume - ).filter_by(project_id=context.project_id - ).filter_by(id=volume_id - ).filter_by(deleted=False - ).first() + result = session.query(models.Volume).\ + filter_by(project_id=context.project_id).\ + filter_by(id=volume_id).\ + filter_by(deleted=False).\ + first() if not result: raise exception.NotFound('No volume for id %s' % volume_id) @@ -1216,19 +1221,20 @@ def volume_get(context, volume_id, session=None): @require_admin_context def volume_get_all(context): session = get_session() - return session.query(models.Volume - ).filter_by(deleted=can_read_deleted(context) - ).all() + return session.query(models.Volume).\ + filter_by(deleted=can_read_deleted(context)).\ + all() + @require_context def volume_get_all_by_project(context, project_id): authorize_project_context(context, project_id) session = get_session() - return session.query(models.Volume - ).filter_by(project_id=project_id - ).filter_by(deleted=can_read_deleted(context) - ).all() + return session.query(models.Volume).\ + filter_by(project_id=project_id).\ + filter_by(deleted=can_read_deleted(context)).\ + all() @require_context @@ -1237,16 +1243,16 @@ def volume_get_by_ec2_id(context, ec2_id): result = None if is_admin_context(context): - result = session.query(models.Volume - ).filter_by(ec2_id=ec2_id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.Volume).\ + filter_by(ec2_id=ec2_id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() elif is_user_context(context): - result = session.query(models.Volume - ).filter_by(project_id=context.project_id - ).filter_by(ec2_id=ec2_id - ).filter_by(deleted=False - ).first() + result = session.query(models.Volume).\ + filter_by(project_id=context.project_id).\ + filter_by(ec2_id=ec2_id).\ + filter_by(deleted=False).\ + first() else: raise exception.NotAuthorized() @@ -1261,19 +1267,19 @@ def volume_ec2_id_exists(context, ec2_id, session=None): if not session: session = get_session() - return session.query(exists( - ).where(models.Volume.id==ec2_id) - ).one()[0] + return session.query(exists().\ + where(models.Volume.id == ec2_id)).\ + one()[0] @require_admin_context def volume_get_instance(context, volume_id): session = get_session() - result = session.query(models.Volume - ).filter_by(id=volume_id - ).filter_by(deleted=can_read_deleted(context) - ).options(joinedload('instance') - ).first() + result = session.query(models.Volume).\ + filter_by(id=volume_id).\ + filter_by(deleted=can_read_deleted(context)).\ + options(joinedload('instance')).\ + first() if not result: raise exception.NotFound('Volume %s not found' % ec2_id) @@ -1283,9 +1289,9 @@ def volume_get_instance(context, volume_id): @require_admin_context def volume_get_shelf_and_blade(context, volume_id): session = get_session() - result = session.query(models.ExportDevice - ).filter_by(volume_id=volume_id - ).first() + result = session.query(models.ExportDevice).\ + filter_by(volume_id=volume_id).\ + first() if not result: raise exception.NotFound('No export device found for volume %s' % volume_id) @@ -1309,10 +1315,10 @@ def volume_update(context, volume_id, values): @require_context def security_group_get_all(context): session = get_session() - return session.query(models.SecurityGroup - ).filter_by(deleted=can_read_deleted(context) - ).options(joinedload_all('rules') - ).all() + return session.query(models.SecurityGroup).\ + filter_by(deleted=can_read_deleted(context)).\ + options(joinedload_all('rules')).\ + all() @require_context @@ -1320,18 +1326,18 @@ def security_group_get(context, security_group_id, session=None): if not session: session = get_session() if is_admin_context(context): - result = session.query(models.SecurityGroup - ).filter_by(deleted=can_read_deleted(context), - ).filter_by(id=security_group_id - ).options(joinedload_all('rules') - ).first() + result = session.query(models.SecurityGroup).\ + filter_by(deleted=can_read_deleted(context),).\ + filter_by(id=security_group_id).\ + options(joinedload_all('rules')).\ + first() else: - result = session.query(models.SecurityGroup - ).filter_by(deleted=False - ).filter_by(id=security_group_id - ).filter_by(project_id=context.project_id - ).options(joinedload_all('rules') - ).first() + result = session.query(models.SecurityGroup).\ + filter_by(deleted=False).\ + filter_by(id=security_group_id).\ + filter_by(project_id=context.project_id).\ + options(joinedload_all('rules')).\ + first() if not result: raise exception.NotFound("No secuity group with id %s" % security_group_id) @@ -1341,13 +1347,13 @@ def security_group_get(context, security_group_id, session=None): @require_context def security_group_get_by_name(context, project_id, group_name): session = get_session() - result = session.query(models.SecurityGroup - ).filter_by(project_id=project_id - ).filter_by(name=group_name - ).filter_by(deleted=False - ).options(joinedload_all('rules') - ).options(joinedload_all('instances') - ).first() + result = session.query(models.SecurityGroup).\ + filter_by(project_id=project_id).\ + filter_by(name=group_name).\ + filter_by(deleted=False).\ + options(joinedload_all('rules')).\ + options(joinedload_all('instances')).\ + first() if not result: raise exception.NotFound( 'No security group named %s for project: %s' \ @@ -1358,23 +1364,23 @@ def security_group_get_by_name(context, project_id, group_name): @require_context def security_group_get_by_project(context, project_id): session = get_session() - return session.query(models.SecurityGroup - ).filter_by(project_id=project_id - ).filter_by(deleted=False - ).options(joinedload_all('rules') - ).all() + return session.query(models.SecurityGroup).\ + filter_by(project_id=project_id).\ + filter_by(deleted=False).\ + options(joinedload_all('rules')).\ + all() @require_context def security_group_get_by_instance(context, instance_id): session = get_session() - return session.query(models.SecurityGroup - ).filter_by(deleted=False - ).options(joinedload_all('rules') - ).join(models.SecurityGroup.instances - ).filter_by(id=instance_id - ).filter_by(deleted=False - ).all() + return session.query(models.SecurityGroup).\ + filter_by(deleted=False).\ + options(joinedload_all('rules')).\ + join(models.SecurityGroup.instances).\ + filter_by(id=instance_id).\ + filter_by(deleted=False).\ + all() @require_context @@ -1409,6 +1415,7 @@ def security_group_destroy(context, security_group_id): 'where group_id=:id', {'id': security_group_id}) + @require_context def security_group_destroy_all(context, session=None): if not session: @@ -1427,16 +1434,16 @@ def security_group_rule_get(context, security_group_rule_id, session=None): if not session: session = get_session() if is_admin_context(context): - result = session.query(models.SecurityGroupIngressRule - ).filter_by(deleted=can_read_deleted(context) - ).filter_by(id=security_group_rule_id - ).first() + result = session.query(models.SecurityGroupIngressRule).\ + filter_by(deleted=can_read_deleted(context)).\ + filter_by(id=security_group_rule_id).\ + first() else: # TODO(vish): Join to group and check for project_id - result = session.query(models.SecurityGroupIngressRule - ).filter_by(deleted=False - ).filter_by(id=security_group_rule_id - ).first() + result = session.query(models.SecurityGroupIngressRule).\ + filter_by(deleted=False).\ + filter_by(id=security_group_rule_id).\ + first() if not result: raise exception.NotFound("No secuity group rule with id %s" % security_group_rule_id) @@ -1451,6 +1458,7 @@ def security_group_rule_create(context, values): security_group_rule_ref.save() return security_group_rule_ref + @require_context def security_group_rule_destroy(context, security_group_rule_id): session = get_session() @@ -1468,10 +1476,10 @@ def user_get(context, id, session=None): if not session: session = get_session() - result = session.query(models.User - ).filter_by(id=id - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.User).\ + filter_by(id=id).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('No user for id %s' % id) @@ -1484,10 +1492,10 @@ def user_get_by_access_key(context, access_key, session=None): if not session: session = get_session() - result = session.query(models.User - ).filter_by(access_key=access_key - ).filter_by(deleted=can_read_deleted(context) - ).first() + result = session.query(models.User).\ + filter_by(access_key=access_key).\ + filter_by(deleted=can_read_deleted(context)).\ + first() if not result: raise exception.NotFound('No user for access key %s' % access_key) @@ -1508,21 +1516,21 @@ def user_create(_context, values): def user_delete(context, id): session = get_session() with session.begin(): - session.execute('delete from user_project_association where user_id=:id', - {'id': id}) - session.execute('delete from user_role_association where user_id=:id', - {'id': id}) - session.execute('delete from user_project_role_association where user_id=:id', - {'id': id}) + session.execute('delete from user_project_association ' + 'where user_id=:id', {'id': id}) + session.execute('delete from user_role_association ' + 'where user_id=:id', {'id': id}) + session.execute('delete from user_project_role_association ' + 'where user_id=:id', {'id': id}) user_ref = user_get(context, id, session=session) session.delete(user_ref) def user_get_all(context): session = get_session() - return session.query(models.User - ).filter_by(deleted=can_read_deleted(context) - ).all() + return session.query(models.User).\ + filter_by(deleted=can_read_deleted(context)).\ + all() def project_create(_context, values): @@ -1547,11 +1555,11 @@ def project_get(context, id, session=None): if not session: session = get_session() - result = session.query(models.Project - ).filter_by(deleted=False - ).filter_by(id=id - ).options(joinedload_all('members') - ).first() + result = session.query(models.Project).\ + filter_by(deleted=False).\ + filter_by(id=id).\ + options(joinedload_all('members')).\ + first() if not result: raise exception.NotFound("No project with id %s" % id) @@ -1561,18 +1569,18 @@ def project_get(context, id, session=None): def project_get_all(context): session = get_session() - return session.query(models.Project - ).filter_by(deleted=can_read_deleted(context) - ).options(joinedload_all('members') - ).all() + return session.query(models.Project).\ + filter_by(deleted=can_read_deleted(context)).\ + options(joinedload_all('members')).\ + all() def project_get_by_user(context, user_id): session = get_session() - user = session.query(models.User - ).filter_by(deleted=can_read_deleted(context) - ).options(joinedload_all('projects') - ).first() + user = session.query(models.User).\ + filter_by(deleted=can_read_deleted(context)).\ + options(joinedload_all('projects')).\ + first() return user.projects @@ -1607,10 +1615,10 @@ def project_update(context, project_id, values): def project_delete(context, id): session = get_session() with session.begin(): - session.execute('delete from user_project_association where project_id=:id', - {'id': id}) - session.execute('delete from user_project_role_association where project_id=:id', - {'id': id}) + session.execute('delete from user_project_association ' + 'where project_id=:id', {'id': id}) + session.execute('delete from user_project_role_association ' + 'where project_id=:id', {'id': id}) project_ref = project_get(context, id, session=session) session.delete(project_ref) @@ -1625,29 +1633,30 @@ def user_get_roles(context, user_id): def user_get_roles_for_project(context, user_id, project_id): session = get_session() with session.begin(): - res = session.query(models.UserProjectRoleAssociation - ).filter_by(user_id=user_id - ).filter_by(project_id=project_id - ).all() + res = session.query(models.UserProjectRoleAssociation).\ + filter_by(user_id=user_id).\ + filter_by(project_id=project_id).\ + all() return [association.role for association in res] + def user_remove_project_role(context, user_id, project_id, role): session = get_session() with session.begin(): - session.execute('delete from user_project_role_association where ' + \ - 'user_id=:user_id and project_id=:project_id and ' + \ - 'role=:role', { 'user_id' : user_id, - 'project_id' : project_id, - 'role' : role }) + session.execute('delete from user_project_role_association where ' + 'user_id=:user_id and project_id=:project_id and ' + 'role=:role', {'user_id': user_id, + 'project_id': project_id, + 'role': role}) def user_remove_role(context, user_id, role): session = get_session() with session.begin(): - res = session.query(models.UserRoleAssociation - ).filter_by(user_id=user_id - ).filter_by(role=role - ).all() + res = session.query(models.UserRoleAssociation).\ + filter_by(user_id=user_id).\ + filter_by(role=role).\ + all() for role in res: session.delete(role) @@ -1656,7 +1665,8 @@ def user_add_role(context, user_id, role): session = get_session() with session.begin(): user_ref = user_get(context, user_id, session=session) - models.UserRoleAssociation(user=user_ref, role=role).save(session=session) + models.UserRoleAssociation(user=user_ref, role=role).\ + save(session=session) def user_add_project_role(context, user_id, project_id, role): @@ -1672,12 +1682,11 @@ def user_add_project_role(context, user_id, project_id, role): ################### - @require_admin_context def host_get_networks(context, host): session = get_session() with session.begin(): - return session.query(models.Network - ).filter_by(deleted=False - ).filter_by(host=host - ).all() + return session.query(models.Network).\ + filter_by(deleted=False).\ + filter_by(host=host).\ + all() diff --git a/nova/db/sqlalchemy/models.py b/nova/db/sqlalchemy/models.py index a63bca2b0..38c96bdec 100644 --- a/nova/db/sqlalchemy/models.py +++ b/nova/db/sqlalchemy/models.py @@ -134,8 +134,8 @@ class NovaBase(object): # """Represents a host where services are running""" # __tablename__ = 'hosts' # id = Column(String(255), primary_key=True) -# -# + + class Service(BASE, NovaBase): """Represents a running service on a host""" __tablename__ = 'services' @@ -277,7 +277,8 @@ class Quota(BASE, NovaBase): class ExportDevice(BASE, NovaBase): """Represates a shelf and blade that a volume can be exported on""" __tablename__ = 'export_devices' - __table_args__ = (schema.UniqueConstraint("shelf_id", "blade_id"), {'mysql_engine': 'InnoDB'}) + __table_args__ = (schema.UniqueConstraint("shelf_id", "blade_id"), + {'mysql_engine': 'InnoDB'}) id = Column(Integer, primary_key=True) shelf_id = Column(Integer) blade_id = Column(Integer) @@ -308,10 +309,13 @@ class SecurityGroup(BASE, NovaBase): instances = relationship(Instance, secondary="security_group_instance_association", - primaryjoin="and_(SecurityGroup.id == SecurityGroupInstanceAssociation.security_group_id," - "SecurityGroup.deleted == False)", - secondaryjoin="and_(SecurityGroupInstanceAssociation.instance_id == Instance.id," - "Instance.deleted == False)", + primaryjoin='and_(' + 'SecurityGroup.id == ' + 'SecurityGroupInstanceAssociation.security_group_id,' + 'SecurityGroup.deleted == False)', + secondaryjoin='and_(' + 'SecurityGroupInstanceAssociation.instance_id == Instance.id,' + 'Instance.deleted == False)', backref='security_groups') @property @@ -330,11 +334,12 @@ class SecurityGroupIngressRule(BASE, NovaBase): parent_group_id = Column(Integer, ForeignKey('security_groups.id')) parent_group = relationship("SecurityGroup", backref="rules", - foreign_keys=parent_group_id, - primaryjoin="and_(SecurityGroupIngressRule.parent_group_id == SecurityGroup.id," - "SecurityGroupIngressRule.deleted == False)") + foreign_keys=parent_group_id, + primaryjoin='and_(' + 'SecurityGroupIngressRule.parent_group_id == SecurityGroup.id,' + 'SecurityGroupIngressRule.deleted == False)') - protocol = Column(String(5)) # "tcp", "udp", or "icmp" + protocol = Column(String(5)) # "tcp", "udp", or "icmp" from_port = Column(Integer) to_port = Column(Integer) cidr = Column(String(255)) @@ -414,8 +419,9 @@ class FixedIp(BASE, NovaBase): instance = relationship(Instance, backref=backref('fixed_ip', uselist=False), foreign_keys=instance_id, - primaryjoin='and_(FixedIp.instance_id==Instance.id,' - 'FixedIp.deleted==False)') + primaryjoin='and_(' + 'FixedIp.instance_id == Instance.id,' + 'FixedIp.deleted == False)') allocated = Column(Boolean, default=False) leased = Column(Boolean, default=False) reserved = Column(Boolean, default=False) @@ -455,13 +461,13 @@ class UserProjectRoleAssociation(BASE, NovaBase): __tablename__ = 'user_project_role_association' user_id = Column(String(255), primary_key=True) user = relationship(User, - primaryjoin=user_id==User.id, + primaryjoin=user_id == User.id, foreign_keys=[User.id], uselist=False) project_id = Column(String(255), primary_key=True) project = relationship(Project, - primaryjoin=project_id==Project.id, + primaryjoin=project_id == Project.id, foreign_keys=[Project.id], uselist=False) @@ -485,7 +491,6 @@ class UserProjectAssociation(BASE, NovaBase): project_id = Column(String(255), ForeignKey(Project.id), primary_key=True) - class FloatingIp(BASE, NovaBase): """Represents a floating ip that dynamically forwards to a fixed ip""" __tablename__ = 'floating_ips' @@ -495,8 +500,9 @@ class FloatingIp(BASE, NovaBase): fixed_ip = relationship(FixedIp, backref=backref('floating_ips'), foreign_keys=fixed_ip_id, - primaryjoin='and_(FloatingIp.fixed_ip_id==FixedIp.id,' - 'FloatingIp.deleted==False)') + primaryjoin='and_(' + 'FloatingIp.fixed_ip_id == FixedIp.id,' + 'FloatingIp.deleted == False)') project_id = Column(String(255)) host = Column(String(255)) # , ForeignKey('hosts.id')) @@ -507,7 +513,7 @@ def register_models(): models = (Service, Instance, Volume, ExportDevice, FixedIp, FloatingIp, Network, SecurityGroup, SecurityGroupIngressRule, SecurityGroupInstanceAssociation, - AuthToken, User, Project) # , Image, Host + AuthToken, User, Project) # , Image, Host engine = create_engine(FLAGS.sql_connection, echo=False) for model in models: model.metadata.create_all(engine) diff --git a/nova/db/sqlalchemy/session.py b/nova/db/sqlalchemy/session.py index 826754f6a..e0d84c107 100644 --- a/nova/db/sqlalchemy/session.py +++ b/nova/db/sqlalchemy/session.py @@ -29,6 +29,7 @@ FLAGS = flags.FLAGS _ENGINE = None _MAKER = None + def get_session(autocommit=True, expire_on_commit=False): """Helper method to grab session""" global _ENGINE @@ -39,5 +40,5 @@ def get_session(autocommit=True, expire_on_commit=False): _MAKER = (sessionmaker(bind=_ENGINE, autocommit=autocommit, expire_on_commit=expire_on_commit)) - session = _MAKER() + session = _MAKER() return session |
