예제 #1
0
파일: api.py 프로젝트: zainubwahid/neutron
def set_quota_usage(context, resource, tenant_id, in_use=None, delta=False):
    """Set resource quota usage.

    :param context: instance of neutron context with db session
    :param resource: name of the resource for which usage is being set
    :param tenant_id: identifier of the tenant for which quota usage is
                      being set
    :param in_use: integer specifying the new quantity of used resources,
                   or a delta to apply to current used resource
    :param delta: Specifies whether in_use is an absolute number
                  or a delta (default to False)
    """
    with db_api.autonested_transaction(context.session):
        query = common_db_api.model_query(context, quota_models.QuotaUsage)
        query = query.filter_by(resource=resource).filter_by(
            tenant_id=tenant_id)
        usage_data = query.first()
        if not usage_data:
            # Must create entry
            usage_data = quota_models.QuotaUsage(resource=resource,
                                                 tenant_id=tenant_id)
            context.session.add(usage_data)
        # Perform explicit comparison with None as 0 is a valid value
        if in_use is not None:
            if delta:
                in_use = usage_data.in_use + in_use
            usage_data.in_use = in_use
        # After an explicit update the dirty bit should always be reset
        usage_data.dirty = False
    return QuotaUsageInfo(usage_data.resource, usage_data.tenant_id,
                          usage_data.in_use, usage_data.dirty)
