#!/bin/bash

set -eux

CUR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
source "$CUR/../_util/test_prepare"

project_id="$1"
cluster_id="$2"

function test_create_import() {
	# TODO: create test data here
	# TODO: create AWS role here

	# TODO: update the S3 URI and role ARN
	create_req='{
        "name": "integration_test_create_import",
        "spec": {
            "source": {
                "type": "S3",
                "uri": "s3://dingshun-pulumi-state/shard-data/",
                "aws_assume_role_access": {
                    "assume_role": "arn:aws:iam::385595570414:role/dingshun-test"
                },
                "format": {
                    "type": "CSV",
                    "csv_config": {
                        "delimiter": ",",
                        "quote": "\"",
                        "backslash_escape": false,
                        "has_header_row": true
                    }
                }
            }
        }
    }'

	import_id=$(create_import "$project_id" "$cluster_id" "$create_req")

	# Exit if import_id is null
	[ -z "$import_id" ] && {
		echo "import_id is null"
		exit 1
	}

	wait_import_completed "$project_id" "$cluster_id" "$import_id"

	status="$?"
	if [ "$status" != "0" ]; then
		echo "import failed"
		exit 1
	fi

	# TODO: check the imported data in the cluster

	echo "import success!"
}

test_create_import
