如何在业务代码里管理配额

创建备份的时候,修改配额使用量:

cinder.backup.api.API#create

from cinder import quota
from cinder import quota_utils


    def create(self, context, name, description, volume_id,
               container, incremental=False, availability_zone=None,
               force=False, snapshot_id=None):
        """Make the RPC call to create a volume backup."""
        
        # Reserve a quota before setting volume status and backup status
        try:
            # 创建配额预留
            reserve_opts = {'backups': 1,
                            'backup_gigabytes': volume['size']}
            reservations = QUOTAS.reserve(context, **reserve_opts)
        except exception.OverQuota as e:
            quota_utils.process_reserve_over_quota(
                context, e,
                resource='backups',
                size=volume.size)
        try:
            
            
            # 提交配额预留,修改quota_usage里的使用量
            QUOTAS.commit(context, reservations)
        except Exception:
            # 延迟抛出异常,先把创建了一半的backup先销毁了,并回退配额,然后再抛出异常
            with excutils.save_and_reraise_exception():
                try:
                    if backup and 'id' in backup:
                        backup.destroy()
                finally:
                    # 回退预留
                    QUOTAS.rollback(context, reservations)

        # TODO(DuncanT): In future, when we have a generic local attach,
        #                this can go via the scheduler, which enables
        #                better load balancing and isolation of services
        self.backup_rpcapi.create_backup(context, backup)

        return backup

你可能感兴趣的:(如何在业务代码里管理配额)