예제 #2
0
파일: api.py 프로젝트: smokony/neutron
def set_quota_usage(context, resource, tenant_id,
                    in_use=None, delta=False):
    """Set resource quota usage.

    :param context: instance of neutron context with db session
    :param resource: name of the resource for which usage is being set
    :param tenant_id: identifier of the tenant for which quota usage is
                      being set
    :param in_use: integer specifying the new quantity of used resources,
                   or a delta to apply to current used resource
    :param delta: Specifies whether in_use is an absolute number
                  or a delta (default to False)
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource).filter_by(tenant_id=tenant_id)
    usage_data = query.first()
    with context.session.begin(subtransactions=True):
        if not usage_data:
            # Must create entry
            usage_data = quota_models.QuotaUsage(
                resource=resource,
                tenant_id=tenant_id)
            context.session.add(usage_data)
        # Perform explicit comparison with None as 0 is a valid value
        if in_use is not None:
            if delta:
                in_use = usage_data.in_use + in_use
            usage_data.in_use = in_use
        # After an explicit update the dirty bit should always be reset
        usage_data.dirty = False
    return QuotaUsageInfo(usage_data.resource,
                          usage_data.tenant_id,
                          usage_data.in_use,
                          usage_data.dirty)
예제 #3
0
파일: api.py 프로젝트: jiangkila/neutron
def get_quota_usage_by_resource_and_tenant(context, resource, tenant_id,
                                           lock_for_update=False):
    """Return usage info for a given resource and tenant.

    :param context: Request context
    :param resource: Name of the resource
    :param tenant_id: Tenant identifier
    :param lock_for_update: if True sets a write-intent lock on the query
    :returns: a QuotaUsageInfo instance
    """

    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource, tenant_id=tenant_id)

    if lock_for_update:
        query = query.with_lockmode('update')

    result = query.first()
    if not result:
        return
    return QuotaUsageInfo(result.resource,
                          result.tenant_id,
                          result.in_use,
                          result.reserved,
                          result.dirty)
예제 #4
0
def patched_set_resources_dirty(context):
    if not cfg.CONF.QUOTAS.track_quota_usage:
        return

    with context.session.begin(subtransactions=True):
        for res in res_reg.get_all_resources().values():
            if res_reg.is_tracked(res.name) and res.dirty:
                dirty_tenants_snap = res._dirty_tenants.copy()
                for tenant_id in dirty_tenants_snap:
                    query = common_db_api.model_query(context,
                                                      quota_models.QuotaUsage)
                    query = query.filter_by(resource=res.name).filter_by(
                        tenant_id=tenant_id)
                    usage_data = query.first()
                    # Set dirty if not set already. This effectively
                    # patches the inner notify method:
                    # https://github.com/openstack/neutron/blob/newton-eol/
                    # neutron/api/v2/base.py#L481
                    # to avoid updating the QuotaUsages table outside
                    # from that method (which starts a new transaction).
                    # The dirty marking would have been already done
                    # in the ml2plus manager at the end of the pre_commit
                    # stage (and prior to the plugin initiated transaction
                    # completing).
                    if usage_data and not usage_data.dirty:
                        res.mark_dirty(context)
예제 #5
0
파일: api.py 프로젝트: fuborui/neutron
def get_quota_usage_by_resource(context, resource):
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource)
    return [
        QuotaUsageInfo(item.resource, item.tenant_id, item.in_use,
                       item.reserved, item.dirty) for item in query
    ]
예제 #6
0
파일: api.py 프로젝트: smokony/neutron
def get_quota_usage_by_tenant_id(context, tenant_id):
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(tenant_id=tenant_id)
    return [QuotaUsageInfo(item.resource,
                           item.tenant_id,
                           item.in_use,
                           item.dirty) for item in query]
예제 #7
0
def patched_set_resources_dirty(context):
    if not cfg.CONF.QUOTAS.track_quota_usage:
        return

    with context.session.begin(subtransactions=True):
        for res in res_reg.get_all_resources().values():
            if res_reg.is_tracked(res.name) and res.dirty:
                dirty_tenants_snap = res._dirty_tenants.copy()
                for tenant_id in dirty_tenants_snap:
                    query = common_db_api.model_query(
                            context, quota_models.QuotaUsage)
                    query = query.filter_by(resource=res.name).filter_by(
                            tenant_id=tenant_id)
                    usage_data = query.first()
                    # Set dirty if not set already. This effectively
                    # patches the inner notify method:
                    # https://github.com/openstack/neutron/blob/newton-eol/
                    # neutron/api/v2/base.py#L481
                    # to avoid updating the QuotaUsages table outside
                    # from that method (which starts a new transaction).
                    # The dirty marking would have been already done
                    # in the ml2plus manager at the end of the pre_commit
                    # stage (and prior to the plugin initiated transaction
                    # completing).
                    if usage_data and not usage_data.dirty:
                        res.mark_dirty(context)
예제 #8
0
파일: api.py 프로젝트: neoareslinux/neutron
def get_quota_usage_by_resource_and_tenant(context, resource, tenant_id,
                                           lock_for_update=False):
    """Return usage info for a given resource and tenant.

    :param context: Request context
    :param resource: Name of the resource
    :param tenant_id: Tenant identifier
    :param lock_for_update: if True sets a write-intent lock on the query
    :returns: a QuotaUsageInfo instance
    """

    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource, tenant_id=tenant_id)

    if lock_for_update:
        query = query.with_lockmode('update')

    result = query.first()
    if not result:
        return
    return QuotaUsageInfo(result.resource,
                          result.tenant_id,
                          result.in_use,
                          result.reserved,
                          result.dirty)
예제 #9
0
 def get_shared_with_tenant(context, rbac_db_model, obj_id, tenant_id):
     # NOTE(korzen) This method enables to query within already started
     # session
     return (common_db_mixin.model_query(context, rbac_db_model).filter(
         and_(rbac_db_model.object_id == obj_id, rbac_db_model.action
              == models.ACCESS_SHARED,
              rbac_db_model.target_tenant.in_(['*', tenant_id]))).count() !=
             0)
예제 #10
0
 def get_shared_with_tenant(context, rbac_db_model, obj_id, tenant_id):
     # NOTE(korzen) This method enables to query within already started
     # session
     return (common_db_mixin.model_query(context, rbac_db_model).filter(
             and_(rbac_db_model.object_id == obj_id,
                  rbac_db_model.action == models.ACCESS_SHARED,
                  rbac_db_model.target_tenant.in_(
                      ['*', tenant_id]))).count() != 0)
예제 #11
0
파일: rbac_db.py 프로젝트: jaguar13/neutron
 def is_shared_with_tenant(cls, context, obj_id, tenant_id):
     ctx = context.elevated()
     rbac_db_model = cls.rbac_db_model
     with ctx.session.begin(subtransactions=True):
         return (common_db_mixin.model_query(ctx, rbac_db_model).filter(
             and_(rbac_db_model.object_id == obj_id,
                  rbac_db_model.action == models.ACCESS_SHARED,
                  rbac_db_model.target_tenant.in_(
                      ['*', tenant_id]))).count() != 0)
예제 #12
0
 def is_shared_with_tenant(cls, context, obj_id, tenant_id):
     ctx = context.elevated()
     rbac_db_model = cls.rbac_db_model
     with ctx.session.begin(subtransactions=True):
         return (common_db_mixin.model_query(ctx, rbac_db_model).filter(
             and_(rbac_db_model.object_id == obj_id, rbac_db_model.action
                  == models.ACCESS_SHARED,
                  rbac_db_model.target_tenant.in_(['*', tenant_id
                                                   ]))).count() != 0)
예제 #13
0
파일: api.py 프로젝트: smokony/neutron
def set_all_quota_usage_dirty(context, resource, dirty=True):
    """Set the dirty bit on quota usage for all tenants.

    :param resource: the resource for which the dirty bit should be set
    :returns: the number of tenants for which the dirty bit was
              actually updated
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource)
    return query.update({'dirty': dirty})
