-
Notifications
You must be signed in to change notification settings - Fork 2
/
service-clone.sh
executable file
·667 lines (566 loc) · 23.7 KB
/
service-clone.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#!/bin/sh
set -o errexit
set -o pipefail
readonly VERSION='0.0.2'
readonly API_BASE_URI='https://api.fastly.com'
if [[ -z "${FASTLY_API_TOKEN}" ]]; then
echo "Please set your API token first."
echo "export FASTLY_API_TOKEN=\"your_api_token\""
exit 1
fi
readonly API_TOKEN=${FASTLY_API_TOKEN}
usage () {
echo "Usage: ./service-clone.sh [OPTIONS]"
echo ""
echo "List of available options"
echo " -s, --src SERVICE_ID [required] source service ID"
echo " -d, --dst SERVICE_ID [required] destinatuon service ID"
echo " -v, --version VERSION (optional) source version number (default: the current active version)"
echo " --no-logging (optional) exclude logging settings (default: include)"
echo " --no-acl (optional) exclude acl (default: include)"
echo " --no-dictionary (optional) exclude dictionry (default: include)"
echo " -h, --help show help"
echo ""
echo "Need more help? Visit: https://github.com/smaeda-ks/fastly-service-clone"
}
case "$(uname -s)" in
Darwin*)
getopt=/usr/local/opt/gnu-getopt/bin/getopt
;;
Linux|*)
[ -x /bin/getopt ] && getopt=/bin/getopt || getopt=/usr/bin/getopt
;;
esac
arg_count=$#
options=$(${getopt} -n service-clone.sh -o s:d:v:h -l src:,dst:,version:,help,no-logging,no-acl,no-dictionary -- "$@")
eval set -- "${options}"
src_service=''
dst_service=''
version=''
dst_version=''
no_logging="false"
no_acl="false"
no_dictionary="false"
while true; do
case "$1" in
-s|--src) shift; src_service=$1 ;;
-d|--dst) shift; dst_service=$1 ;;
-v|--version) shift; version=$1 ;;
-h|--help) usage; exit ;;
--no-logging) no_logging="true" ;;
--no-acl) no_acl="true" ;;
--no-dictionary) no_dictionary="true" ;;
--) shift; break ;;
esac
shift
done
options_check () {
if [[ ${arg_count} -eq 0 ]]; then
usage
exit 0
fi
if [[ -z "${src_service}" ]]; then
echo "please specify a source service ID"
exit 1
fi
if [[ -z "${dst_service}" ]]; then
echo "please specify a destination service ID"
exit 1
fi
if [[ ! -x "$(command -v jq)" ]]; then
echo "please install jq command"
exit 1
fi
if [[ ! -x "$(command -v curl)" ]]; then
echo "please install curl command"
exit 1
fi
}
get_src_version () {
if [[ -z "${version}" ]]; then
echo "[INFO] checking the current active version number for service ID: ${src_service} ..."
version=$(curl -sS --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${src_service}/version/active" | jq '.number')
fi
echo "[INFO] source version is: ${version}"
}
global_dst_active_version=''
get_dst_active_version () {
global_dst_active_version=$(curl -sS -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/active" | jq '.number')
}
check_dst_service () {
echo "[INFO] checking destination service ID: ${dst_service} ..."
local raw
raw=$(curl -sS --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}")
}
check_src_service () {
echo "[INFO] checking source service ID: ${src_service} ..."
local raw
raw=$(curl -sS --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${src_service}")
local type=$(jq -r ".type" <<< ${raw})
if [[ "${type}" != "vcl" ]]; then
echo "[ERROR] wasm service is not supported ..."
exit 1
fi
}
global_get_objects=''
get_objects () {
local readonly path=$1
global_get_objects=$(curl -sS --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}${path}")
}
raw_conditions=''
get_conditions () {
echo "[INFO] fetching condtions from the source service ..."
get_objects "/service/${src_service}/version/${version}/condition"
raw_conditions=${global_get_objects}
}
set_conditions () {
echo "[INFO] copying condtions to the destination service ..."
local count=$(jq 'length' <<< ${raw_conditions})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_conditions})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/condition" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_healthchecks=''
get_healthchecks () {
echo "[INFO] fetching healthchecks from the source service ..."
get_objects "/service/${src_service}/version/${version}/healthcheck"
raw_healthchecks=${global_get_objects}
}
set_healthchecks () {
echo "[INFO] copying healthchecks to the destination service ..."
local count=$(jq 'length' <<< ${raw_healthchecks})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_healthchecks})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/healthcheck" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_cache_settings=''
get_cache_settings () {
echo "[INFO] fetching cache_settings from the source service ..."
get_objects "/service/${src_service}/version/${version}/cache_settings"
raw_cache_settings=${global_get_objects}
}
set_cache_settings () {
echo "[INFO] copying cache_settings to the destination service ..."
local count=$(jq 'length' <<< ${raw_cache_settings})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_cache_settings})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/cache_settings" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_backends=''
get_backends () {
echo "[INFO] fetching backends from the source service ..."
get_objects "/service/${src_service}/version/${version}/backend"
raw_backends=${global_get_objects}
}
set_backends () {
echo "[INFO] copying backends to the destination service ..."
local count=$(jq 'length' <<< ${raw_backends})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_backends})
filter_objects "${object}" backend
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/backend" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_directors=''
get_directors () {
echo "[INFO] fetching directors from the source service ..."
get_objects "/service/${src_service}/version/${version}/director"
raw_directors=${global_get_objects}
}
set_directors () {
echo "[INFO] copying directors to the destination service ..."
local count=$(jq 'length' <<< ${raw_directors})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_directors})
filter_objects "${object}" director
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/director" -H "Content-Type: application/json" -d "${post_body}"
# establishes a relationship between a backend and a director
local director_name=$(jq -r ".[${i}] | .name" <<< ${raw_directors})
director_name=$(urlencode "${director_name}")
local backends=$(jq ".[${i}] | .backends" <<< ${raw_directors})
local backends_count=$(jq 'length' <<< ${backends})
for (( j=0; j<${backends_count}; j++ ))
do
local backend_name=$(jq -r ".[${j}]" <<< ${backends})
backend_name=$(urlencode "${backend_name}")
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/director/${director_name}/backend/${backend_name}"
done
done
}
raw_gzips=''
get_gzips () {
echo "[INFO] fetching gzips from the source service ..."
get_objects "/service/${src_service}/version/${version}/gzip"
raw_gzips=${global_get_objects}
}
set_gzips () {
echo "[INFO] copying gzips to the destination service ..."
local count=$(jq 'length' <<< ${raw_gzips})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_gzips})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/gzip" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_headers=''
get_headers () {
echo "[INFO] fetching headers from the source service ..."
get_objects "/service/${src_service}/version/${version}/header"
raw_headers=${global_get_objects}
}
set_headers () {
echo "[INFO] copying headers to the destination service ..."
local count=$(jq 'length' <<< ${raw_headers} )
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_headers} )
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/header" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_request_settings=''
get_request_settings () {
echo "[INFO] fetching request_settings from the source service ..."
get_objects "/service/${src_service}/version/${version}/request_settings"
raw_request_settings=${global_get_objects}
}
set_request_settings () {
echo "[INFO] copying request_settings to the destination service ..."
local count=$(jq 'length' <<< ${raw_request_settings})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_request_settings})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/request_settings" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_response_objects=''
get_response_objects () {
echo "[INFO] fetching response_objects from the source service ..."
get_objects "/service/${src_service}/version/${version}/response_object"
raw_response_objects=${global_get_objects}
}
set_response_objects () {
echo "[INFO] copying response_objects to the destination service ..."
local count=$(jq 'length' <<< ${raw_response_objects})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_response_objects})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/response_object" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_settings=''
get_settings () {
echo "[INFO] fetching settings from the source service ..."
get_objects "/service/${src_service}/version/${version}/settings"
raw_settings=${global_get_objects}
}
set_settings () {
echo "[INFO] copying settings to the destination service ..."
local object=${raw_settings}
filter_objects "${object}" settings
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X PUT --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/settings" -H "Content-Type: application/json" -d "${post_body}"
}
raw_vcls=''
get_vcls () {
echo "[INFO] fetching vcls from the source service ..."
get_objects "/service/${src_service}/version/${version}/vcl"
raw_vcls=${global_get_objects}
}
set_vcls () {
echo "[INFO] copying vcls to the destination service ..."
local count=$(jq 'length' <<< ${raw_vcls})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_vcls})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/vcl" -H "Content-Type: application/json" -d "${post_body}"
done
# the "main-ness" of the vcl does not get carried over during copy, so explicitly setting main
if [[ count -ne 0 ]]; then
local main_vcl_name=$(jq -r '.[] | select(.main == true) | .name' <<< ${raw_vcls})
main_vcl_name=$(urlencode "${main_vcl_name}")
curl -sS -o /dev/null -X PUT --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/vcl/${main_vcl_name}/main"
fi
}
raw_snippets=''
get_snippets () {
echo "[INFO] fetching snippets from the source service ..."
get_objects "/service/${src_service}/version/${version}/snippet"
raw_snippets=${global_get_objects}
}
set_snippets () {
echo "[INFO] copying snippets to the destination service ..."
local count=$(jq 'length' <<< ${raw_snippets})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_snippets})
local dynamic=$(jq -r '.dynamic' <<< ${object})
if [[ dynamic -eq 1 ]]; then
local snippet_id=$(jq -r '.id' <<< ${object})
get_objects "/service/${src_service}/snippet/${snippet_id}"
local raw=${global_get_objects}
filter_objects "${raw}" dynamic_snippet
raw=${global_filter_objects}
object=$(jq <<< ${object} | jq ". |= .+ ${raw}")
fi
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/snippet" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_dictionaries=''
get_dictionaries () {
echo "[INFO] fetching dictionaries from the source service ..."
get_objects "/service/${src_service}/version/${version}/dictionary"
raw_dictionaries=${global_get_objects}
}
set_dictionaries () {
echo "[INFO] copying dictionaries to the destination service ..."
local count=$(jq 'length' <<< ${raw_dictionaries})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_dictionaries})
local write_only=$(jq -r '.write_only' <<< ${object})
filter_objects "${object}"
local post_body=${global_filter_objects}
local result=$(curl -sS -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/dictionary" -H "Content-Type: application/json" -d "${post_body}")
if [[ "${write_only}" == "true" ]]; then
local dict_name=$(jq -r '.name' <<< ${object})
echo "--> unable to retrive the contents of a write_only dictionary (name: \"${dict_name}\"), so creating an empty write_only dictionary instead ..."
continue
fi
local dictionary_id=$(jq -r '.id' <<< ${object})
get_objects "/service/${src_service}/dictionary/${dictionary_id}/items"
local raw_items=${global_get_objects}
local items_count=$(jq 'length' <<< ${raw_items})
if [[ items_count -eq 0 ]]; then
continue
fi
local json_items=''
for (( j=0; j<${items_count}; j++ ))
do
local object=$(jq ".[${j}]" <<< ${raw_items})
local item_key=$(jq '.item_key' <<< ${object})
local item_value=$(jq '.item_value' <<< ${object})
if [[ ${j} -ne 0 ]]; then
json_items="${json_items},"
fi
json_items="${json_items}{\"op\": \"create\", \"item_key\": ${item_key}, \"item_value\": ${item_value}}"
done
local dst_dictionary_id=$(jq -r '.id' <<< ${result})
post_body=$(jq <<< '{"items": []}' | jq ".items |= .+ [${json_items}]")
curl -sS -o /dev/null -X PATCH --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/dictionary/${dst_dictionary_id}/items" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_acls=''
get_acls () {
echo "[INFO] fetching acls from the source service ..."
get_objects "/service/${src_service}/version/${version}/acl"
raw_acls=${global_get_objects}
}
set_acls () {
echo "[INFO] copying acls to the destination service ..."
local count=$(jq 'length' <<< ${raw_acls})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_acls})
filter_objects "${object}"
local post_body=${global_filter_objects}
local result=$(curl -sS -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/acl" -H "Content-Type: application/json" -d "${post_body}")
local acl_id=$(jq -r '.id' <<< ${object})
get_objects "/service/${src_service}/acl/${acl_id}/entries"
local raw_entries=${global_get_objects}
local entries_count=$(jq 'length' <<< ${raw_entries})
if [[ entries_count -eq 0 ]]; then
continue
fi
local json_entries=''
for (( j=0; j<${entries_count}; j++ ))
do
local object=$(jq ".[${j}]" <<< ${raw_entries})
local ip=$(jq '.ip' <<< ${object})
local subnet=$(jq '.subnet' <<< ${object})
local negated=$(jq '.negated' <<< ${object})
local comment=$(jq '.comment' <<< ${object})
if [[ ${j} -ne 0 ]]; then
json_entries="${json_entries},"
fi
json_entries="${json_entries}{\"op\": \"create\", \"ip\": ${ip}, \"subnet\": ${subnet}, \"negated\": ${negated}, \"comment\": ${comment}}"
done
local dst_acl_id=$(jq -r '.id' <<< ${result})
post_body=$(jq <<< '{"entries": []}' | jq ".entries |= .+ [${json_entries}]")
curl -sS -o /dev/null --fail -X PATCH -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/acl/${dst_acl_id}/entries" -H "Content-Type: application/json" -d "${post_body}"
done
}
raw_loggigns=''
declare -a array_logging_types=("azureblob" "bigquery" "cloudfiles" "datadog" "digitalocean" "elasticsearch" "ftp" "pubsub" "gcs" "https" "heroku" "honeycomb" "kafka" "kinesis" "logshuttle" "logentries" "loggly" "newrelic" "openstack" "papertrail" "s3" "sftp" "scalyr" "splunk" "sumologic" "syslog")
copy_loggings () {
echo "[INFO] copying loggings from the source service ..."
local type
for type in ${array_logging_types[@]}
do
printf " type: %s ... " "${type}"
get_objects "/service/${src_service}/version/${version}/logging/${type}"
raw_loggigns=${global_get_objects}
local count=$(jq 'length' <<< ${raw_loggigns})
if [[ count -eq 0 ]]; then
printf "skip\n"
continue
else
printf "copying ...\n"
fi
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_loggigns})
filter_objects "${object}"
logging_compression_check_codec "${global_filter_objects}"
local post_body=${filter_logging_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/logging/${type}" -H "Content-Type: application/json" -d "${post_body}"
done
done
}
filter_logging_objects=''
logging_compression_check_codec() {
local readonly object=$1
local compression_codec=$(jq -r ".compression_codec" <<< ${object})
local gzip_level=$(jq -r ".gzip_level" <<< ${object})
if [[ "${compression_codec}" == "null" && "${gzip_level}" == "null" ]]; then
# for endpoints don't support compression
filter_logging_objects=${object}
elif [[ "${compression_codec}" != "null" && "${compression_codec}" != "" ]]; then
# follow the original compression_codec
filter_logging_objects=$(jq 'del(.gzip_level)' <<< ${object})
elif [[ "${gzip_level}" != "null" && "${gzip_level}" -gt "0" ]]; then
# follow the original gzip_level
filter_logging_objects=$(jq 'del(.compression_codec)' <<< ${object})
else
# the endpoint doesn't have any custom compression setting
# remove them as they were supplied by API originally
filter_logging_objects=$(jq 'del(.compression_codec, .gzip_level)' <<< ${object})
fi
}
raw_domains=''
get_domains () {
get_objects "/service/${dst_service}/version/${global_dst_active_version}/domain"
raw_domains=${global_get_objects}
}
set_domains () {
local count=$(jq 'length' <<< ${raw_domains})
for (( i=0; i<${count}; i++ ))
do
local object=$(jq ".[${i}]" <<< ${raw_domains})
filter_objects "${object}"
local post_body=${global_filter_objects}
curl -sS -o /dev/null -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version/${dst_version}/domain" -H "Content-Type: application/json" -d "${post_body}"
done
}
urlencode() {
old_lc_collate=$LC_COLLATE
LC_COLLATE=C
local length="${#1}"
for (( i=0; i<length; i++ )); do
local c="${1:$i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
LC_COLLATE=$old_lc_collate
}
create_new_version () {
echo "[INFO] creating a new version for service ID: ${dst_service} ..."
dst_version=$(curl -sS -X POST --fail -H "fastly-key: ${API_TOKEN}" "${API_BASE_URI}/service/${dst_service}/version" | jq '.number')
echo "[INFO] created a new version ${dst_version}"
}
global_filter_objects=''
filter_objects () {
local readonly object=$1
local readonly type=$2
local filtered
filtered=$(jq 'with_entries(select(.value != null and .value != "")) | del(.created_at, .updated_at, .deleted_at, .locked, .version, .id, .service_id)' <<< ${object})
case ${type} in
"backend") filtered=$(jq 'del(.ipv4, .ipv6, .hostname)' <<< ${filtered} ) ;;
"director") filtered=$(jq 'del(.backends)' <<< ${filtered} ) ;;
"settings") filtered=$(jq 'del(."general.default_pci")'<<< ${filtered} ) ;;
"dynamic_snippet") filtered=$(jq 'del(.snippet_id)'<<< ${filtered} ) ;;
esac
global_filter_objects=${filtered}
}
main () {
options_check
get_src_version
check_src_service
check_dst_service
create_new_version
get_conditions
set_conditions
get_healthchecks
set_healthchecks
get_cache_settings
set_cache_settings
get_backends
set_backends
get_directors
set_directors
get_gzips
set_gzips
get_headers
set_headers
get_request_settings
set_request_settings
get_response_objects
set_response_objects
get_settings
set_settings
get_vcls
set_vcls
get_snippets
set_snippets
if [[ "${no_dictionary}" == "false" ]]; then
get_dictionaries
set_dictionaries
fi
if [[ "${no_acl}" == "false" ]]; then
get_acls
set_acls
fi
if [[ "${no_logging}" == "false" ]]; then
copy_loggings
fi
# restore domains from the current active version (for destination service)
get_dst_active_version
if [[ "${global_dst_active_version}" != "null" ]]; then
echo "[INFO] restoring domains ..."
get_domains
set_domains
fi
echo "\nCompleted! Go visit the destination service configuration page and activate the new version:"
echo "https://manage.fastly.com/configure/services/${dst_service}/versions/${dst_version}/domains"
}
main
exit 0