forked from erjosito/azcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vwan-functions.sh
2431 lines (2295 loc) · 115 KB
/
vwan-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
############################################################################
# Created by Jose Moreno
# May 2020
#
# Contains functions to manage VWAN using the 2020-05-01 APIs for custom routing
# Support for up to 3 locations.
############################################################################
# Variables
# rg=vwantest # RG to be defined in the main function
# vwan_name=vwantest # vwan_name to be defined in the main function
location1=westcentralus
location2=westcentralus
location3=uksouth
password=Microsoft123! # Used as IPsec PSK too
publisher=cisco
offer=cisco-csr-1000v
sku=16_12-byol
version=$(az vm image list -p $publisher -f $offer -s $sku --all --query '[0].version' -o tsv)
nva_size=Standard_B2ms
vm_size=Standard_B1ms
logws_name=log$RANDOM
azfw_policy_name=vwan
#####################
# JSON snippets #
#####################
# REST Variables
# vwan_api_version=2020-05-01
# vwan_api_version=2021-03-01
vwan_api_version=2022-07-01
subscription_id=$(az account show --query id -o tsv)
# JSON
vwan_json='{location: $location, properties: {disableVpnEncryption: false, type: $sku}}'
vhub_json='{location: $location, properties: {virtualWan: {id: $vwan_id}, addressPrefix: $hub_prefix, sku: $sku}}'
vpnsitelink_json='{name: $link_name, properties: {ipAddress: $remote_pip, bgpProperties: {bgpPeeringAddress: $remote_bgp_ip, asn: $remote_asn}, linkProperties: {linkProviderName: "vendor1", linkSpeedInMbps: 100}}}'
vpnsite_json='{location: $location, properties: {virtualWan: {id: $vwan_id}, addressSpace: { addressPrefixes: [ $site_prefix ] }, isSecuritySite: $security, vpnSiteLinks: [ '${vpnsitelink_json}']}}'
cx_json='{name: $cx_name, properties: {connectionBandwidth: 200, vpnConnectionProtocolType: "IKEv2", enableBgp: true, sharedKey: $psk, vpnSiteLink: {id: $site_link_id}}}'
vpncx_json='{properties: {enableInternetSecurity: true, remoteVpnSite: {id: $site_id}, vpnLinkConnections: ['$cx_json']}}'
vpngw_json='{location: $location, properties: {virtualHub: {id: $vhub_id}, connections: [], isRoutingPreferenceInternet: true, bgpSettings: {asn: $asn, peerWeight: 0}}}'
vnet_cx_json='{properties: {remoteVirtualNetwork: {id: $vnet_id}, enableInternetSecurity: true}}'
rt_json='{properties: {routes: [], labels: []}}'
route_json='{name: $name, destinationType: "CIDR", destinations: [ $prefixes ], nextHopType: $type, nextHop: $nexthop }'
cxroute_json='{name: $name, addressPrefixes: [ $prefixes ], nextHopIpAddress: $nexthop }'
rtintent_json='{ properties: { routingPolicies: [ $routing_policies ] } }'
rtintent_policy_json='{name: $policy_name, destinations: [ $policy_destination ], nextHop: $policy_nexthop}'
###############
# Aliases #
###############
alias remote="ssh -n -o BatchMode=yes -o StrictHostKeyChecking=no"
####################
# Wait functions #
####################
wait_interval=5
function wait_until_finished {
resource_id=$1
resource_name=$(echo $resource_id | cut -d/ -f 9)
echo "Waiting for resource $resource_name to finish provisioning..."
start_time=`date +%s`
state=$(az resource show --id $resource_id --query properties.provisioningState -o tsv)
until [[ "$state" == "Succeeded" ]] || [[ "$state" == "Failed" ]] || [[ -z "$state" ]]
do
sleep $wait_interval
state=$(az resource show --id $resource_id --query properties.provisioningState -o tsv)
done
if [[ -z "$state" ]]
then
echo "Something really bad happened..."
else
run_time=$(expr `date +%s` - $start_time)
((minutes=${run_time}/60))
((seconds=${run_time}%60))
# echo "Resource $resource_name provisioning state is $state, wait time $minutes minutes and $seconds seconds"
fi
}
function wait_until_csr_finished {
branch_id=$1
wait_interval_csr=30 # longer wait interval, since this is quite verbose
echo "Waiting until CSR in branch${branch_id} is reachable..."
# Wait until getting an IP
branch_ip=$(az network public-ip show -n "branch${branch_id}-pip" -g $rg --query ipAddress -o tsv 2>/dev/null)
until [[ -n "$branch_ip" ]]
do
sleep $wait_interval_csr
branch_ip=$(az network public-ip show -n "branch${branch_id}-pip" -g $rg --query ipAddress -o tsv 2>/dev/null)
done
# Wait until getting SSH output
command="sho ver | i uptime"
command_output=$(remote $branch_ip "$command" 2>/dev/null)
until [[ -n "$command_output" ]]
do
sleep $wait_interval_csr
command_output=$(remote $branch_ip "$command")
done
echo "CSR is live, output to the command \"$command\" is $command_output"
}
function wait_until_hub_finished {
hub_name=$1
hub_id=$(az network vhub show -n $hub_name -g $rg --query id -o tsv)
wait_until_finished $hub_id
# Check state of connections
# echo "Hub state is $(get_vhub_state $hub_name), checking connections..."
connections=$(get_vnetcx_state $hub_name | grep Updating)
until [[ -z "$connections" ]]
do
sleep $wait_interval
connections=$(get_vnetcx_state $hub_name | grep Updating)
done
# Check state of route tables
# echo "No connections in Updating state in hub $hub_name, checking route tables..."
rts=$(get_rt_state $hub_name | grep Updating)
until [[ -z "$rts" ]]
do
sleep $wait_interval
rts=$(get_rt_state $hub_name | grep Updating)
done
}
function wait_until_gw_finished {
gw_name=$1
# It can be that we do not get a valid GW ID at the first try
echo "Finding out ID for VPN gateway $gw_name..."
gw_id=$(az network vpn-gateway show -n $gw_name -g $rg --query id -o tsv)
while [[ -z "$gw_id" ]]
do
sleep $wait_interval
gw_id=$(az network vpn-gateway show -n $gw_name -g $rg --query id -o tsv)
done
wait_until_finished $gw_id
}
# REST API: PUT routing intent
# https://learn.microsoft.com/en-us/rest/api/virtualwan/routing-intent/create-or-update?tabs=HTTP
# Parameters:
# - hub id (1 or 2)
# - policies (internet, private or both)
function put_routing_intent() {
# Get AzFW ID
hub_id=$1
azfw_id=$(az network vhub show -n hub${hub_id} -g $rg --query 'azureFirewall.id' -o tsv)
# Construct JSON for policies (Internet/Private)
rtintent_policy_internet_json_string=$(jq -n \
--arg policy_name "InternetTraffic" \
--arg policy_destination "Internet" \
--arg policy_nexthop "$azfw_id" \
"$rtintent_policy_json")
rtintent_policy_private_json_string=$(jq -n \
--arg policy_name "PrivateTrafficPolicy" \
--arg policy_destination "PrivateTraffic" \
--arg policy_nexthop "$azfw_id" \
"$rtintent_policy_json")
# Construct JSON for routing intent (depending on parameter)
policy=$2
if [[ "$policy" == 'internet' ]]; then
rtintent_json_string=$(jq -n \
--arg routing_policies "${rtintent_policy_internet_json_string}" \
"$rtintent_json")
elif [[ "$policy" == 'private' ]]; then
rtintent_json_string=$(jq -n \
--arg routing_policies "${rtintent_policy_private_json_string}" \
"$rtintent_json")
elif [[ "$policy" == 'both' ]]; then
rtintent_json_string=$(jq -n \
--arg routing_policies "${rtintent_policy_internet_json_string},${rtintent_policy_private_json_string}" \
"$rtintent_json")
else
echo "Routing intent policy $2 not recognized, only 'internet', 'private' or 'both' supported"
return
fi
# Remove escapes from string (jq substitution doesnt work for objects)
rtintent_json_string=$(echo "$rtintent_json_string" | sed "s@\\\\@@g")
rtintent_json_string=$(echo "${rtintent_json_string}" | sed "s@\"{@{@g")
rtintent_json_string=$(echo "${rtintent_json_string}" | sed "s@}\"@}@g")
# Send REST
subscription_id=$(az account show --query id -o tsv)
hub_name="hub$1"
intent_name="intent$1"
rtintent_uri="https://management.azure.com/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/virtualHubs/${hub_name}/routingIntent/${intent_name}?api-version=$vwan_api_version"
# echo "Sending PUT request to $rtintent_uri..."
az rest --method put --uri $rtintent_uri --headers Content-Type=application/json --body $rtintent_json_string -o none
wait_until_finished_rest $rtintent_uri
}
# Get details of routing intent for a hub
function get_routing_intent() {
hub_name="hub$1"
intent_name="intent$1"
rtintent_uri="https://management.azure.com/subscriptions/${subscription_id}/resourceGroups/${rg}/providers/Microsoft.Network/virtualHubs/${hub_name}/routingIntent/${intent_name}?api-version=$vwan_api_version"
# echo "Sending GET request to $rtintent_uri..."
az rest --method get --uri $rtintent_uri
}
# Get JSON for a hub or all hubs
function get_vhub {
hub_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}?api-version=$vwan_api_version"
if [[ -z "${hub_name}" ]]
then
az rest --method get --uri $uri | jq '.value'
else
az rest --method get --uri $uri | jq
fi
}
# Get provisioningState for a hub or all hubs
function get_vhub_state {
hub_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}?api-version=$vwan_api_version"
if [[ -z "${hub_name}" ]]
then
az rest --method get --uri $uri | jq -r '.value | map({name, provisioningState: .properties.provisioningState})'
else
az rest --method get --uri $uri | jq -r '.properties.provisioningState'
fi
}
# Print a list of virtual hubs to iterate over it
function list_vhub {
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs?api-version=$vwan_api_version"
az rest --method get --uri $uri | jq -r '.value[].name'
}
# Get JSON for a vnet connection or all vnet connections in a hub
function get_vnetcx {
hub_name=$1
cx_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
if [[ -z "${cx_name}" ]]
then
az rest --method get --uri $uri | jq '.value'
else
az rest --method get --uri $uri | jq
fi
}
# Get provisioningState for a vnet cx in a hub or all vnet cx in a hub
function get_vnetcx_state {
hub_name=$1
cx_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
if [[ -z "${cx_name}" ]]
then
az rest --method get --uri $uri | jq -r '.value | map({name, provisioningState: .properties.provisioningState})'
else
az rest --method get --uri $uri | jq -r '.properties.provisioningState'
fi
}
# List vnet connections
function list_vnetcx {
hub_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/?api-version=$vwan_api_version"
az rest --method get --uri $uri | jq -r '.value[].name' 2>/dev/null
}
# Get JSON for a hubRT or all hubRTs
function get_rt {
hub_name=$1
rt_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
if [[ -z "${rt_name}" ]]
then
az rest --method get --uri $uri | jq '.value'
else
az rest --method get --uri $uri | jq
fi
}
# Get provisioningState for a hubRT or all hubRTs
function get_rt_state {
hub_name=$1
rt_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
if [[ -z "${rt_name}" ]]
then
az rest --method get --uri $uri | jq -r '.value | map({name, provisioningState: .properties.provisioningState})'
else
az rest --method get --uri $uri | jq -r '.properties.provisioningState'
fi
}
# Get provisioningState for a hubRT or all hubRTs
function list_rt {
hub_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables?api-version=$vwan_api_version"
az rest --method get --uri $uri | jq -r '.value[].name'
}
# Get JSON for a vpngw or all vpngw
function get_vpngw {
gw_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
if [[ -z "${gw_name}" ]]
then
az rest --method get --uri $uri | jq '.value'
else
az rest --method get --uri $uri | jq
fi
}
# Get provisioningState for a VPN GW or all VPN GWs
function get_vpngw_state {
gw_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
if [[ -z "${gw_name}" ]]
then
az rest --method get --uri $uri | jq -r '.value | map({name, provisioningState: .properties.provisioningState})'
else
az rest --method get --uri $uri | jq -r '.properties.provisioningState'
fi
}
# Get VPN gateway ID for a certain hub
function get_vpngw_id {
hub_id=$1
az network vhub show -n hub${hub_id} -g $rg --query vpnGateway.id -o tsv
}
# Get VPN gateway ID for a certain hub
function get_azfw_id {
hub_id=$1
az network firewall show -n azfw${hub_id} -g $rg --query id -o tsv
}
# Get VPN gateway ID for a certain hub
function get_vnetcx_id {
hub_id=$1
spoke_id=$2
cx_name="spoke${hub_id}${spoke_id}"
hub_name=hub${hub_id}
az network vhub connection show -n $cx_name --vhub-name $hub_name -g $rg --query id -o tsv
}
# List all VPN gateways (to iterate over them afterwards)
function list_vpngw {
gw_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways?api-version=$vwan_api_version"
az rest --method get --uri $uri | jq -r '.value[].name'
}
# Get BGP info for a VPN GW or all VPN GWs
function get_vpngw_bgp {
gw_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
if [[ -z "${gw_name}" ]]
then
az rest --method get --uri $uri | jq -r '.value | map({name, provisioningState: .properties.bgpSettings})'
else
az rest --method get --uri $uri | jq -r '.properties.bgpSettings'
fi
}
# Get VPN connections
function get_vpngw_cx {
gw_name=$1
cx_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
if [[ -z "${cx_name}" ]]
then
az rest --method get --uri $uri | jq -r '.properties.connections'
else
az rest --method get --uri $uri | jq -r '.properties.connections[] | select (.name == "'$cx_name'")'
fi
}
# Get VPN connections state
function get_vpngw_cx_state {
gw_name=$1
cx_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
if [[ -z "${cx_name}" ]]
then
az rest --method get --uri $uri | jq -r '.properties.connections | map({name, provisioningState: .properties.provisioningState})'
else
az rest --method get --uri $uri | jq -r '.properties.connections[] | select (.name == "'$cx_name'") | .properties.provisioningState'
fi
}
# Get JSON for a VPN site or all VPN sites
function get_vpnsite {
site_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnSites/${site_name}?api-version=$vwan_api_version"
if [[ -z "${site_name}" ]]
then
az rest --method get --uri $uri | jq '.value'
else
az rest --method get --uri $uri | jq
fi
}
# Get provisioningState for a VPN site or all VPN sites
function get_vpnsite_state {
site_name=$1
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnSites/${site_name}?api-version=$vwan_api_version"
if [[ -z "${site_name}" ]]
then
az rest --method get --uri $uri | jq -r '.value | map({name, provisioningState: .properties.provisioningState})'
else
az rest --method get --uri $uri | jq -r '.properties.provisioningState'
fi
}
#################
# RT #
#################
# Create Route Table (aka hubRouteTable)
# https://docs.microsoft.com/en-us/rest/api/virtualwan/hubroutetables/createorupdate
function create_rt {
hub_name=$1
rt_name=$2
rt_label=$3
rt_json_string=$(jq -n \
"$rt_json")
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
if [[ -n "$rt_label" ]]
then
rt_json_string=$(echo $rt_json_string | jq '.properties.labels += [ "'$rt_label'" ] | {name, properties}')
fi
echo "Creating route in ${hub_name}/${rt_name}..."
wait_until_hub_finished $hub_name
az rest --method put --uri $rt_uri --body $rt_json_string >/dev/null
}
# Delete rt
function delete_rt {
hub_name=$1
rt_name=$2
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
az rest --method delete --uri $rt_uri
}
# Update vnet connection associated RT
function cx_set_ass_rt {
hub_name=$1
cx_name=$2
new_rt_name=$3
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
new_rt_id="/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${new_rt_name}"
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.associatedRouteTable.id = "'$new_rt_id'" | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $cx_uri --body $cx_json_updated >/dev/null
# az rest --method get --uri $cx_uri | jq '.properties.routingConfiguration.associatedRouteTable.id'
}
# Update vnet connection propagated RT
# Example: cx_set_prop_rt hub1 spoke1 redRT,defaultRouteTable
function cx_set_prop_rt {
hub_name=$1
cx_name=$2
IFS=',' read -r -a new_rt_names <<< "$3"
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
new_rt_ids=""
for new_rt_name in ${new_rt_names[@]}; do
new_rt_ids="${new_rt_ids}{\"id\": \"/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${new_rt_name}\"},"
done
new_rt_ids="${new_rt_ids: : -1}" # Remove trailing comma
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.propagatedRouteTables.ids = ['$new_rt_ids'] | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $cx_uri --body $cx_json_updated >/dev/null
}
# Modify propagation labels
function cx_set_prop_labels {
hub_name=$1
cx_name=$2
if [[ -n "$3" ]]
then
echo "Setting labels from connection ${hub_name}/${cx_name} to $3..."
if [ -n "$BASH_VERSION" ]; then
arr_opt=a
elif [ -n "$ZSH_VERSION" ]; then
arr_opt=A
fi
IFS=',' read -r"$arr_opt" new_labels <<< "$3"
new_labels_txt=""
for new_label in ${new_labels[@]}; do
new_labels_txt="${new_labels_txt}\"${new_label}\","
done
new_labels_txt="${new_labels_txt: : -1}" # Remove trailing comma
else
echo "Deleting labels from connection ${hub_name}/${cx_name}..."
new_labels_txt=" "
fi
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.propagatedRouteTables.labels = ['${new_labels_txt}'] | {name, properties}')
wait_until_hub_finished $hub_name
# Check: if hub is Failed, we can try to reset it
# We dont do this inside of the wait_until_hub_finished function because we could have infinite recursion
hub_state=$(get_vhub_state $hub_name)
if [[ "$hub_state" == "Failed" ]]
then
echo "Hub $hub_name is Failed, trying to fix it with a reset"
reset_vhub "$hub_name"
wait_until_hub_finished "$hub_name"
fi
# Check: if hub is still Failed, do not do anything
hub_state=$(get_vhub_state "$hub_name")
if [[ "$hub_state" == "Succeeded" ]]
then
az rest --method put --uri "$cx_uri" --body "$cx_json_updated" >/dev/null
else
echo "Hub $hub_name is $hub_state and could not fix it"
fi
}
# Modify propagated RT for VPN connection
# RT can be given as rt_name or hub_name/rt_name
# Example:
# vpncx_set_prop_rt 1 branch1 hub1/defaultRouteTable
function vpncx_set_prop_rt {
hub_id=$1
cx_name=$2
gw_id=$(az network vhub show -n hub${hub_id} -g $rg --query vpnGateway.id -o tsv)
gw_name=$(echo $gw_id | cut -d/ -f 9)
echo "Setting routing for VPN connection $cx_name in gateway $gw_name..."
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}/vpnConnections/${cx_name}?api-version=$vwan_api_version"
if [ -n "$BASH_VERSION" ]; then
arr_opt=a
elif [ -n "$ZSH_VERSION" ]; then
arr_opt=A
fi
IFS=',' read -r"$arr_opt" new_rt_names <<< "$3"
hub_id=$(az network vpn-gateway show -n $gw_name -g $rg --query 'virtualHub.id' -o tsv)
hub_name=$(echo $hub_id | cut -d/ -f 9)
new_rt_ids=""
for new_proprt_name in "${new_rt_names[@]}"
do
# support both formats: hub/rt and rt (defaults to local hub)
proprt_hub_name=$(echo $new_proprt_name | cut -d/ -f 1)
proprt_rt_name=$(echo $new_proprt_name | cut -d/ -f 2)
if [[ -z "$proprt_rt_name" ]]
then
proprt_hub_name=$hub_name
proprt_rt_name=$new_proprt_name
fi
new_rt_ids="${new_rt_ids}{\"id\": \"/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${proprt_hub_name}/hubRouteTables/${proprt_rt_name}\"},"
done
new_rt_ids="${new_rt_ids: : -1}" # Remove trailing comma
vpncx_json=$(az rest --method get --uri $uri)
vpncx_json=$(echo $vpncx_json | jq '{name, properties}')
# Remove unneeded attributes
vpncx_json=$(echo $vpncx_json | jq 'del(.properties.vpnLinkConnections[].resourceGroup)')
vpncx_json=$(echo $vpncx_json | jq 'del(.properties.vpnLinkConnections[].etag)')
vpncx_json=$(echo $vpncx_json | jq 'del(.properties.vpnLinkConnections[].type)')
# Update routing config
# echo "Setting route table ids to $new_rt_ids..."
vpncx_json_updated=$(echo $vpncx_json | jq '.properties.routingConfiguration.propagatedRouteTables.ids=['"$new_rt_ids"']')
# Optionally, set labels
if [[ -n "$4" ]]
then
IFS=',' read -r"$arr_opt" new_labels <<< "$4"
new_labels_txt=""
for new_label in "${new_labels[@]}"; do
new_labels_txt="${new_labels_txt}\"${new_label}\","
done
new_labels_txt="${new_labels_txt: : -1}" # Remove trailing comma
# echo "Setting labels to $new_labels_txt..."
vpncx_json_updated=$(echo $vpncx_json_updated | jq '.properties.routingConfiguration.propagatedRouteTables.labels=['"$new_labels_txt"']')
fi
# Wait for the vpn gw to be Suceeded
wait_until_gw_finished $gw_name
# Send JSON
az rest --method put --uri $uri --body $vpncx_json_updated >/dev/null
}
# Using vpncx URI
# https://docs.microsoft.com/en-us/rest/api/virtualwan/vpnconnections/createorupdate
function vpncx_set_prop_labels {
gw_name=$1
cx_name=$2
uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}/vpnConnections/${cx_name}?api-version=$vwan_api_version"
hub_id=$(az network vpn-gateway show -n $gw_name -g $rg --query 'virtualHub.id' -o tsv)
hub_name=$(echo $hub_id | cut -d/ -f 9)
if [[ -n "$3" ]]
then
if [ -n "$BASH_VERSION" ]; then
arr_opt=a
elif [ -n "$ZSH_VERSION" ]; then
arr_opt=A
fi
IFS=',' read -r"$arr_opt" new_labels <<< "$3"
new_labels_txt=""
for new_label in ${new_labels[@]}; do
new_labels_txt="${new_labels_txt}\"${new_label}\","
done
new_labels_txt="${new_labels_txt: : -1}" # Remove trailing comma
else
new_labels_txt=""
fi
# Get current JSON
vpncx_json=$(az rest --method get --uri $uri)
# Remove unneeded attributes
vpncx_json=$(echo $vpncx_json | jq 'del(.properties.vpnLinkConnections[].resourceGroup)')
vpncx_json=$(echo $vpncx_json | jq 'del(.properties.vpnLinkConnections[].etag)')
vpncx_json=$(echo $vpncx_json | jq 'del(.properties.vpnLinkConnections[].type)')
# Update routing config
vpncx_json_updated=$(echo $vpncx_json | jq '.properties.routingConfiguration.propagatedRouteTables.labels=['"$new_labels_txt"'] | {name, properties}')
# Wait for the vpn gw to be Suceeded
wait_until_gw_finished $gw_name
# PUT new JSON
az rest --method put --uri $uri --body $vpncx_json_updated >/dev/null
}
# Update vnet connection associated and propagated RT at the same time
# example: cx_set_rt hub1 spoke1 redRT redRT,defaultRouteTable
function cx_set_rt {
hub_name=$1
cx_name=$2
new_assrt_name=$3
if [ -n "$BASH_VERSION" ]; then
arr_opt=a
elif [ -n "$ZSH_VERSION" ]; then
arr_opt=A
fi
IFS=',' read -r"$arr_opt" new_proprt_names <<< "$4"
echo "Setting connection $cx_name associated route table to $new_assrt_name, propagated route tables to $new_proprt_names" # DEBUG
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
new_assrt_id="/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${new_assrt_name}"
new_proprt_ids=""
for new_proprt_name in ${new_proprt_names[@]}; do
# support both formats: hub/rt and rt (defaults to local hub)
proprt_hub_name=$(echo $new_proprt_name | cut -d/ -f 1)
proprt_rt_name=$(echo $new_proprt_name | cut -d/ -f 2)
if [[ -z "$proprt_rt_name" ]]
then
proprt_hub_name=$hub_name
proprt_rt_name=$new_proprt_name
fi
new_proprt_ids="${new_proprt_ids}{\"id\": \"/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${proprt_hub_name}/hubRouteTables/${proprt_rt_name}\"},"
done
new_proprt_ids="${new_proprt_ids: : -1}" # Remove trailing comma
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.associatedRouteTable.id = "'$new_assrt_id'" | .properties.routingConfiguration.propagatedRouteTables.ids = ['$new_proprt_ids'] | {name, properties}')
if [[ -n "$5" ]]
then
IFS=',' read -r"$arr_opt" new_labels <<< "$5"
new_labels_txt=""
for new_label in ${new_labels[@]}; do
new_labels_txt="${new_labels_txt}\"${new_label}\","
done
new_labels_txt="${new_labels_txt: : -1}" # Remove trailing comma
cx_json_updated=$(echo $cx_json_updated | jq '.properties.routingConfiguration.propagatedRouteTables.labels = ['${new_labels_txt}'] | {name, properties}')
fi
wait_until_hub_finished $hub_name
az rest --method put --uri $cx_uri --body $cx_json_updated >/dev/null
}
# Add routes to RT
# https://docs.microsoft.com/en-us/rest/api/virtualwan/hubroutetables/createorupdate#hubroute
function rt_add_route {
hub_name=hub$1
rt_name=$2
prefix=$3
nexthop=$4
echo "Adding static route for ${prefix} to route table ${hub_name}/${rt_name}"
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
rt_json_current=$(az rest --method get --uri $rt_uri)
# type (next hop): CIDR, resourceId, Service
# prefixes: comma-separated prefix list
new_route_json_string=$(jq -n \
--arg name "route$RANDOM" \
--arg type "ResourceId" \
--arg prefixes "$prefix" \
--arg nexthop "$nexthop" \
"$route_json")
rt_json_updated=$(echo $rt_json_current | jq '.properties.routes += [ '$new_route_json_string' ] | {name, properties}')
wait_until_hub_finished $hub_name
# Check: if hub is Failed, we can try to reset it
# We dont do this inside of the wait_until_hub_finished function because we could have infinite recursion
hub_state=$(get_vhub_state $hub_name)
if [[ "$hub_state" == "Failed" ]]
then
echo "Hub $hub_name is Failed, trying to fix it with a reset"
reset_vhub "$hub_name"
wait_until_hub_finished "$hub_name"
fi
# Check: if hub is still Failed, do not do anything
hub_state=$(get_vhub_state "$hub_name")
if [[ "$hub_state" == "Succeeded" ]]
then
az rest --method put --uri $rt_uri --body $rt_json_updated >/dev/null # PUT
else
echo "Hub $hub_name is $hub_state and could not fix it"
fi
}
# Delete all routes from RT
function rt_delete_routes {
hub_name=$1
rt_name=$2
echo "Deleting all routes from route table ${hub_name}/${rt_name}"
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
rt_json_current=$(az rest --method get --uri $rt_uri)
rt_json_updated=$(echo $rt_json_current | jq '.properties.routes = [] | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $rt_uri --body $rt_json_updated >/dev/null # PUT
}
# Add routes to vnet conection
# https://docs.microsoft.com/en-us/rest/api/virtualwan/hubvirtualnetworkconnections/createorupdate#staticroute
# Example: cx_add_routes hub1 spoke1 192.168.0.0/16 172.21.10.68
function cx_add_routes {
hub_name=hub$1
cx_name=$2
prefix=$3
nexthop=$4
echo "Adding route for $prefix to $nexthop in connection ${hub_name}/${cx_name}..."
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
# prefixes: comma-separated prefix list
new_route_json_string=$(jq -n \
--arg name "route$RANDOM" \
--arg prefixes "$prefix" \
--arg nexthop "$nexthop" \
"$cxroute_json")
existing_routes=$(echo $cx_json | jq '.properties.routingConfiguration.vnetRoutes.staticRoutes[]')
if [ -z "${existing_routes}" ]
then
new_routes=${new_route_json_string}
else
new_routes=${existing_routes},${new_route_json_string}
fi
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.vnetRoutes.staticRoutes = ['$new_routes'] | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $cx_uri --body $cx_json_updated >/dev/null # PUT
}
# Delete all routes from vnet cx
function cx_delete_routes {
hub_name=$1
cx_name=$2
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.vnetRoutes.staticRoutes = [] | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $cx_uri --body $cx_json_updated >/dev/null
# az rest --method get --uri $cx_uri | jq '.properties.routingConfiguration.vnetRoutes.staticRoutes' # GET
}
# Delete all labels from vnet cx
function cx_delete_labels {
hub_name=$1
cx_name=$2
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json=$(az rest --method get --uri $cx_uri)
cx_json_updated=$(echo $cx_json | jq '.properties.routingConfiguration.propagatedRouteTables.labels = [] | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $cx_uri --body $cx_json_updated # PUT
# az rest --method get --uri $cx_uri | jq '.properties.routingConfiguration.propagatedRouteTables.labels' # GET
}
# Add label to route table
# https://docs.microsoft.com/en-us/rest/api/virtualwan/hubroutetables/createorupdate#hubroute
function rt_add_label {
hub_name=$1
rt_name=$2
new_label=$3
wait_until_hub_finished $hub_name
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
rt_json_current=$(az rest --method get --uri $rt_uri)
rt_json_updated=$(echo $rt_json_current | jq '.properties.labels += [ "'$new_label'" ] | {name, properties}')
az rest --method put --uri $rt_uri --body $rt_json_updated # PUT
}
# Delete all labels
function rt_delete_labels {
hub_name=$1
rt_name=$2
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
rt_json_current=$(az rest --method get --uri $rt_uri)
rt_json_updated=$(echo $rt_json_current | jq '.properties.labels = [] | {name, properties}')
wait_until_hub_finished $hub_name
az rest --method put --uri $rt_uri --body $rt_json_updated # PUT
}
#################
# Reset stuff #
#################
# reset vhub
function reset_vhub {
hub_name=$1
hub_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}?api-version=$vwan_api_version"
hub_json_current=$(az rest --method get --uri $hub_uri)
hub_json_updated=$(echo $hub_json_current | jq '{name, location, properties}')
az rest --method put --uri $hub_uri --body $hub_json_updated >/dev/null
}
# reset vhub
function reset_rt {
hub_name=$1
rt_name=$2
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
rt_json_current=$(az rest --method get --uri $rt_uri)
rt_json_updated=$(echo $rt_json_current | jq '{name, location, properties}')
az rest --method put --uri $rt_uri --body $rt_json_updated >/dev/null
}
# reset vnet cx
function reset_vhub_cx {
hub_name=$1
cx_name=$2
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
cx_json_current=$(az rest --method get --uri $cx_uri) # GET
cx_json_updated=$(echo $cx_json_current | jq '{name, location, properties}')
# If you delete, you should wait until the delete operation finishes before sending the PUT, hence commented out
# az rest --method delete --uri $cx_uri # DELETE
az rest --method put --uri $cx_uri --body $cx_json_updated >/dev/null
}
# reset vpngw - NOT WORKING!
function reset_vpngw {
gw_name=$1
gw_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
gw_json_current=$(az rest --method get --uri $gw_uri)
gw_json_updated=$(echo $gw_json_current | jq '{name, location, properties}')
az rest --method put --uri $gw_uri --body $gw_json_updated
}
##################
# Summary info #
##################
# Get label info
function get_cx_labels {
hub_name=$1
cx_name=$2
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
az rest --method get --uri $cx_uri | jq -r '.properties.routingConfiguration.propagatedRouteTables.labels[]' | paste -sd, - 2>/dev/null
}
function get_rt_labels {
hub_name=$1
rt_name=$2
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
az rest --method get --uri $rt_uri | jq -r '.properties.labels[]' | paste -sd, - 2>/dev/null
}
function get_rt_routes {
hub_name=$1
rt_name=$2
rt_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubRouteTables/${rt_name}?api-version=$vwan_api_version"
az rest --method get --uri $rt_uri | jq -r '.properties.routes[] | .destinations[],.nextHop' | paste -sd, - 2>/dev/null
}
function get_vpncx_labels {
gw_name=$1
cx_name=$2
gw_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
az rest --method get --uri $gw_uri | jq -r '.properties.connections[] | select (.name == "'$cx_name'") | .properties.routingConfiguration.propagatedRouteTables.labels[]' | paste -sd, - 2>/dev/null
}
function get_vpncx {
gw_name=$1
cx_name=$2
gw_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
az rest --method get --uri $gw_uri | jq -r '.properties.connections[] | select (.name == "'$cx_name'")' 2>/dev/null
}
function list_vpncx {
gw_name=$1
gw_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/vpnGateways/${gw_name}?api-version=$vwan_api_version"
az rest --method get --uri $gw_uri | jq -r '.properties.connections[].name' 2>/dev/null
}
function labels {
# Vnet connections
hubs=$(list_vhub)
while IFS= read -r hub_name; do
vnet_cxs=$(list_vnetcx $hub_name)
if [[ -n "$vnet_cxs" ]]
then
while IFS= read -r vnetcx_name; do
echo "${hub_name}/${vnetcx_name} connection: $(get_cx_labels $hub_name $vnetcx_name)"
done <<< "$vnet_cxs"
else
echo "No vnet connections in hub $hub_name"
fi
done <<< "$hubs"
# VPN connections
vpngws=$(list_vpngw)
while IFS= read -r gw_name; do
vpn_cxs=$(list_vpncx $gw_name)
if [[ -n "$vpn_cxs" ]]
then
while IFS= read -r vpncx_name; do
echo "${gw_name}/${vpncx_name} connection: $(get_vpncx_labels $gw_name $vpncx_name)"
done <<< "$vpn_cxs"
else
echo "No VPN connections in gateway $gw_name"
fi
done <<< "$vpngws"
# Route Tables
while IFS= read -r hub_name; do
rts=$(list_rt $hub_name)
if [[ -n "$rts" ]]
then
while IFS= read -r rt_name; do
echo "${hub_name}/${rt_name}: $(get_rt_labels $hub_name $rt_name)"
done <<< "$rts"
else
echo "No route tables in hub $hub_name"
fi
done <<< "$hubs"
}
function state {
# Hubs
hubs=$(list_vhub)
while IFS= read -r hub_name; do
echo "${hub_name}: $(get_vhub_state $hub_name)"
# Vnet connections
vnet_cxs=$(list_vnetcx $hub_name)
if [[ -n "$vnet_cxs" ]]
then
while IFS= read -r vnetcx_name; do
echo "${hub_name}/${vnetcx_name} connection: $(get_vnetcx_state $hub_name $vnetcx_name)"
done <<< "$vnet_cxs"
else
echo "No vnet connections in hub $hub_name"
fi
# Route Tables
rts=$(list_rt $hub_name)
if [[ -n "$rts" ]]
then
while IFS= read -r rt_name; do
echo "${hub_name}/${rt_name}: $(get_rt_state $hub_name $rt_name)"
done <<< "$rts"
else
echo "No route tables in hub $hub_name"
fi
done <<< "$hubs"
# VPN connections
vpngws=$(list_vpngw)
while IFS= read -r gw_name; do
echo "${gw_name}: $(get_vpngw_state $gw_name)"
vpn_cxs=$(list_vpncx $gw_name)
if [[ -n "$vpn_cxs" ]]
then
while IFS= read -r vpncx_name; do
echo "${gw_name}/${vpncx_name} connection: $(get_vpngw_cx_state $gw_name $vpncx_name)"
done <<< "$vpn_cxs"
else
echo "No VPN connections in gateway $gw_name"
fi
done <<< "$vpngws"
}
# Get associated/propagated routing tables
function print_routing {
routing=$1
assrt=$(echo $routing | jq -r '.associatedRouteTable.id')
assrt_hub=$(echo $assrt | cut -d/ -f 9)
assrt_name=$(echo $assrt | cut -d/ -f 11)
proprt=$(echo $routing | jq -r '.propagatedRouteTables.ids[].id')
proprt_txt=""
while IFS= read -r proprt_id; do
proprt_hub=$(echo $proprt_id | cut -d/ -f 9)
proprt_name=$(echo $proprt_id | cut -d/ -f 11)
if [[ -n "$proprt_txt" ]]
then
proprt_txt+=", "
fi
proprt_txt+=${proprt_hub}/${proprt_name}
done <<< "$proprt"
proplbls=$(echo $routing | jq -r '.propagatedRouteTables.labels[]')
proplbl_txt=""
while IFS= read -r label; do
if [[ -n "$proplbl_txt" ]]
then
proplbl_txt+=", "
fi
proplbl_txt+=${label}
done <<< "$proplbls"
echo " * Associated: ${assrt_hub}/${assrt_name}"
echo " * Propagated: $proprt_txt - Labels: ${proplbl_txt}"
}
function get_cx_routing {
hub_name=$1
cx_name=$2
cx_uri="https://management.azure.com/subscriptions/$subscription_id/resourceGroups/$rg/providers/Microsoft.Network/virtualHubs/${hub_name}/hubVirtualNetworkConnections/${cx_name}?api-version=$vwan_api_version"
routing=$(az rest --method get --uri $cx_uri | jq -r '.properties.routingConfiguration')
echo "$hub_name / $cx_name"