예제 #14
0
파일: api.py 프로젝트: fuborui/neutron
def set_all_quota_usage_dirty(context, resource, dirty=True):
    """Set the dirty bit on quota usage for all tenants.

    :param resource: the resource for which the dirty bit should be set
    :returns: the number of tenants for which the dirty bit was
              actually updated
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource)
    return query.update({'dirty': dirty})
예제 #15
0
파일: api.py 프로젝트: dingboopt/neutron-x
def delete_policy_port_binding(context, policy_id, port_id):
    try:
        with context.session.begin(subtransactions=True):
            db_object = (db.model_query(context, models.QosPortPolicyBinding)
                         .filter_by(policy_id=policy_id,
                                    port_id=port_id).one())
            context.session.delete(db_object)
    except orm_exc.NoResultFound:
        raise n_exc.PortQosBindingNotFound(port_id=port_id,
                                           policy_id=policy_id)
예제 #16
0
파일: api.py 프로젝트: fuborui/neutron
def set_quota_usage_dirty(context, resource, tenant_id, dirty=True):
    """Set quota usage dirty bit for a given resource and tenant.

    :param resource: a resource for which quota usage if tracked
    :param tenant_id: tenant identifier
    :param dirty: the desired value for the dirty bit (defaults to True)
    :returns: 1 if the quota usage data were updated, 0 otherwise.
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource).filter_by(tenant_id=tenant_id)
    return query.update({'dirty': dirty})
예제 #17
0
파일: api.py 프로젝트: smokony/neutron
def set_quota_usage_dirty(context, resource, tenant_id, dirty=True):
    """Set quota usage dirty bit for a given resource and tenant.

    :param resource: a resource for which quota usage if tracked
    :param tenant_id: tenant identifier
    :param dirty: the desired value for the dirty bit (defaults to True)
    :returns: 1 if the quota usage data were updated, 0 otherwise.
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(resource=resource).filter_by(tenant_id=tenant_id)
    return query.update({'dirty': dirty})
예제 #18
0
파일: api.py 프로젝트: smokony/neutron
def set_resources_quota_usage_dirty(context, resources, tenant_id, dirty=True):
    """Set quota usage dirty bit for a given tenant and multiple resources.

    :param resources: list of resource for which the dirty bit is going
                      to be set
    :param tenant_id: tenant identifier
    :param dirty: the desired value for the dirty bit (defaults to True)
    :returns: the number of records for which the bit was actually set.
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(tenant_id=tenant_id)
    if resources:
        query = query.filter(quota_models.QuotaUsage.resource.in_(resources))
    # synchronize_session=False needed because of the IN condition
    return query.update({'dirty': dirty}, synchronize_session=False)
