/
astra_api.yml
308 lines (308 loc) · 14.1 KB
/
astra_api.yml
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# Demonstration of using Astra APIs (DevOps & Data) with Ansible
# ansible-playbook astra_api.yml --extra-vars 'astra_devops_api_auth_token=<your_token> astra_db_name=astra astra_db_keyspace=astra astra_db_cloudProvider=GCP astra_db_tier=serverless astra_db_capacityUnits=1 astra_db_region=us-east1 astra_db_username=astra1 astra_db_tableName=astra1'
# Prerequisite: Create an Astra account, Create a Service Account, Copy the Credentials -- https://docs.astra.datastax.com/docs/creating-a-new-service-account-for-your-database
---
- hosts: localhost
connection: local
gather_facts: false
vars_prompt:
- name: astra_devops_api_auth_token
prompt: Enter the astra_devops_api_auth_token
private: yes
- name: astra_db_name
prompt: Enter your Astra database name?
- name: astra_db_keyspace
prompt: Enter your Astra database's keyspace name?
- name: astra_db_cloudProvider
prompt: "Enter the cloud provider name where you need Astra database to be created? Examples: AWS/AZURE/GCP"
- name: astra_db_tier
prompt: "Enter your Astra database tier? Examples: serverless/a5/a10/a20/a40/c10/c20/c40/d10/d20/d40"
- name: astra_db_capacityUnits
prompt: "Enter your Astra database capacity units?"
- name: astra_db_region
prompt: "Enter your Astra database region?"
- name: astra_db_username
prompt: "Enter your Astra database username?"
#- name: astra_db_password
# prompt: "Enter your Astra database password?"
# private: yes
- name: astra_db_tableName
prompt: "Enter your Astra database table name?"
tasks:
- name: Print the authentication token for debugging
debug:
var: astra_devops_api_auth_token
when:
- astra_devops_api_auth_token is defined
- name: Returns supported regions and availability for a given user and org combination
uri:
url: "https://api.astra.datastax.com/v2/availableRegions"
method: GET
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
timeout: 30
status_code: 200
register: supported_regions_for_user_org
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- name: Print the supported region and availability for a given user and org combination response for debugging
debug:
var: supported_regions_for_user_org
when:
- supported_regions_for_user_org is defined
- name: Create a new Astra database
uri:
url: https://api.astra.datastax.com/v2/databases
method: POST
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
body: >
{
"name": "{{ astra_db_name | default('astra') }}",
"keyspace": "{{ astra_db_keyspace | default('astra') }}",
"cloudProvider": "{{ astra_db_cloudProvider | default('GCP') }}",
"tier": "{{ astra_db_tier | default('serverless') }}",
"capacityUnits": {{ astra_db_capacityUnits | default(1) | int }},
"region": "{{ astra_db_region | default('us-east1') }}",
"user": "{{ astra_db_username | default('astra') }}",
"password": "{{ astra_db_password | default('Astra123') }}"
}
body_format: json
timeout: 60
status_code: 201
register: create_astra_db_response
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- name: Print the create Astra database response for debugging
debug:
var: create_astra_db_response.location
when:
- create_astra_db_response is defined
- name: Find the created Astra database using its ID
uri:
url: "https://api.astra.datastax.com/v2/databases/{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}"
method: GET
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
timeout: 30
status_code: 200
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
register: find_astra_db_by_id_response
until: (find_astra_db_by_id_response.json.status == 'ACTIVE')
retries: 100
delay: 30
- name: Print the Find the created Astra database using its ID response for debugging
debug:
var: find_astra_db_by_id_response
when:
- find_astra_db_by_id_response is defined
- name: Obtain the secure connect bundle for the created Astra database using its ID
uri:
url: "https://api.astra.datastax.com/v2/databases/{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}/secureBundleURL"
method: POST
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
timeout: 30
status_code: 200
register: secure_connect_bundle_url
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- name: Print the obtain the secure connect bundle for the created Astra database response for debugging
debug:
var: secure_connect_bundle_url
when:
- secure_connect_bundle_url is defined
- name: Return all keyspaces in the Astra database using its ID
uri:
url: "https://{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}-{{ astra_db_region }}.apps.astra.datastax.com/api/rest/v1/keyspaces"
method: GET
headers:
Accept: "application/json"
Content-Type: "application/json"
X-Cassandra-Token: "{{ astra_devops_api_auth_token }}"
timeout: 30
status_code: 200
register: return_all_keyspaces
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- name: Print the return all keyspaces in Astra database response for debugging
debug:
var: return_all_keyspaces
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- return_all_keyspaces is defined
- name: Add a table in the created Astra database
uri:
url: "https://{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}-{{ astra_db_region }}.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/{{ astra_db_keyspace }}/tables"
method: POST
headers:
Accept: "application/json"
Content-Type: "application/json"
X-Cassandra-Token: "{{ astra_devops_api_auth_token }}"
body: >
{"name": "{{ astra_db_tableName }}","ifNotExists": true,"columnDefinitions":[
{"static":false,"name":"pk1","typeDefinition":"int"},
{"static":false,"name":"pk2","typeDefinition":"text"},
{"static":false,"name":"ck1","typeDefinition":"double"},
{"static":false,"name":"ck2","typeDefinition":"date"},
{"static":false,"name":"c1","typeDefinition":"timeuuid"}],"primaryKey":{"partitionKey":["pk1","pk2"],"clusteringKey":["ck1","ck2"]},"tableOptions":{"defaultTimeToLive":0,"clusteringExpression":[{"column":"ck1","order":"ASC"},{"column":"ck2","order":"DESC"}]}}
body_format: json
timeout: 30
status_code: 201
register: create_table_response
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- name: Print the created Astra database table name response for debugging
debug:
var: create_table_response
when:
- create_table_response is defined
- name: Return all tables in the Astra database using its ID
uri:
url: "https://{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}-{{ astra_db_region }}.apps.astra.datastax.com/api/rest/v2/schemas/keyspaces/{{ astra_db_keyspace }}/tables"
method: GET
headers:
Accept: "application/json"
Content-Type: "application/json"
X-Cassandra-Token: "{{ astra_devops_api_auth_token }}"
timeout: 30
status_code: 200
register: return_all_tables
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- create_table_response is defined
- name: Print the return all tables response for debugging
debug:
var: return_all_tables
when:
- return_all_tables is defined
- name: Resize the already created Astra database
uri:
url: "https://api.astra.datastax.com/v2/databases/{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}/resize"
method: POST
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
body: >
{
"capacityUnits": {{ astra_db_capacityUnits | default(3) | int }}
}
body_format: json
timeout: 30
status_code: 202
register: resize_astra_db
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- astra_db_tier != 'serverless'
- astra_db_tier != 'developer'
- name: Print resize the already created Astra database response for debugging
debug:
var: resize_astra_db
when:
- resize_astra_db is defined
- astra_db_tier is defined
- name: Park the already created Astra database
uri:
url: "https://api.astra.datastax.com/v2/databases/{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}/park"
method: POST
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
timeout: 600
status_code: 202
register: park_astra_db
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- astra_db_tier != 'serverless'
- astra_db_tier != 'developer'
- name: Print park the already created Astra database response for debugging
debug:
var: park_astra_db
when:
- park_astra_db is defined
- name: Find the parked Astra database using its ID
uri:
url: "https://api.astra.datastax.com/v2/databases/{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}"
method: GET
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
timeout: 30
status_code: 200
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- astra_db_tier != 'serverless'
- astra_db_tier != 'developer'
register: find_parked_astra_db_by_id_response
until: (find_parked_astra_db_by_id_response.json.status == 'PARKED')
retries: 100
delay: 30
- name: Print the find the parked Astra database using its ID response for debugging
debug:
var: find_parked_astra_db_by_id_response
when:
- find_parked_astra_db_by_id_response is defined
- name: Unpark the already created Astra database
uri:
url: "https://api.astra.datastax.com/v2/databases/{{ create_astra_db_response.location | regex_search('[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}') }}/unpark"
method: POST
headers:
Accept: "application/json"
Content-Type: "application/json"
Authorization: "Bearer {{ astra_devops_api_auth_token }}"
timeout: 600
status_code: 202
register: unpark_astra_db
when:
- astra_devops_api_auth_token is defined
- astra_devops_api_auth_token | length > 0
- create_astra_db_response.location is defined
- create_astra_db_response.location | length > 0
- astra_db_tier != 'serverless'
- astra_db_tier != 'developer'
- name: Print park the already created Astra database response for debugging
debug:
var: unpark_astra_db
when:
- unpark_astra_db is defined