#/bin/bash
# requires: httpie, jq

TEST_NUMBER=$1
API_BASE="https://pulp2.dev/pulp/api/v2/"
TEST_MANIFEST_DIR="/home/vagrant/devel/pulp_docker/busybox_manifest_lists/"

function phttp (){
		http --verify no --cert ~/.pulp/user-cert.pem $@
}

function create_upload (){
		 phttp --ignore-stdin POST "$API_BASE"content/uploads/ | jq -r .upload_id
}

function upload_bits (){
		phttp -vvv PUT "$API_BASE"content/uploads/"$UPLOAD_ID"/0/ < "$TEST_MANIFEST_DIR""$TEST_NUMBER"_test_manifest_list.json
}

function import_upload (){
		phttp POST "$API_BASE"repositories/busybox/actions/import_upload/ \
			upload_id="$UPLOAD_ID" \
			unit_type_id="docker_manifest_list" \
			unit_key:='{"digest":"sha256:validateme"}'
}

function bad_import_upload (){
		phttp POST "$API_BASE"repositories/busybox/actions/import_upload/ \
			upload_id="$UPLOAD_ID" \
			unit_type_id="bad_docker_manifest_list" \
			unit_key:='{"digest":"validateme"}'
}
echo "Creating Upload"
UPLOAD_ID="$(create_upload)"
echo "Uploading Bits"
upload_bits
echo "Import into Repo"
import_upload
