Example #1
0
def restore_disk_backup(payload):
    resource_id = payload.pop("resource_id", None)
    backup_id = payload.pop("backup_id")
    action = payload.pop("action")
    version = payload.pop("version")
    backup_info = DiskBackupModel.get_backup_by_id(backup_id=backup_id)
    backup_uuid = backup_info.uuid

    if resource_id is not None:
        try:
            resource_uuid = DisksModel.get_disk_by_id(disk_id=resource_id).uuid
        except Exception:
            error_info = "cannot find disk with disk_id " + resource_id
            return console_response(BackupErrorCode.RESTORE_RESOURCE_NOT_FOUND,
                                    error_info)
    else:
        payload.update({
            "action": "DescribeDiskBackup",
            "backup_id": backup_uuid
        })
        # resp = api.get(payload=payload, timeout=10)
        resp = api.get(payload=payload)
        if resp.get("code") != 0:
            return console_response(CommonErrorCode.REQUEST_API_ERROR,
                                    resp.get("msg"))
        elif resp["data"]["total_count"] <= 0:
            return console_response(
                BackupErrorCode.ASSOCIATE_DISK_NOT_FOUND,
                "the disk related to the backup cannot "
                "be found, may be it has already "
                "been deleted")
        resource_uuid = resp["data"]["ret_set"][0]["volume_id"]
        zone_record = ZoneModel.get_zone_by_name(payload["zone"])
        resource_record = DisksModel.get_disk_by_uuid(resource_uuid,
                                                      zone_record)
        if resource_record:
            resource_id = resource_record.disk_id
    payload.update({
        "action": action,
        "version": version,
        "disk_uuid": resource_uuid,
        "backup_id": backup_uuid
    })
    # resp = api.get(payload=payload, timeout=10)
    resp = api.get(payload=payload)

    msg = resp.get("msg")
    if msg is None:
        msg = "Success"
    code = 0
    if resp.get("code") != 0:
        code = CommonErrorCode.REQUEST_API_ERROR
    # source_backup_id is for record
    return console_response(code, msg, 1, [{
        "disk_id": resource_id,
        "backup_id": backup_id
    }], {"source_backup_id": resource_id})
Example #2
0
def format_data_list(item, data_list, instance, payload):
    name_dict = {}
    item = str(item)
    new_data_list = []
    if item.startswith("CPU") or item.startswith("MEMORY"):
        new_data_list = [None]
    elif item.startswith("NET"):
        instance = dict(instance)
        net_info = instance.get("addresses")
        for net_name, net_detail in net_info.items():
            nets = list(net_detail)
            for net in nets:
                net = dict(net)
                ip_addr = net.get("addr")
                mac_addr = net.get("OS-EXT-IPS-MAC:mac_addr")
                addr_type = net.get("OS-EXT-IPS:type")
                if ip_addr in data_list and addr_type.strip() == "fixed":
                    new_data_list.append(mac_addr)
                    name_dict.update({mac_addr: ip_addr})
        new_data_list = set(new_data_list)
    elif item.startswith("SYS_DISK"):
        new_data_list = ["vda"]
    elif item.startswith("DATA_DISK"):
        instance = dict(instance)
        attached_disks = instance.get("os-extended-volumes:volumes_attached")
        for disk in attached_disks:
            disk = dict(disk)
            disk_uuid = disk.get("id")
            disk_info = DisksModel.get_disk_by_uuid(
                disk_uuid, ZoneModel.get_zone_by_name(payload.get("zone")))
            disk_id = disk_info.disk_id
            if disk_uuid in data_list:
                new_data_list.append(disk_uuid)
                name_dict.update({disk_uuid: disk_id})
    elif item.startswith("PUBLIC_IP"):
        instance = dict(instance)
        float_ip_info = instance.get("addresses")
        for net_name, net_detail in float_ip_info.items():
            nets = list(net_detail)
            for net in nets:
                net = dict(net)
                ip_addr = net.get("addr")
                addr_type = net.get("OS-EXT-IPS:type")
                if ip_addr in data_list and addr_type.strip() == "floating":
                    new_data_list.append(ip_addr)
                    name_dict.update({ip_addr: ip_addr})
        new_data_list = set(new_data_list)

    return new_data_list, name_dict
Example #3
0
def get_disks_info(volumes_attached, zone):
    zone_record = ZoneModel.get_zone_by_name(zone)
    disks = []
    for volume in volumes_attached:
        disk_uuid = volume["id"]
        disk_obj = DisksModel.get_disk_by_uuid(uuid=disk_uuid,
                                               zone=zone_record)
        if disk_obj:
            disk = {}
            disk["disk_id"] = disk_obj.disk_id
            disk["disk_name"] = disk_obj.name
            disk["create_datetime"] =\
                datetime_to_timestamp(disk_obj.create_datetime)
            disks.append(disk)
    return disks
Example #4
0
def get_resource_inst_by_uuid(backup_type, uuid, zone, deleted=False):
    if backup_type == "disk":
        try:
            inst = DisksModel.get_disk_by_uuid(uuid=uuid,
                                               zone=zone,
                                               deleted=deleted)
            return inst
        except Exception as exp:
            return None
    else:
        try:
            inst = InstancesModel.get_instance_by_uuid(uuid=uuid,
                                                       deleted=deleted)
            return inst
        except Exception as exp:
            return None
Example #5
0
def get_data_disk(ins_list, payload):
    info_dict = {}
    name_dict = {}
    for ins in ins_list:
        ins_uuid = ins.get("id")
        data_list = []
        attached_disks = ins.get("os-extended-volumes:volumes_attached")
        for disk in attached_disks:
            disk = dict(disk)
            disk_uuid = disk.get("id")
            disk_info = DisksModel.get_disk_by_uuid(
                disk_uuid, ZoneModel.get_zone_by_name(payload.get("zone")))
            if disk_info is not None:
                disk_id = disk_info.disk_id
                name_dict.update({disk_uuid: disk_id})
                data_list.append(disk_uuid)
        info_dict[ins_uuid] = data_list
    return info_dict, name_dict