static int s_parse_instance_info()

in source/aws_imds_client.c [1167:1279]


static int s_parse_instance_info(cJSON *document_root, struct aws_imds_instance_info *dest) {

    bool success = false;
    cJSON *account_id = cJSON_GetObjectItemCaseSensitive(document_root, "accountId");
    if (!cJSON_IsString(account_id) || (account_id->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse accountId from Json document for ec2 instance info.");
        goto done;
    }
    dest->account_id = aws_byte_cursor_from_c_str(account_id->valuestring);

    cJSON *architecture = cJSON_GetObjectItemCaseSensitive(document_root, "architecture");
    if (!cJSON_IsString(architecture) || (architecture->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse architecture from Json document for ec2 instance info.");
        goto done;
    }
    dest->architecture = aws_byte_cursor_from_c_str(architecture->valuestring);

    cJSON *availability_zone = cJSON_GetObjectItemCaseSensitive(document_root, "availabilityZone");
    if (!cJSON_IsString(availability_zone) || (availability_zone->valuestring == NULL)) {
        AWS_LOGF_ERROR(
            AWS_LS_IMDS_CLIENT, "Failed to parse availabilityZone from Json document for ec2 instance info.");
        goto done;
    }
    dest->availability_zone = aws_byte_cursor_from_c_str(availability_zone->valuestring);

    cJSON *billing_products = cJSON_GetObjectItemCaseSensitive(document_root, "billingProducts");
    if (cJSON_IsArray(billing_products)) {
        cJSON *element;
        cJSON_ArrayForEach(element, billing_products) {
            if (cJSON_IsString(element) && (element->valuestring != NULL)) {
                struct aws_byte_cursor item = aws_byte_cursor_from_c_str(element->valuestring);
                aws_array_list_push_back(&dest->billing_products, (const void *)&item);
            }
        }
    }

    cJSON *marketplace_product_codes = cJSON_GetObjectItemCaseSensitive(document_root, "marketplaceProductCodes");
    if (cJSON_IsArray(marketplace_product_codes)) {
        cJSON *element;
        cJSON_ArrayForEach(element, marketplace_product_codes) {
            if (cJSON_IsString(element) && (element->valuestring != NULL)) {
                struct aws_byte_cursor item = aws_byte_cursor_from_c_str(element->valuestring);
                aws_array_list_push_back(&dest->marketplace_product_codes, (const void *)&item);
            }
        }
    }

    cJSON *image_id = cJSON_GetObjectItemCaseSensitive(document_root, "imageId");
    if (cJSON_IsString(image_id) && (image_id->valuestring != NULL)) {
        dest->image_id = aws_byte_cursor_from_c_str(image_id->valuestring);
    }

    cJSON *instance_id = cJSON_GetObjectItemCaseSensitive(document_root, "instanceId");
    if (!cJSON_IsString(instance_id) || (instance_id->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse instanceId from Json document for ec2 instance info.");
        goto done;
    }
    dest->instance_id = aws_byte_cursor_from_c_str(instance_id->valuestring);

    cJSON *instance_type = cJSON_GetObjectItemCaseSensitive(document_root, "instanceType");
    if (!cJSON_IsString(instance_type) || (instance_type->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse instanceType from Json document for ec2 instance info.");
        goto done;
    }
    dest->instance_type = aws_byte_cursor_from_c_str(instance_type->valuestring);

    cJSON *kernel_id = cJSON_GetObjectItemCaseSensitive(document_root, "kernelId");
    if (cJSON_IsString(kernel_id) && (kernel_id->valuestring != NULL)) {
        dest->kernel_id = aws_byte_cursor_from_c_str(kernel_id->valuestring);
    }

    cJSON *private_ip = cJSON_GetObjectItemCaseSensitive(document_root, "privateIp");
    if (cJSON_IsString(private_ip) && (private_ip->valuestring != NULL)) {
        dest->private_ip = aws_byte_cursor_from_c_str(private_ip->valuestring);
    }

    cJSON *ramdisk_id = cJSON_GetObjectItemCaseSensitive(document_root, "ramdiskId");
    if (cJSON_IsString(ramdisk_id) && (ramdisk_id->valuestring != NULL)) {
        dest->ramdisk_id = aws_byte_cursor_from_c_str(ramdisk_id->valuestring);
    }

    cJSON *region = cJSON_GetObjectItemCaseSensitive(document_root, "region");
    if (!cJSON_IsString(region) || (region->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse region from Json document for ec2 instance info.");
        goto done;
    }
    dest->region = aws_byte_cursor_from_c_str(region->valuestring);

    cJSON *version = cJSON_GetObjectItemCaseSensitive(document_root, "version");
    if (!cJSON_IsString(version) || (version->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse version from Json document for ec2 instance info.");
        goto done;
    }
    dest->version = aws_byte_cursor_from_c_str(version->valuestring);

    cJSON *pending_time = cJSON_GetObjectItemCaseSensitive(document_root, "pendingTime");
    if (!cJSON_IsString(pending_time) || (pending_time->valuestring == NULL)) {
        AWS_LOGF_ERROR(AWS_LS_IMDS_CLIENT, "Failed to parse pendingTime from Json document for ec2 instance info.");
        goto done;
    }

    struct aws_byte_cursor pending_time_cursor = aws_byte_cursor_from_c_str(pending_time->valuestring);
    if (aws_date_time_init_from_str_cursor(&dest->pending_time, &pending_time_cursor, AWS_DATE_FORMAT_ISO_8601)) {
        AWS_LOGF_ERROR(
            AWS_LS_IMDS_CLIENT, "pendingTime in instance info Json document is not a valid ISO_8601 date string.");
        goto done;
    }

    success = true;

done:
    return success ? AWS_OP_ERR : AWS_OP_SUCCESS;
}