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

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

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容