components/openstack/neutron/files/evs/db/quotas_db.py
changeset 3998 5bd484384122
parent 3997 0ca3f3d6c919
child 4002 95b8f35fcdd5
equal deleted inserted replaced
3997:0ca3f3d6c919 3998:5bd484384122
     1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
       
     2 
       
     3 # Copyright 2011 OpenStack Foundation.
       
     4 # All Rights Reserved.
       
     5 #
       
     6 # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     7 #
       
     8 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
       
     9 #    not use this file except in compliance with the License. You may obtain
       
    10 #    a copy of the License at
       
    11 #
       
    12 #         http://www.apache.org/licenses/LICENSE-2.0
       
    13 #
       
    14 #    Unless required by applicable law or agreed to in writing, software
       
    15 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
       
    16 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
       
    17 #    License for the specific language governing permissions and limitations
       
    18 #    under the License.
       
    19 #
       
    20 # @author: Girish Moodalbail, Oracle, Inc.
       
    21 #
       
    22 
       
    23 import sqlalchemy as sa
       
    24 
       
    25 from neutron.db import quota_db
       
    26 from neutron.openstack.common import uuidutils
       
    27 from neutron.plugins.evs.db import api as evs_db
       
    28 
       
    29 
       
    30 class Quota(evs_db.EVS_DB_BASE):
       
    31     """Represent a single quota override for a tenant.
       
    32 
       
    33     If there is no row for a given tenant id and resource, then the
       
    34     default for the quota class is used.
       
    35     """
       
    36 
       
    37     id = sa.Column(sa.String(36), primary_key=True,
       
    38                    default=uuidutils.generate_uuid)
       
    39     tenant_id = sa.Column(sa.String(255), index=True)
       
    40     resource = sa.Column(sa.String(255))
       
    41     limit = sa.Column(sa.Integer)
       
    42 
       
    43 
       
    44 class EVSDbQuotaDriver(quota_db.DbQuotaDriver):
       
    45     """
       
    46     Driver to perform necessary checks to enforce quotas and obtain
       
    47     quota information.  The default driver utilizes the local
       
    48     database.
       
    49     """
       
    50 
       
    51     @staticmethod
       
    52     def get_tenant_quotas(context, resources, tenant_id):
       
    53         """
       
    54         Given a list of resources, retrieve the quotas for the given
       
    55         tenant.
       
    56 
       
    57         :param context: The request context, for access checks.
       
    58         :param resources: A dictionary of the registered resource keys.
       
    59         :param tenant_id: The ID of the tenant to return quotas for.
       
    60         :return dict: from resource name to dict of name and limit
       
    61         """
       
    62 
       
    63         return quota_db.DbQuotaDriver().\
       
    64             get_tenant_quotas(evs_db.get_evs_context(context), resources,
       
    65                               tenant_id)
       
    66 
       
    67     @staticmethod
       
    68     def delete_tenant_quota(context, tenant_id):
       
    69         """Delete the quota entries for a given tenant_id.
       
    70 
       
    71         Atfer deletion, this tenant will use default quota values in conf.
       
    72         """
       
    73 
       
    74         quota_db.DbQuotaDriver().\
       
    75             delete_tenant_quota(evs_db.get_evs_context(context), tenant_id)
       
    76 
       
    77     @staticmethod
       
    78     def get_all_quotas(context, resources):
       
    79         """
       
    80         Given a list of resources, retrieve the quotas for the all
       
    81         tenants.
       
    82 
       
    83         :param context: The request context, for access checks.
       
    84         :param resources: A dictionary of the registered resource keys.
       
    85         :return quotas: list of dict of tenant_id:, resourcekey1:
       
    86         resourcekey2: ...
       
    87         """
       
    88 
       
    89         return quota_db.DbQuotaDriver().\
       
    90             get_all_quotas(evs_db.get_evs_context(context), resources)
       
    91 
       
    92     @staticmethod
       
    93     def update_quota_limit(context, tenant_id, resource, limit):
       
    94         quota_db.DbQuotaDriver().\
       
    95             update_quota_limit(evs_db.get_evs_context(context), tenant_id,
       
    96                                resource, limit)
       
    97 
       
    98     def limit_check(self, context, tenant_id, resources, values):
       
    99         """Check simple quota limits.
       
   100 
       
   101         For limits--those quotas for which there is no usage
       
   102         synchronization function--this method checks that a set of
       
   103         proposed values are permitted by the limit restriction.
       
   104 
       
   105         This method will raise a QuotaResourceUnknown exception if a
       
   106         given resource is unknown or if it is not a simple limit
       
   107         resource.
       
   108 
       
   109         If any of the proposed values is over the defined quota, an
       
   110         OverQuota exception will be raised with the sorted list of the
       
   111         resources which are too high.  Otherwise, the method returns
       
   112         nothing.
       
   113 
       
   114         :param context: The request context, for access checks.
       
   115         :param tenant_id: The tenant_id to check the quota.
       
   116         :param resources: A dictionary of the registered resources.
       
   117         :param values: A dictionary of the values to check against the
       
   118                        quota.
       
   119         """
       
   120 
       
   121         super(EVSDbQuotaDriver, self).\
       
   122             limit_check(evs_db.get_evs_context(context), tenant_id, resources,
       
   123                         values)