예제 #19
0
파일: api.py 프로젝트: fuborui/neutron
def set_resources_quota_usage_dirty(context, resources, tenant_id, dirty=True):
    """Set quota usage dirty bit for a given tenant and multiple resources.

    :param resources: list of resource for which the dirty bit is going
                      to be set
    :param tenant_id: tenant identifier
    :param dirty: the desired value for the dirty bit (defaults to True)
    :returns: the number of records for which the bit was actually set.
    """
    query = common_db_api.model_query(context, quota_models.QuotaUsage)
    query = query.filter_by(tenant_id=tenant_id)
    if resources:
        query = query.filter(quota_models.QuotaUsage.resource.in_(resources))
    # synchronize_session=False needed because of the IN condition
    return query.update({'dirty': dirty}, synchronize_session=False)
예제 #20
0
파일: driver.py 프로젝트: bigswitch/neutron
    def get_tenant_quotas(context, resources, tenant_id):
        """Given a list of resources, retrieve the quotas for the given
        tenant. If no limits are found for the specified tenant, the operation
        returns the default limits.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resource keys.
        :param tenant_id: The ID of the tenant to return quotas for.
        :return dict: from resource name to dict of name and limit
        """

        # init with defaults
        tenant_quota = dict((key, resource.default) for key, resource in resources.items())

        # update with tenant specific limits
        q_qry = common_db.model_query(context, quota_models.Quota).filter_by(tenant_id=tenant_id)
        for item in q_qry:
            tenant_quota[item["resource"]] = item["limit"]

        return tenant_quota
예제 #21
0
    def get_tenant_quotas(context, resources, tenant_id):
        """Given a list of resources, retrieve the quotas for the given
        tenant. If no limits are found for the specified tenant, the operation
        returns the default limits.

        :param context: The request context, for access checks.
        :param resources: A dictionary of the registered resource keys.
        :param tenant_id: The ID of the tenant to return quotas for.
        :return dict: from resource name to dict of name and limit
        """

        # init with defaults
        tenant_quota = dict((key, resource.default)
                            for key, resource in resources.items())

        # update with tenant specific limits
        q_qry = common_db.model_query(context, quota_models.Quota).filter_by(
            tenant_id=tenant_id)
        for item in q_qry:
            tenant_quota[item['resource']] = item['limit']

        return tenant_quota
예제 #22
0
def get_network_ids_by_network_policy_binding(context, policy_id):
    query = (db.model_query(context, models.QosNetworkPolicyBinding)
            .filter_by(policy_id=policy_id).all())
    return [entry.network_id for entry in query]
예제 #23
0
파일: rbac_db.py 프로젝트: jaguar13/neutron
 def _get_db_obj_rbac_entries(cls, context, rbac_obj_id, rbac_action):
     rbac_db_model = cls.rbac_db_model
     return common_db_mixin.model_query(context, rbac_db_model).filter(
         and_(rbac_db_model.object_id == rbac_obj_id,
              rbac_db_model.action == rbac_action))
예제 #24
0
 def _get_db_obj_rbac_entries(cls, context, rbac_obj_id, rbac_action):
     rbac_db_model = cls.rbac_db_model
     return common_db_mixin.model_query(context, rbac_db_model).filter(
         and_(rbac_db_model.object_id == rbac_obj_id,
              rbac_db_model.action == rbac_action))
예제 #25
0
def get_port_ids_by_port_policy_binding(context, policy_id):
    query = (db.model_query(context, models.QosPortPolicyBinding)
            .filter_by(policy_id=policy_id).all())
    return [entry.port_id for entry in query]
예제 #26
0
def get_objects(context, model, **kwargs):
    with context.session.begin(subtransactions=True):
        return (common_db_mixin.model_query(context,
                                            model).filter_by(**kwargs).all())
예제 #27
0
def get_objects(context, model, **kwargs):
    with context.session.begin(subtransactions=True):
        return (common_db_mixin.model_query(context, model)
                .filter_by(**kwargs)
                .all())