Skip to content
    Sandbox DocumentAPI@CedrickLunven
    main.sh
    Config files
    .replit
    replit.nix
    #!/bin/bash

    clear
    printf "\x1b[34m[SETUP]\x1b[0m\n";
    printf "\x1b[33m\n(1/4) - Please enter your database identifier:\x1b[0m\n";
    read ASTRA_DB_ID
    #export ASTRA_DB_ID=3ed83de7-d97f-4fb6-bf9f-82e9f7eafa23

    printf "\x1b[33m\n(2/4) - Please enter your database region:\x1b[0m\n";
    read ASTRA_DB_REGION
    #export ASTRA_DB_REGION=eu-central-1

    printf "\x1b[33m\n(3/4) - Please enter your keyspace (must exist):\x1b[0m\n";
    read ASTRA_DB_KEYSPACE
    #export ASTRA_DB_KEYSPACE=devoxx

    printf "\x1b[33m\n(4/4) - Please enter your token (AstraCS:...)\x1b[0m\n";
    read ASTRA_DB_APPLICATION_TOKEN
    #export ASTRA_DB_APPLICATION_TOKEN=AstraCS:gdZaqzmFZszaBTOlLgeecuPs:edd25600df1c01506f5388340f138f277cece2c93cb70f4b5fa386490daa5d44

    echo ""
    printf "\x1b[34m[VALIDATIONS]\x1b[0m\n";
    # --------------------------------------------------------------
    # Token Validation
    # --------------------------------------------------------------
    token_status=$(curl --location \
    -s -o /dev/null -f -w "%{http_code}" \
    --request GET https://api.astra.datastax.com/v2/currentOrg \
    --header "Authorization: Bearer $ASTRA_DB_APPLICATION_TOKEN" \
    --data-raw '')
    if (test $token_status -eq 200)
    then
    printf "Token........................\x1b[32m[OK]\x1b[0m\n"
    else
    printf "Token........................\x1b[31m[KO]\x1b[0m\n"
    exit 400
    fi

    # --------------------------------------------------------------
    # Database Id Validation
    # --------------------------------------------------------------
    db_status=$(curl --location \
    -s -o /dev/null -f -w "%{http_code}" \
    --request GET https://api.astra.datastax.com/v2/databases/$ASTRA_DB_ID \
    --header "Authorization: Bearer $ASTRA_DB_APPLICATION_TOKEN" \
    --data-raw '')
    if (test $db_status -eq 200)
    then
    printf "Database Id..................\x1b[32m[OK]\x1b[0m\n"
    else
    printf "Database Id..................\x1b[31m[KO]\x1b[0m\n"
    exit 400
    fi

    # --------------------------------------------------------------
    # Database Region
    # --------------------------------------------------------------
    db_region=$(curl --location \
    -s -f \
    --request GET https://api.astra.datastax.com/v2/databases/$ASTRA_DB_ID/datacenters \
    --header "Authorization: Bearer $ASTRA_DB_APPLICATION_TOKEN" \
    --data-raw '')
    db_region_status=$(echo $db_region | grep "\"region\":\"$ASTRA_DB_REGION\"" | wc -l)
    if (test $db_region_status -eq 1)
    then
    printf "Database Region..............\x1b[32m[OK]\x1b[0m\n"
    else
    printf "Database Region..............\x1b[31m[KO]\x1b[0m\n"
    exit 400
    fi

    # --------------------------------------------------------------
    # Keyspaces
    # --------------------------------------------------------------
    ks_status=$(curl --location \
    -s -o /dev/null -f -w "%{http_code}" \
    --request GET https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/$ASTRA_DB_KEYSPACE \
    -H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
    -H 'Content-Type: application/json')
    if (test $ks_status -eq 200)
    then
    printf "Keyspace.....................\x1b[32m[OK]\x1b[0m\n"
    else
    printf "Keyspace.....................\x1b[31m[KO]\x1b[0m\n"
    exit 400
    fi

    echo ""
    printf "\x1b[34m[DOCUMENT API]\x1b[0m\n";

    # List Namespaces
    echo ""
    printf "\x1b[33m(1) List Available Namespaces\x1b[0m\n"
    echo ""
    printf "\x1b[33m+ Query:\x1b[0m"
    echo ""
    echo "curl -f --request GET https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/schemas/namespaces -H \"X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN\" -H 'Content-Type: application/json'"
    echo ""
    printf "\x1b[33m+ Results:\x1b[0m"
    echo ""
    curl -f --request GET \
    https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/schemas/namespaces \
    -H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
    -H 'Content-Type: application/json'

    # Create collections
    echo ""
    echo ""
    printf "\x1b[33m(2) Create collection users and sample Document users \x1b[0m\n"
    echo ""
    printf "\x1b[33m+ Query:\x1b[0m"
    echo ""
    echo "curl --request POST --url https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/namespaces/$ASTRA_DB_KEYSPACE/collections/users -H \"X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN\" -H 'Content-Type: application/json' -d '{ \"firstname\": \"Cedrick\", \"lastname\": \"Frenchy\"}'"
    echo ""
    printf "\x1b[33m+ Results:\x1b[0m"
    echo ""
    curl --request POST \
    --url https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/namespaces/$ASTRA_DB_KEYSPACE/collections/users \
    -H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
    -H 'Content-Type: application/json' \
    -d '{
    "firstname": "Cedrick",
    "lastname": "Frenchy"
    }'

    # List Collections
    echo ""
    echo ""
    printf "\x1b[33m(3) List Available Collections in $ASTRA_DB_KEYSPACE \x1b[0m"
    echo ""
    echo ""
    printf "\x1b[33m+ Query:\x1b[0m"
    echo ""
    echo "curl -f --request GET https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/namespaces/$ASTRA_DB_KEYSPACE/collections -H \"X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN\" -H 'Content-Type: application/json'"
    echo ""
    printf "\x1b[33m+ Results:\x1b[0m"
    echo ""
    curl -f --request GET \
    https://$ASTRA_DB_ID-$ASTRA_DB_REGION.apps.astra.datastax.com/api/rest/v2/namespaces/$ASTRA_DB_KEYSPACE/collections \
    -H "X-Cassandra-Token: $ASTRA_DB_APPLICATION_TOKEN" \
    -H 'Content-Type: application/json'


    echo ""
    printf "\x1b[32m\nYou can stay here tand keep posting curl commands.\n";
    echo ""