Untitled

mail@pastecode.io avatar
unknown
python
15 days ago
1.2 kB
1
Indexable
Never
output_filename = "output.csv"


def save_to_csv(data):
    with open(output_filename, mode="w", encoding="utf-8", errors="ignore") as file:
        writer = csv.writer(file, lineterminator="\n")
        writer.writerow(["MDM", "disktype", "volume_size_total", "volume_size_used"])
        for one in data:
            if one:
                writer.writerow(one)


def parse_response(content):
    records = content["records"]
    pattern = r"n\d{2}_([a-z]+)"
    final_data = []
    for one in records:
        instance_name = None
        aggregates = None
        name = None
        storage_type_raw = None
        space = None
        size = None
        used = None
        instance_name = one["name"]
        aggregates = one["aggregates"]
        if isinstance(aggregates, list):
            name = aggregates[0]["name"]
            storage_type_raw = name
        if storage_type_raw:
            storage_type = re.search(pattern, storage_type_raw).group(1)
        space = one["space"]
        size = space["size"]
        used = space["used"]
        final_data.append([instance_name, storage_type, size, used])

    return final_data
Leave a Comment