Пример #1
0
def sync_quota(resource, _payload):
    ret_set = []
    extra_infos = RESOURCE_TO_DESC_EXTRA_PARA.get(resource)
    func = RESOURCE_TO_DESC_FUN.get(resource)
    for extra_info in extra_infos:
        resp = func(dict(_payload, **extra_info))
        if resp.get("ret_code") != 0:
            logger.error("cannot get the resource list of %s" %
                         unicode(resource))
            return
        ret_set += resp.get("ret_set")

    if resource != "net":
        value = len(ret_set)
        # add logic for pre-pay
        resource_ids = []
        records = []
        pre_pay_num = 0
        if resource == 'disk':
            for disk_info in ret_set:
                resource_ids.append(disk_info.get('disk_id'))
            records = DisksModel.get_exact_disks_by_ids(resource_ids)
        elif resource == 'instance':
            for inst_info in ret_set:
                resource_ids.append(inst_info.get('instance_id'))
            records = InstancesModel.get_exact_instances_by_ids(resource_ids)
        elif resource == 'backup':
            for backup_info in ret_set:
                resource_ids.append(backup_info.get('backup_id'))
            records_fir = InstanceBackupModel.\
                get_exact_backups_by_ids(resource_ids)
            records_sec = DiskBackupModel.get_exact_backups_by_ids(
                resource_ids)
            for record in records_fir:
                records.append(record)
            for record in records_sec:
                records.append(record)
        elif resource == 'pub_ip':
            for ip_info in ret_set:
                resource_ids.append(ip_info.get('ip_id'))
            records = IpsModel.get_exact_ips_by_ids(resource_ids)
        if len(records) > 0:
            for record in records:
                if getattr(record, 'charge_mode') != "pay_on_time":
                    pre_pay_num += 1
        value -= pre_pay_num

        payload = deepcopy(_payload)
        q_type = RESOURCE_TO_GENERAL_QTYPE.get(resource, None)
        if q_type:
            payload.update({"quota_type": q_type})
            synchronize_visible_to_db(payload, value)
    # special quota sync
    if resource in SYNC_QUOTA_FUNC.keys():
        func = SYNC_QUOTA_FUNC.get(resource)
        func(ret_set, _payload["owner"], _payload["zone"])
Пример #2
0
        def handle_args(*args, **kwargs):

            _payload = kwargs.get("payload", None)
            if _payload is None:
                _payload = args[0]
            payload = copy.deepcopy(_payload)
            owner = payload.get("owner")
            zone = payload.get("zone")
            capacity = 1

            resp = func(*args, **kwargs)
            value = resp.get("ret_set")
            logger.info("%s, %s, %s" % (str(owner), str(zone), str(quota_type)))
            if value is not None:

                # add logic for pre-pay
                pre_pay_num = 0
                if resp.get("ret_code") == 0:
                    records = []
                    if quota_type == 'disk':
                        resource_ids = payload.get("disk_id")
                        records = DisksModel.get_exact_disks_by_ids(resource_ids)
                    elif quota_type == 'instance':
                        resource_ids = payload.get("instances")
                        records = InstancesModel.get_exact_instances_by_ids(
                            resource_ids)
                    elif quota_type == 'backup':
                        resource_ids = payload.get("backup_id_list")
                        records_fir = InstanceBackupModel. \
                            get_exact_backups_by_ids(resource_ids)
                        records_sec = DiskBackupModel. \
                            get_exact_backups_by_ids(resource_ids)
                        for record in records_fir:
                            records.append(record)
                        for record in records_sec:
                            records.append(record)
                    elif quota_type == 'pub_ip':
                        resource_ids = payload.get("ips")
                        records = IpsModel.get_exact_ips_by_ids(resource_ids)
                    if len(records) > 0:
                        for record in records:
                            if getattr(record, 'charge_mode') != "pay_on_time":
                                pre_pay_num += 1

                if isinstance(value, list):
                    dec_resp = append_quota(
                        zone=zone, owner=owner, q_type=quota_type,
                        value=((len(value) - pre_pay_num) * capacity))
                    if dec_resp.get("ret_code") != 0:
                        logger.error(QUOTAS_MSG.get(QuotaErrorCode.
                                                    QUOTA_MODIFICATION_ERROR))
                    else:
                        logger.info("%s quota increased by: %s" %
                                    (str(quota_type), str(len(value) * capacity)))
                else:
                    dec_resp = append_quota(
                        zone=zone, owner=owner, q_type=quota_type,
                        value=(1 - pre_pay_num) * capacity)
                    if dec_resp.get("ret_code") != 0:
                        logger.error(QUOTAS_MSG.get(QuotaErrorCode.
                                                    QUOTA_MODIFICATION_ERROR))
                    else:
                        logger.info("%s quota increased by: %s" %
                                    (str(quota_type), str(capacity)))
            return resp