-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_rest_functions.sh
2093 lines (1858 loc) · 55.2 KB
/
basic_rest_functions.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/bash
## @file
## @brief A set of functions to talk to the TF Config REST server
## @author Matvey Kraposhin
if [[ -v BASIC_REST_FUNCTIONS ]]
then
echo "The file basic_rest_functions.sh has been already sourced"
echo "Can not proceed"
exit 0
fi
#
# Constants and a configuration
#
## @brief Max value of $RANDOM function
declare RANDOM_MAX=32768
## @brief Ordered list of digits for hexadecimal numerical system
declare -a HEX_DIGITS=("0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F")
## @brief Ordered list of capital latin letters
declare -a LAT_LETTERS=(A B C D E F G H I J K L M N O P Q R S T U V U W X Y Z)
## @brief Powers of 2
declare -a POWERS_OF_2=("1" "2" "4" "8" "16" "32" "64" "128" "256")
## @brief Contains name of log file
declare MESG_LOG=log
if [[ ! -v CONTROLLER_SETTINGS ]]
then
echo "Settings for controller (controller_settings.sh) were not found"
echo "Can not proceed"
exit 0
fi
REST_ADDRESS="http://$CONTROLLER_HOST:$CONTROLLER_PORT"
RES_FOLDER="/tmp/$USER-run-results"
RES_PREFIX="test_result_"
## @fn random_hex_digit()
## @brief Returns a random hex digit
function random_hex_digit(){
local digit_pos=`expr \( $RANDOM \) / 2048`
echo ${HEX_DIGITS[$digit_pos]}
}
## @fn random_dec_digit()
## @brief Returns a random decimal digit
function random_dec_digit(){
local digit_pos=`expr \( $RANDOM \) / 2048`
while [ $digit_pos -gt 9 ]
do
digit_pos=`expr \( $RANDOM \) / 2048`
done
echo ${HEX_DIGITS[$digit_pos]}
}
## @fn random_dec_digit_max()
## @brief Returns a random decimal digit in the range [0,max)
function random_dec_digit_max(){
local max=$1
local digit_pos=`expr \( $RANDOM \) / 2048`
while [ $digit_pos -ge $max ]
do
digit_pos=`expr \( $RANDOM \) / 2048`
done
echo ${HEX_DIGITS[$digit_pos]}
}
## @fn random_letter()
## @brief Returns a random capital latin letter
function random_letter(){
local letter_pos=`expr \( $RANDOM \) / 1260`
echo ${LAT_LETTERS[$letter_pos]}
}
## @fn random_string()
## @brief Returns a string with the given length of random letters
function random_string(){
local string_len=$1
if [ "$string_len" = "" ]
then
echo ""
return 1
fi
local i=0
str=""
while [ $i -lt $string_len ]
do
str="$str"`random_letter`
i=`expr $i + 1`
done
echo "$str"
}
## @fn execute_rest_request()
## @brief Executes REST request
function execute_rest_request(){
local req_type="$1"
local req_str="$2"
local req_url="$3"
if [ "$req_str" = "" ]
then
curl -s -X "$req_type"\
-H "X-Auth-Token: $OS_TOKEN"\
-H "Content-Type: application/json; charset=UTF-8"\
"$req_url"
else
curl -s -X "$req_type"\
-H "X-Auth-Token: $OS_TOKEN"\
-H "Content-Type: application/json; charset=UTF-8"\
--data " $req_str " "$req_url"
fi
}
## @fn execute_put_request()
## @brief Executes PUT REST request
function execute_put_request(){
local req_str="$1"
local req_url="$2"
execute_rest_request "PUT" "$req_str" "$req_url"
}
## @fn execute_post_request()
## @brief Executes POST REST request (creation of a new object)
function execute_post_request(){
local req_str="$1"
local req_url="$2"
execute_rest_request "POST" "$req_str" "$req_url"
}
## @fn execute_get_request()
## @brief Executes GET REST request
function execute_get_request(){
local req_url="$1"
execute_rest_request "GET" "" "$req_url"
}
## @fn execute_delete_request()
## @brief Executes DELETE REST request
function execute_delete_request(){
local req_url=$1
execute_rest_request "DELETE" "" "$req_url"
}
## @fn name_to_fqname()
## @brief Returns fqname for a given network name
function name_to_fqname(){
local fqname="[\"default-domain\", \"admin\", \"$1\"]"
echo "$fqname"
}
## @fn prefix_to_ip()
## @brief Returns IP part of a prefix
function prefix_to_ip(){
local prefix_str=$1
local slash_pos=`expr index "$prefix_str" /`
slash_pos=`expr $slash_pos - 1`
echo ${prefix_str:0:$slash_pos}
}
## @fn is_ipv6_address()
## @brief Determines whether a given address belongs to IPv6 range
function is_ipv6_address(){
grep -q ":" <<< "$1"
if [ $? -eq 0 ]
then
echo "yes"
return 0
fi
echo "no"
return 1
}
## @fn is_ipv4_address()
## @brief Determines whether a given address belongs to IPv4 range
function is_ipv4_address(){
grep -q "." <<< "$1"
if [ $? -eq 0 ]
then
echo "yes"
return 0
fi
echo "no"
return 1
}
## @fn fqname_json_to_csv()
## @brief Returns fqname in the CSV format
function fqname_json_to_csv() {
local fqname="$1"
local csv_fqname=`echo "$fqname" | sed 's/ //g'` # remove spaces
csv_fqname=`echo "$csv_fqname" | sed 's/"//g'` # remove \" characters
csv_fqname=`echo "$csv_fqname" | sed 's/,/:/g'` # replace "," w/ ":"
csv_fqname=${csv_fqname::-1} #remove last ]
csv_fqname=${csv_fqname:1} #remove first [
echo "$csv_fqname"
}
## @fn fqname_to_uuid()
## @brief Returns the UUID of an object with a given fqname
function fqname_to_uuid(){
local fqname="$1"
local type="$2"
local REQ_STR
read -r -d '' REQ_STR <<- REQ_MARKER
{
"fq_name": $fqname,
"type": "$type"
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/fqname-to-id"
local UUID_JSON=`execute_post_request "$REQ_STR" "$REQ_URL"`
grep -q "uuid" <<< "$UUID_JSON"
local has_uuid=$?
local UUID_STR=""
if [ $has_uuid -eq 0 ]
then
UUID_STR=`jq -c -r '.uuid' <<< "$UUID_JSON"`
fi
echo $UUID_STR
}
## @fn set_config_field()
## @brief Takes JSON config and sets the given field in it to the given value
function set_config_field() {
local config="$1"
local field_name="$2"
local value="$3"
local fields=`jq -c -r 'keys[]' <<< "$config" `
local mod_config=
local t_value=
local has_nested=
for field in $fields
do
t_value=`jq -c -r ".$field" <<< "$config"`
if [ "$field" = "$field_name" ]
then
t_value="$value"
fi
! grep -q ']' <<< "$t_value" && ! grep -q '}' <<< "$t_value"
local has_nested=$?
if [ $has_nested -eq 0 ]
then
t_value=\"$t_value\"
fi
t_value="\"$field\":$t_value"
#echo "$t_value"
mod_config="$t_value"",""$mod_config"
done
mod_config=${mod_config::-1} #remove last ","
mod_config="{$mod_config}"
echo $mod_config
}
## @fn make_ipam_subnet_str()
## @brief Creates a JSON representation for subnet object
function make_ipam_subnet_str(){
local prefix=$1
local prefix_len=$2
iss="{\"subnet\": {\"ip_prefix\": \"$prefix\",\"ip_prefix_len\": $prefix_len},"
iss=$iss"\"dns_server_address\": \"$prefix""2\","
iss=$iss" \"enable_dhcp\": true, \"default_gateway\": \"$prefix""1\","
iss=$iss" \"addr_from_start\": true}"
echo "$iss"
}
## @fn ipv4_to_number()
## @brief converts an IPv4 address into the number
function ipv4_to_number() {
local ipv4_address=$1
local octets=
local oct=
local number=0
local n=1
local k=3
IFS='.'
read -ra octets <<< "$ipv4_address"
if [ ${#octets[@]} -ne 4 ]
then
echo "0"
return 1
fi
while [ $k -ge 0 ]
do
oct=${octets[$k]}
number=`expr $oct \* $n + $number`
n=`expr 256 \* $n`
k=`expr $k - 1`
done
echo "$number"
}
## @fn number_to_ipv4()
## @brief Converts a number to IPv4 address
function number_to_ipv4() {
local number=$1
local ipv4=
oct1n=1
oct2n="$((256))"
oct3n="$((256*256))"
oct4n="$((256*256*256))"
oct="$(($number / $oct4n))"
ipv4="$oct"
number="$(($number - $oct*$oct4n))"
oct="$(($number / $oct3n))"
ipv4="$ipv4"."$oct"
number="$(($number - $oct*$oct3n))"
oct="$(($number / $oct2n))"
ipv4="$ipv4"."$oct"
number="$(($number - $oct*$oct2n))"
oct="$(($number / $oct1n))"
ipv4="$ipv4"."$oct"
echo "$ipv4"
}
## @fn split_network_by24()
## @brief splits the given network by 24 mask subnets
function split_network_by24() {
local net_prefix=$1
local net_prefix_len=${net_prefix#*"/"}
local slash_pos=$(( ${#net_prefix} - ${#net_prefix_len} - 1 ))
local net_addr="${net_prefix:0:$slash_pos}"
local net_power="$((32 - $net_prefix_len))"
local net_number=`ipv4_to_number $net_addr`
local subnets=
local subnet24_prefix
if [ $net_prefix_len -ge 24 ]
then
echo "$net_prefix"
return 1
fi
if [ $net_number -eq 0 ]
then
echo "0.0.0.0/24"
return 1
fi
local host_number="$((2**$net_power))"
local next_net_number="$(($net_number + $host_number))"
local net24_number=$net_number
local networks24=
local net24_addr=
while [ $net24_number -lt $next_net_number ]
do
net24_addr=`number_to_ipv4 $net24_number`
if [ "$networks24" = "" ]
then
networks24="$net24_addr/24"
else
networks24="$networks24 $net24_addr/24"
fi
net24_number="$(($net24_number + 256))"
done
echo "$networks24"
}
## @fn make_random_ipv4_address()
## @brief Creates random IPv4 address
function make_random_ipv4_address(){
local subnet_prefix=
local c_digit=
local o_digit=
# octet 1
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
o_digit=$c_digit
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ] || [ "$o_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
subnet_prefix=$subnet_prefix"`random_dec_digit_max 6`"
subnet_prefix=$subnet_prefix"."
# octet 2
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
o_digit=$c_digit
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ] || [ "$o_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
subnet_prefix=$subnet_prefix"`random_dec_digit_max 6`"
subnet_prefix=$subnet_prefix"."
# octet 3
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
o_digit=$c_digit
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ] || [ "$o_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
subnet_prefix=$subnet_prefix"`random_dec_digit_max 6`"
subnet_prefix=$subnet_prefix"."
# octet 4
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
o_digit=$c_digit
c_digit=`random_dec_digit_max 3`
if [ "$c_digit" != "0" ] || [ "$o_digit" != "0" ]
then
subnet_prefix=$subnet_prefix"$c_digit"
fi
subnet_prefix=$subnet_prefix"`random_dec_digit_max 6`"
echo "$subnet_prefix"
}
## @fn make_random_ipv4_subnet_octet
## @brief Creates a random octet for subnet with the given length (maximum
## number of the most significant bits)
function make_random_ipv4_subnet_octet() {
local k=$1
if [ $k -lt 1 ] || [ $k -gt 8 ]
then
echo "0"
return 1
fi
local c=${POWERS_OF_2[$k]}
local r=`expr $RANDOM \* $c / $RANDOM_MAX`
local delta=`expr 256 / $c`
local addr=`expr $delta \* $r`
echo $addr
return 0
}
## @fn
## @brief
function make_random_ipv4_subnet() {
local prefix_len=$1
if [ $prefix_len -le 0 ] || [ $prefix_len -ge 32 ]
then
echo "0.0.0.0"
return 1
fi
local n_octets=4
local i_octet=0
local bits_left=$prefix_len
local bits_in_octet=
local subnet_str=
local octet_str=
while [ $i_octet -lt $n_octets ]
do
bits_in_octet=$bits_left
if [ $bits_in_octet -lt 0 ]
then
subnet_str=$subnet_str".0"
else
if [ $bits_in_octet -ge 8 ]
then
octet_str=`make_random_ipv4_subnet_octet 8`
else
octet_str=`make_random_ipv4_subnet_octet $bits_in_octet`
fi
if [ $i_octet -eq 0 ]
then
subnet_str=$octet_str
else
subnet_str=$subnet_str"."$octet_str
fi
fi
bits_left=`expr $bits_left - 8`
i_octet=`expr $i_octet + 1`
done
echo $subnet_str
}
## @fn make_random_ipam_subnet_prefix_ipv4()
## @brief Creates random IPv4 subnet prefix of a given length
function make_random_ipam_subnet_prefix_ipv4(){
local subnet_prefix=
subnet_prefix=$subnet_prefix"`random_dec_digit_max 3`"
subnet_prefix=$subnet_prefix"`random_dec_digit_max 6`"
subnet_prefix=$subnet_prefix"`random_dec_digit_max 6`"
subnet_prefix="$1""$subnet_prefix"
echo "$subnet_prefix"
}
## @fn make_random_ipam_subnet_prefix_ipv6()
## @brief Creates random IPv6 subnet prefix of of length 24 (3 digits)
function make_random_ipam_subnet_prefix_ipv6(){
subnet_prefix=$subnet_prefix"`random_hex_digit`"
subnet_prefix=$subnet_prefix"`random_hex_digit`"
subnet_prefix=$subnet_prefix"`random_hex_digit`"
subnet_prefix=$subnet_prefix"::"
echo "$subnet_prefix"
}
## @fn make_random_ipam_subnet_ipv6()
## @brief Creates JSON description of IPv6 subnet of length 16
function make_random_ipam_subnet_ipv6(){
local ipam_subnet_prefix=`make_random_ipam_subnet_prefix_ipv6`
local prefix_len=24
local ipam_subnet_str=`make_ipam_subnet_str $ipam_subnet_prefix $prefix_len`
echo "$ipam_subnet_str"
}
## @fn make_random_ipam_subnet_ipv4()
## @brief Creates random ipam subnet prefix. Input: prepending part (e.g. 10., 10.1., etc)
function make_random_ipam_subnet_ipv4(){
local prefix_prep=$1
local prefix=`make_random_ipam_subnet_prefix_ipv4 $prefix_prep`
local ipam_subnet_str=
local n_dots=`echo "$prefix_prep" | grep -o "\." | wc -l` #search for dots
if [ "$prefix_prep" = "" ] && [ $n_dots -eq 0 ]
then
prefix="$prefix"".0.0.0"
ipam_subnet_str=`make_ipam_subnet_str $prefix 8`
elif [ $n_dots -eq 1 ]
then
prefix="$prefix"".0.0"
ipam_subnet_str=`make_ipam_subnet_str $prefix 16`
elif [ $n_dots -eq 2 ]
then
prefix="$prefix"".0"
ipam_subnet_str=`make_ipam_subnet_str $prefix 24`
else
ipam_subnet_str=""
fi
echo $ipam_subnet_str
}
## @fn make_ipam_subnet()
## @brief Creates the JSON description for a subnet with the given prefix length 16
function make_ipam_subnet(){
local ipam_subnet_prefix=$1
local prefix_len=16
local ipam_subnet_str=`make_ipam_subnet_str $ipam_subnet_prefix $prefix_len`
echo "$ipam_subnet_str"
}
## @fn make_random_ipam_subnets_ipv6()
## @brief Creates several ipv6 subnets
function make_random_ipam_subnets_ipv6(){
local n_subnets=$1
local i=0
local subnets_str=""
while [ $i -lt $n_subnets ]
do
if [ $i -gt 0 ]
then
subnets_str=$subnets_str", "`make_random_ipam_subnet_ipv6`
else
subnets_str=`make_random_ipam_subnet_ipv6`
fi
i=`expr $i + 1`
done
echo "$subnets_str"
}
## @fn make_random_ipam_subnets_ipv4()
## @brief Creates several ipv4 subnets
## With a given constant prepended network part
## Input: number of subnets, prepended part
function make_random_ipam_subnets_ipv4(){
local n_subnets=$1
local prepend_part=$2
local i=0
local subnets_str=""
while [ $i -lt $n_subnets ]
do
if [ $i -gt 0 ]
then
subnets_str=$subnets_str", "`make_random_ipam_subnet_ipv4\
$prepend_part`
else
subnets_str=`make_random_ipam_subnet_ipv4 $prepend_part`
fi
i=`expr $i + 1`
done
echo "$subnets_str"
}
## @fn make_ip_instance_name()
## @brief Makes a name for an ip intance
## INPUT: the prefix (string) and optionally, length of the random part
## in a new name
function make_ip_instance_name(){
local prefix="$1"
local random_len="$2"
local stop="no"
local ipi_name=""
local fq_name="\"[$ipi_name]\""
local obj_uuid=""
local i=0;
while [ "$stop" != "yes" ]
do
if [ "$random_len" != "" ]
then
ipi_name="$prefix""_"`random_string $random_len`
else
ipi_name="$prefix""_$i"
fi
fq_name="[\"$ipi_name\"]"
obj_uuid=`fqname_to_uuid "$fq_name" "instance-ip"`
if [ "$obj_uuid" = "" ]
then
stop="yes"
fi
i=`expr $i + 1`
done
echo $ipi_name
}
## @fn add_reference()
## @brief Creates a reference between two objects
function add_reference() {
local from_uuid="$1"
local from_type="$2"
local to_uuid="$3"
local to_type="$4"
local REQ_STR=
read -r -d '' REQ_STR <<- REQ_MARKER
{
"operation": "ADD",
"uuid": "$from_uuid",
"type": "$from_type",
"ref-uuid": "$to_uuid",
"ref-type": "$to_type",
"attr": {"sequence": {"major": 0, "minor": 0}}
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/ref-update"
echo >> $MESG_LOG
execute_post_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn del_reference()
## @brief Removes the reference between two objects
function del_reference() {
local from_uuid="$1"
local from_type="$2"
local to_uuid="$3"
local to_type="$4"
local REQ_STR=
read -r -d '' REQ_STR <<- REQ_MARKER
{
"operation": "DELETE",
"uuid": "$from_uuid",
"type": "$from_type",
"ref-uuid": "$to_uuid",
"ref-type": "$to_type",
"attr": {"sequence": {"major": 0, "minor": 0}}
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/ref-update"
echo >> $MESG_LOG
execute_post_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn delete_entity()
## @brief Deletes an entity (object)
## INPUT: an uuid of an entity and type of an entity
function delete_entity(){
local entity_uuid="$1"
local type="$2"
local REQ_URL=
local REQ_STR=$REST_ADDRESS/"$type"/"$entity_uuid"
execute_delete_request $REQ_STR
}
## @fn network_ipam_subnets()
## @brief Extracts UUIDS of IPAM subnets of a given virtual network
## INPUT: fqname of a virtual network
function network_ipam_subnets(){
local nw_name=$1
local nw_fqname=`name_to_fqname $nw_name`
local nw_uuid=`fqname_to_uuid "$nw_fqname" virtual-network`
local REQ_URL="$REST_ADDRESS/virtual-network/$nw_uuid"
if [ "$nw_uuid" = "" ]
then
echo "network_ipam_subnets: Network $nw_fqname not found" >> $MESG_LOG
echo ""
return 1
fi
local CURL_RES=`execute_get_request $REQ_URL`
local VNW_DICT=`jq -c -r '.["virtual-network"]' <<< "$CURL_RES"`
local IPAM_REFS_DICT=`jq -c -r '.["network_ipam_refs"]' <<< "$VNW_DICT"`
local IPAMS_DICT=`jq -c -r '.[0].attr.ipam_subnets' <<< "$IPAM_REFS_DICT"`
local N_SUBNETS=`jq -c -r '. | length' <<< "$IPAMS_DICT"`
if [ "$N_SUBNETS" = "" ]
then
N_SUBNETS=0
fi
local i_subnet=0
local IPAM_UUIDS=""
while [ $i_subnet -lt $N_SUBNETS ]
do
subnet=`jq -c -r ".[$i_subnet].subnet.ip_prefix" <<< "$IPAMS_DICT"`
subnet_uuid=`jq -c -r ".[$i_subnet].subnet_uuid" <<< "$IPAMS_DICT"`
IPAM_UUIDS=$IPAM_UUIDS" $subnet_uuid"
i_subnet=`expr $i_subnet + 1`
done
echo $IPAM_UUIDS
}
## @fn network_set_ipams()
## @brief Sets an IPAM for the network
function network_set_ipams() {
local nw_name="$1"
local new_ipam_prefixes="$2"
local ipam_name="$3"
local ipam_fqname=`name_to_fqname "$ipam_name"`
local ipam_uuid=`fqname_to_uuid "$ipam_fqname" "network-ipam"`
if [ "$ipam_uuid" = "" ]
then
echo "switching to default ipam from: $ipam_fqname"
ipam_fqname="[\"default-domain\", \"default-project\", \"default-network-ipam\"]"
fi
local nw_fqname=`name_to_fqname "$nw_name"`
local nw_uuid=`fqname_to_uuid "$nw_fqname" virtual-network`
if [ "$nw_uuid" = "" ]
then
echo "network_set_ipams: Cant find $nw_fqname"
return 1
fi
local ipam_subnets=""
for subnet in $new_ipam_prefixes
do
local subnet_prefix_len=${subnet#*"/"}
local slash_pos=$(( ${#subnet} - ${#subnet_prefix_len} - 1 ))
local subnet_prefix="${subnet:0:$slash_pos}"
ipam_subnets="$ipam_subnets "`make_ipam_subnet_str $subnet_prefix $subnet_prefix_len`","
done
ipam_subnets=${ipam_subnets::-1} #remove last ","
read -r -d '' REQ_STR <<- REQ_MARKER
{
"virtual-network":
{
"network_ipam_refs":
[{
"to": $ipam_fqname,
"attr" : {"ipam_subnets":[$ipam_subnets]}
}]
}
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/virtual-network/$nw_uuid"
REQ_STR=`echo $REQ_STR`
echo >> $MESG_LOG
execute_put_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn floating_ip_set_address()
## @brief Sets a new IP address for the given FIP
function floating_ip_set_address() {
local iip_fip="$1"
local fip_address="$2"
local fip_name=${iip_fip#*","}
local fip_pos=$(( ${#iip_fip} - ${#fip_name} - 1 ))
local iip_name="${iip_fip:0:$fip_pos}"
local fip_fqname="[\"$iip_name\",\"$fip_name\"]"
local fip_uuid=`fqname_to_uuid "$fip_fqname" "floating-ip"`
if [ "$fip_uuid" = "" ]
then
echo "floating_ip_set_address: Cant find $fip_fqname"
return 1
fi
read -r -d '' REQ_STR <<- REQ_MARKER
{
"floating-ip":
{
"floating_ip_address": "$fip_address"
}
}
REQ_MARKER
echo $REQ_STR
local REQ_URL="$REST_ADDRESS/floating-ip/$fip_uuid"
REQ_STR=`echo $REQ_STR`
echo >> $MESG_LOG
execute_put_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn create_network()
## @brief Creates a new network
## INPUT: name of network, number of subnets per
## network, version of the IP protocol
function create_network(){
local nw_name=$1
local n_ipam_subnets=$2
local ip_version=$3
local ipam_subnets=
grep -q "\." <<< "$2"
local has_v4_subnets=$?
grep -q ":" <<< "$2"
local has_v6_subnets=$?
if [ $has_v4_subnets -eq 0 ] || [ $has_v6_subnets -eq 0 ]
then
for subnet in $2
do
local subnet_prefix_len=${subnet#*"/"}
local slash_pos=$(( ${#subnet} - ${#subnet_prefix_len} - 1 ))
local subnet_prefix="${subnet:0:$slash_pos}"
ipam_subnets="$ipam_subnets "`make_ipam_subnet_str $subnet_prefix $subnet_prefix_len`","
done
ipam_subnets=${ipam_subnets::-1} #remove last ","
else
if [ "$ip_version" = "ipv4" ]
then
ipam_subnets=`make_random_ipam_subnets_ipv4 $n_ipam_subnets "10.0."`
fi
if [ "$ip_version" = "ipv6" ]
then
ipam_subnets=`make_random_ipam_subnets_ipv6 $n_ipam_subnets`
fi
fi
local vn_fqname=`name_to_fqname $nw_name`
read -r -d '' REQ_STR <<- REQ_MARKER
{
"virtual-network":
{
"parent_type": "project",
"fq_name": $vn_fqname,
"network_ipam_refs":
[{
"to": ["default-domain", "default-project", "default-network-ipam"],
"attr" : {"ipam_subnets":[$ipam_subnets]}
}]
}
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/virtual-networks"
REQ_STR=`echo $REQ_STR`
echo >> $MESG_LOG
execute_post_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn create_instance_ip()
## @brief Creates an IP Instance
## Input: name, virtual-network fqname, subnet_uuid, ip address(optionally)
function create_instance_ip(){
local REQ_STR
local iip_name="$1"
local nw_name="$2"
local subnet_uuid="$3"
local ip_addr="$4"
if [ "$iip_name" = "" ] || [ "$nw_name" = "" ] || [ "$subnet_uuid" = "" ]
then
echo "create_instance_ip error: iip_name=$iip_name, nw_name=$nw_name, subnet_uuid=$subnet_uuid" >> $MESG_LOG
return 1;
fi
local ip_instance_addr=""
local nw_fqname=`name_to_fqname "$nw_name"`
if [ "$ip_addr" != "" ]
then
ip_instance_addr=", \"instance_ip_address\": \"$ip_addr\""
fi
read -r -d '' REQ_STR <<- REQ_MARKER
{
"instance-ip": {
"parent_type": "config-root",
"fq_name": ["$iip_name"],
"virtual_network_refs" : [{"to" : $nw_fqname}],
"subnet_uuid" : "$subnet_uuid",
"instance_ip_mode": "active-active"
$ip_instance_addr
}
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/instance-ips"
echo >> $MESG_LOG
execute_post_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn create_instance_ip_ifnp()
## @brief Creates a new instance ip if an object with the given name
## is not found
function create_instance_ip_ifnp() {
local ipi_name="$1"
local nw_name="$2"
local subnet_uuid="$3"
local ip_addr="$4"
local t_ipi_fqname="[\"$ipi_name\"]"
local t_ipi_uuid=`fqname_to_uuid "$t_ipi_fqname" "instance-ip"`
if [ "$t_ipi_uuid" = "" ]
then
create_instance_ip "$ipi_name" "$nw_name" "$subnet_uuid" "$ip_addr"
fi
}
## @fn create_floating_ip()
## @brief Creates a floating ip object
## @input: fip name, parent ipi name, fip ip address, fip direction
function create_floating_ip(){
local REQ_STR
local fip_name=$1
local ipi_name=$2
local fip_addr=$3
local fip_dir=$4 #optional
if ["$fip_dir" = ""]
then
fip_dir="both"
fi
local fip_fqname="[\"$ipi_name\",\"$fip_name\"]"
local ipi_fqname="[\"$ipi_name\"]"
ipi_uuid=`fqname_to_uuid "$ipi_fqname" "instance-ip"`
if [ "$ipi_uuid" = "" ]
then
return 1
fi
read -r -d '' REQ_STR <<- REQ_MARKER
{
"floating-ip": {
"parent_type": "instance-ip",
"parent_uuid" : "$ipi_uuid",
"fq_name": $fip_fqname,
"project_refs": [{"to": ["default-domain","admin"]}],
"floating_ip_address": "$fip_addr",
"floating_ip_traffic_direction": "$fip_dir"
}
}
REQ_MARKER
local REQ_URL="$REST_ADDRESS/floating-ips"
echo >> $MESG_LOG
execute_post_request "$REQ_STR" "$REQ_URL" >> $MESG_LOG
}
## @fn create_intf_route_table()
## @brief Creates new interface routes table
function create_intf_route_table() {
local REQ_STR
local irt_name=$1
local irt_cidr=$2
# local fip_name=$1
# local ipi_name=$2
local irt_fqname=`name_to_fqname "$irt_name"` #