generated from DataStax-Examples/astradb-spring-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitpod-setup.sh
executable file
·88 lines (74 loc) · 4.17 KB
/
gitpod-setup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
function setupTable() {
if [[ -z "$ASTRA_DB_USERNAME" ]]; then
echo "What is your Astra DB username? 🚀"
read -r ASTRA_DB_USERNAME
export ASTRA_DB_USERNAME="${ASTRA_DB_USERNAME// /}"
gp env ASTRA_DB_USERNAME="${ASTRA_DB_USERNAME// /}" &>/dev/null
fi
if [[ -z "$ASTRA_DB_PASSWORD" ]]; then
echo "What is your Astra DB password? 🔒"
read -s ASTRA_DB_PASSWORD
export ASTRA_DB_PASSWORD="${ASTRA_DB_PASSWORD// /}"
gp env ASTRA_DB_PASSWORD="${ASTRA_DB_PASSWORD// /}" &>/dev/null
fi
if [[ -z "$ASTRA_DB_KEYSPACE" ]]; then
echo "What is your Astra keyspace name? 🔑"
read -r ASTRA_DB_KEYSPACE
export ASTRA_DB_KEYSPACE="${ASTRA_DB_KEYSPACE// /}"
gp env ASTRA_DB_KEYSPACE="${ASTRA_DB_KEYSPACE// /}" &>/dev/null
fi
if [[ -z "$ASTRA_DB_ID" ]]; then
echo "What is your Astra database id? Example: 4e62bc79-0e12-4667-bd7d-2191ece2a32c ☁️"
read -r ASTRA_DB_ID
export ASTRA_DB_ID="${ASTRA_DB_ID// /}"
gp env ASTRA_DB_ID="${ASTRA_DB_ID// /}" &>/dev/null
fi
if [[ -z "$ASTRA_DB_REGION" ]]; then
echo "What is your Astra database region? Example: us-east1 🌍"
read -r ASTRA_DB_REGION
export ASTRA_DB_REGION="${ASTRA_DB_REGION// /}"
gp env ASTRA_DB_REGION="${ASTRA_DB_REGION// /}" &>/dev/null
fi
# Get Astra auth token
echo "Getting your Astra auth token..."
AUTH_TOKEN=$(curl -s --request POST \
--url "https://${ASTRA_DB_ID}-${ASTRA_DB_REGION}.apps.astra.datastax.com/api/rest/v1/auth" \
--header 'content-type: application/json' \
--data '{"username":"'"${ASTRA_DB_USERNAME}"'","password":"'"${ASTRA_DB_PASSWORD}"'"}' | jq -r '.authToken')
export AUTH_TOKEN="${AUTH_TOKEN}"
gp env AUTH_TOKEN="${AUTH_TOKEN}" &>/dev/null
eval $(gp env -e)
# Create tables
echo "Creating Astra tables..."
curl -s --request POST \
--url "https://${ASTRA_DB_ID}-${ASTRA_DB_REGION}.apps.astra.datastax.com/api/rest/v1/keyspaces/${ASTRA_DB_KEYSPACE}/tables" \
--header 'content-type: application/json' \
--header "x-cassandra-token: ${AUTH_TOKEN}" \
--data '{"ifNotExists":true,"columnDefinitions":[{"static":false,"name":"name","typeDefinition":"text"},{"static":false,"name":"id","typeDefinition":"int"},{"static":false,"name":"actor_name","typeDefinition":"text"},{"static":false,"name":"house_name","typeDefinition":"text"},{"static":false,"name":"royal","typeDefinition":"boolean"}],"primaryKey":{"partitionKey":["name"]},"tableOptions":{"defaultTimeToLive":0},"name":"spring_rest_characters"}'
curl -s --request POST \
--url "https://${ASTRA_DB_ID}-${ASTRA_DB_REGION}.apps.astra.datastax.com/api/rest/v1/keyspaces/${ASTRA_DB_KEYSPACE}/tables/spring_rest_characters/rows" \
--header 'content-type: application/json' \
--header "x-cassandra-token: ${AUTH_TOKEN}" \
--data '{"columns":[{"name":"id","value":1},{"name":"name","value":"Jon Snow"},{"name":"actor_name","value":"Kit Harington"},{"name":"house_name","value":"Stark"},{"name":"royal","value":true}]}'
curl -s --request POST \
--url "https://${ASTRA_DB_ID}-${ASTRA_DB_REGION}.apps.astra.datastax.com/api/rest/v1/keyspaces/${ASTRA_DB_KEYSPACE}/tables/spring_rest_characters/rows" \
--header 'content-type: application/json' \
--header "x-cassandra-token: ${AUTH_TOKEN}" \
--data '{"columns":[{"name":"id","value":2},{"name":"name","value":"Daenerys Targaryen"},{"name":"actor_name","value":"Emilia Clark"},{"name":"house_name","value":"Targaryen"},{"name":"royal","value":true}]}'
curl -s --request POST \
--url "https://${ASTRA_DB_ID}-${ASTRA_DB_REGION}.apps.astra.datastax.com/api/rest/v1/keyspaces/${ASTRA_DB_KEYSPACE}/tables/spring_rest_characters/rows" \
--header 'content-type: application/json' \
--header "x-cassandra-token: ${AUTH_TOKEN}" \
--data '{"columns":[{"name":"id","value":3},{"name":"name","value":"Tyrion Lannister"},{"name":"actor_name","value":"Peter Dinklage"},{"name":"house_name","value":"Lannister"},{"name":"royal","value":false}]}'
}
setupTable
while [ "$AUTH_TOKEN" = '' ]; do
echo "Your database details were invalid. Trying again:"
unset ASTRA_DB_ID
unset ASTRA_DB_REGION
unset ASTRA_DB_KEYSPACE
unset ASTRA_DB_PASSWORD
unset ASTRA_DB_USERNAME
setupTable
done