/
resource_aviatrix_gateway.go
2780 lines (2602 loc) · 103 KB
/
resource_aviatrix_gateway.go
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
package aviatrix
import (
"errors"
"fmt"
"log"
"strconv"
"strings"
"sync"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/AviatrixSystems/terraform-provider-aviatrix/v3/goaviatrix"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAviatrixGateway() *schema.Resource {
return &schema.Resource{
Create: resourceAviatrixGatewayCreate,
Read: resourceAviatrixGatewayRead,
Update: resourceAviatrixGatewayUpdate,
Delete: resourceAviatrixGatewayDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceAviatrixGatewayResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceAviatrixGatewayStateUpgradeV0,
Version: 0,
},
},
Schema: map[string]*schema.Schema{
"cloud_type": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
Description: "Type of cloud service provider.",
ValidateFunc: validateCloudType,
},
"account_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Account name. This account will be used to launch Aviatrix gateway.",
},
"gw_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Aviatrix gateway unique name.",
},
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of legacy VPC/Vnet to be connected.",
DiffSuppressFunc: DiffSuppressFuncGatewayVpcId,
},
"vpc_reg": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Region where this gateway will be launched.",
},
"gw_size": {
Type: schema.TypeString,
Required: true,
Description: "Size of Gateway Instance.",
},
"subnet": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A VPC Network address range selected from one of the available network ranges.",
},
"zone": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Availability Zone. Only available for Azure (8), Azure GOV (32), Azure CHINA (2048) and Public Subnet Filtering gateway. Must be in the form 'az-n', for example, 'az-2'.",
},
"insane_mode_az": {
Type: schema.TypeString,
Optional: true,
Default: "",
ForceNew: true,
Description: "AZ of subnet being created for Insane Mode Gateway. Required if insane_mode is set.",
},
"single_ip_snat": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable Source NAT for this container.",
},
"vpn_access": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable user access through VPN to this container.",
},
"vpn_cidr": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "VPN CIDR block for the container.",
},
"enable_elb": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Specify whether to enable ELB or not.",
},
"split_tunnel": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Specify split tunnel mode.",
},
"max_vpn_conn": {
Type: schema.TypeString,
Optional: true,
Default: nil,
Computed: true,
Description: "Maximum connection of VPN access. Valid for VPN gateway only. If not set, '100' will be default value.",
},
"name_servers": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A list of DNS servers used to resolve domain names by " +
"a connected VPN user when Split Tunnel Mode is enabled.",
},
"search_domains": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A list of domain names that will use the NameServer " +
"when a specific name is not in the destination when Split Tunnel Mode is enabled.",
},
"additional_cidrs": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A list of destination CIDR ranges that will also go through the VPN tunnel " +
"when Split Tunnel Mode is enabled.",
},
"otp_mode": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Two step authentication mode.",
},
"saml_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "This field indicates whether to enable SAML or not.",
},
"enable_vpn_nat": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "This field indicates whether to enable VPN NAT or not. Only supported for VPN gateway. Valid values: true, false. Default value: true.",
},
"okta_token": {
Type: schema.TypeString,
Optional: true,
Default: "",
Sensitive: true,
Description: "Token for Okta auth mode.",
},
"okta_url": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "URL for Okta auth mode.",
},
"okta_username_suffix": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Username suffix for Okta auth mode.",
},
"duo_integration_key": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Integration key for DUO auth mode.",
},
"duo_secret_key": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Default: "",
Description: "Secret key for DUO auth mode.",
},
"duo_api_hostname": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "API hostname for DUO auth mode.",
},
"duo_push_mode": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Push mode for DUO auth.",
},
"enable_ldap": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Specify whether to enable LDAP or not.",
},
"ldap_server": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "LDAP server address. Required: Yes if enable_ldap is 'yes'.",
},
"ldap_bind_dn": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "LDAP bind DN. Required: Yes if enable_ldap is 'yes'.",
},
"ldap_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Default: "",
Description: "LDAP password. Required: Yes if enable_ldap is 'yes'.",
},
"ldap_base_dn": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "LDAP base DN. Required: Yes if enable_ldap is 'yes'.",
},
"ldap_username_attribute": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "LDAP user attribute. Required: Yes if enable_ldap is 'yes'.",
},
"peering_ha_subnet": {
Type: schema.TypeString,
Optional: true,
Description: "Public Subnet Information while creating Peering HA Gateway, only subnet is accepted. " +
"Required to create peering ha gateway if cloud_type = 1 or 8 (AWS or Azure). Optional if cloud_type = 4 (GCP)",
},
"peering_ha_zone": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Zone information for creating Peering HA Gateway. Required to create peering ha gateway if cloud_type = 4 (GCP). Optional for cloud_type = 8 (Azure).",
},
"peering_ha_insane_mode_az": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "AZ of subnet being created for Insane Mode Peering HA Gateway. Required if insane_mode is set.",
},
"peering_ha_gw_size": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Peering HA Gateway Size.",
},
"single_az_ha": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Set to true if this feature is desired.",
},
"allocate_new_eip": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "When value is false, reuse an idle address in Elastic IP pool for this gateway. " +
"Otherwise, allocate a new Elastic IP and use it for this gateway.",
},
"insane_mode": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
Description: "Enable Insane Mode for Gateway. Valid values: true, false.",
},
"enable_vpc_dns_server": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable vpc_dns_server for Gateway. Valid values: true, false.",
},
"enable_designated_gateway": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable 'designated_gateway' feature for Gateway. Valid values: true, false.",
},
"additional_cidrs_designated_gateway": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "A list of CIDR ranges separated by comma to configure when 'designated_gateway' feature is enabled.",
},
"enable_encrypt_volume": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable encrypt gateway EBS volume. Only supported for AWS provider. Valid values: true, false. Default value: false.",
},
"customer_managed_keys": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
Description: "Customer managed key ID.",
},
"enable_monitor_gateway_subnets": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable monitor gateway subnets. Valid values: true, false. Default value: false.",
},
"monitor_exclude_list": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Description: "A set of monitored instance ids. Only valid when 'enable_monitor_gateway_subnets' = true.",
},
"idle_timeout": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: validation.IntAtLeast(301),
Description: "Typed value when modifying idle_timeout. If it's -1, this feature is disabled.",
},
"renegotiation_interval": {
Type: schema.TypeInt,
Optional: true,
Default: -1,
ValidateFunc: validation.IntAtLeast(301),
Description: "Typed value when modifying renegotiation_interval. If it's -1, this feature is disabled.",
},
"fqdn_lan_cidr": {
Type: schema.TypeString,
Optional: true,
Default: "",
ForceNew: true,
Description: "FQDN gateway lan interface cidr.",
},
"fqdn_lan_vpc_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: DiffSuppressFuncGCPVpcId,
Description: "LAN VPC ID. Only used for GCP FQDN Gateway.",
},
"enable_public_subnet_filtering": {
Type: schema.TypeBool,
Optional: true,
Default: false,
RequiredWith: []string{
"public_subnet_filtering_route_tables",
"public_subnet_filtering_guard_duty_enforced",
},
ConflictsWith: conflictingPublicSubnetFilteringGatewayConfigKeys,
Description: "Create a [Public Subnet Filtering gateway](https://docs.aviatrix.com/HowTos/public_subnet_filtering_faq.html).",
},
"public_subnet_filtering_route_tables": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Route tables whose associated public subnets are protected. Required when `enable_public_subnet_filtering` attribute is true.",
},
"public_subnet_filtering_ha_route_tables": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Route tables whose associated public subnets are protected for the HA PSF gateway. Required when enable_public_subnet_filtering and peering_ha_subnet are set.",
},
"public_subnet_filtering_guard_duty_enforced": {
Type: schema.TypeBool,
Default: true,
Optional: true,
Description: "Whether to enforce Guard Duty IP blocking. Required when `enable_public_subnet_filtering` attribute is true. Valid values: true or false. Default value: true.",
},
"enable_jumbo_frame": {
Type: schema.TypeBool,
Default: true,
Optional: true,
Description: "Enable jumbo frame support for Gateway. Valid values: true or false. Default value: true.",
},
"enable_gro_gso": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Description: "Specify whether to disable GRO/GSO or not.",
},
"tags": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "A map of tags to assign to the gateway.",
},
"enable_spot_instance": {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
v := val.(bool)
if !v {
errs = append(errs, fmt.Errorf("expected %s to true to enable spot instance, got: %v", key, val))
return warns, errs
}
return
},
Description: "Enable spot instance. NOT supported for production deployment.",
RequiredWith: []string{"spot_price"},
},
"spot_price": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Price for spot instance. NOT supported for production deployment.",
RequiredWith: []string{"enable_spot_instance"},
},
"rx_queue_size": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{"1K", "2K", "4K", "8K", "16K"}, false),
Description: "Gateway ethernet interface RX queue size. Supported for AWS related clouds only. Applies on HA as well if enabled.",
},
"availability_domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "Availability domain for OCI.",
},
"fault_domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "Fault domain for OCI.",
},
"peering_ha_availability_domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Peering HA availability domain for OCI.",
},
"peering_ha_fault_domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Peering HA fault domain for OCI.",
},
"eip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Required when allocate_new_eip is false. It uses specified EIP for this gateway.",
},
"peering_ha_eip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Public IP address that you want assigned to the HA peering instance.",
},
"azure_eip_name_resource_group": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the public IP address and its resource group in Azure to assign to this Gateway.",
ValidateFunc: validateAzureEipNameResourceGroup,
},
"peering_ha_azure_eip_name_resource_group": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the public IP address and its resource group in Azure to assign to the Peering HA Gateway.",
ValidateFunc: validateAzureEipNameResourceGroup,
},
"elb_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "A name for the ELB that is created.",
},
"vpn_protocol": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"TCP", "UDP"}, false),
Description: "Elb protocol for VPN gateway with elb enabled. Only supports AWS provider. " +
"Valid values: 'TCP', 'UDP'. If not specified, 'TCP'' will be used.",
},
"tunnel_detection_time": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(20, 600),
Description: "The IPSec tunnel down detection time for the Gateway.",
},
"software_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "software_version can be used to set the desired software version of the gateway. " +
"If set, we will attempt to update the gateway to the specified version. " +
"If left blank, the gateway software version will continue to be managed through the aviatrix_controller_config resource.",
},
"peering_ha_software_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "peering_ha_software_version can be used to set the desired software version of the HA gateway. " +
"If set, we will attempt to update the gateway to the specified version. " +
"If left blank, the gateway software version will continue to be managed through the aviatrix_controller_config resource.",
},
"image_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "image_version can be used to set the desired image version of the gateway. " +
"If set, we will attempt to update the gateway to the specified version.",
},
"peering_ha_image_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "peering_ha_image_version can be used to set the desired image version of the HA gateway. " +
"If set, we will attempt to update the gateway to the specified version.",
},
"elb_dns_name": {
Type: schema.TypeString,
Computed: true,
Description: "ELB DNS Name.",
},
"security_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "Security group used for the gateway.",
},
"peering_ha_security_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "Peering HA security group used for the gateway.",
},
"public_dns_server": {
Type: schema.TypeString,
Computed: true,
Description: "NS server used by the gateway.",
},
"cloud_instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance ID of the gateway.",
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Description: "Private IP address of the Gateway created.",
},
"peering_ha_cloud_instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance ID of the peering HA gateway.",
},
"peering_ha_gw_name": {
Type: schema.TypeString,
Computed: true,
Description: "Aviatrix gateway unique name of HA gateway.",
},
"peering_ha_private_ip": {
Type: schema.TypeString,
Computed: true,
Description: "Private IP address of HA gateway.",
},
"fqdn_lan_interface": {
Type: schema.TypeString,
Computed: true,
Description: "FQDN gateway lan interface id.",
},
},
}
}
func resourceAviatrixGatewayCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
gateway := &goaviatrix.Gateway{
CloudType: d.Get("cloud_type").(int),
GwName: d.Get("gw_name").(string),
AccountName: d.Get("account_name").(string),
VpcID: d.Get("vpc_id").(string),
VpcNet: d.Get("subnet").(string),
VpcSize: d.Get("gw_size").(string),
VpnCidr: d.Get("vpn_cidr").(string),
ElbName: d.Get("elb_name").(string),
MaxConn: d.Get("max_vpn_conn").(string),
OtpMode: d.Get("otp_mode").(string),
OktaToken: d.Get("okta_token").(string),
OktaURL: d.Get("okta_url").(string),
OktaUsernameSuffix: d.Get("okta_username_suffix").(string),
DuoIntegrationKey: d.Get("duo_integration_key").(string),
DuoSecretKey: d.Get("duo_secret_key").(string),
DuoAPIHostname: d.Get("duo_api_hostname").(string),
DuoPushMode: d.Get("duo_push_mode").(string),
LdapServer: d.Get("ldap_server").(string),
LdapBindDn: d.Get("ldap_bind_dn").(string),
LdapPassword: d.Get("ldap_password").(string),
LdapBaseDn: d.Get("ldap_base_dn").(string),
LdapUserAttr: d.Get("ldap_username_attribute").(string),
AdditionalCidrs: d.Get("additional_cidrs").(string),
NameServers: d.Get("name_servers").(string),
SearchDomains: d.Get("search_domains").(string),
Eip: d.Get("eip").(string),
SaveTemplate: "no",
AvailabilityDomain: d.Get("availability_domain").(string),
FaultDomain: d.Get("fault_domain").(string),
}
err := checkPublicSubnetFilteringConfig(d)
if err != nil {
return err
}
if d.Get("enable_public_subnet_filtering").(bool) {
var routeTables []string
for _, v := range d.Get("public_subnet_filtering_route_tables").(*schema.Set).List() {
routeTables = append(routeTables, v.(string))
}
gateway.RouteTable = strings.Join(routeTables, ",")
gateway.VpcNet = fmt.Sprintf("%s~~%s", d.Get("subnet").(string), d.Get("zone").(string))
}
fqdnLanCidr := d.Get("fqdn_lan_cidr").(string)
fqdnLanVpcID := d.Get("fqdn_lan_vpc_id").(string)
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.GCPRelatedCloudTypes) && fqdnLanVpcID != "" {
return fmt.Errorf("attribute 'fqdn_lan_vpc_id' is only valid for GCP FQDN Gateways")
}
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.GCPRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes) && fqdnLanCidr != "" {
return fmt.Errorf("attribute 'fqdn_lan_cidr' is only valid for GCP and Azure FQDN Gateways")
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.GCPRelatedCloudTypes) {
if (fqdnLanCidr != "" && fqdnLanVpcID == "") || (fqdnLanCidr == "" && fqdnLanVpcID != "") {
return fmt.Errorf("to create a GCP FQDN gateway, both 'fqdn_lan_cidr' and 'fqdn_lan_vpc_id' must be set")
}
if fqdnLanCidr != "" && fqdnLanVpcID != "" {
gateway.LanVpcID = fqdnLanVpcID
gateway.LanPrivateSubnet = fqdnLanCidr
gateway.CreateFQDNGateway = true
}
} else if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AzureArmRelatedCloudTypes) {
if fqdnLanCidr != "" {
gateway.FqdnLanCidr = fqdnLanCidr
}
}
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AzureArmRelatedCloudTypes) && !d.Get("enable_public_subnet_filtering").(bool) && d.Get("zone").(string) != "" {
return fmt.Errorf("attribute 'zone' is only valid for Azure, Azure GOV, Azure China or Public Subnet Filtering Gateways")
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AzureArmRelatedCloudTypes) && d.Get("zone").(string) != "" {
gateway.VpcNet = fmt.Sprintf("%s~~%s~~", d.Get("subnet").(string), d.Get("zone").(string))
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes|goaviatrix.OCIRelatedCloudTypes|goaviatrix.AliCloudRelatedCloudTypes) {
gateway.VpcRegion = d.Get("vpc_reg").(string)
} else if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.GCPRelatedCloudTypes) {
// for gcp, rest api asks for "zone" rather than vpc region
gateway.Zone = d.Get("vpc_reg").(string)
} else {
return fmt.Errorf("invalid cloud type, it can only be AWS (1), GCP (4), Azure (8), OCI (16), AzureGov (32), AWSGov (256), AWSChina (1024), AzureChina (2048), Alibaba Cloud (8192), AWS Top Secret (16384) or AWS Secret (32768)")
}
singleIpNat := d.Get("single_ip_snat").(bool)
if singleIpNat {
gateway.EnableNat = "yes"
} else {
gateway.EnableNat = "no"
}
allocateNewEip := d.Get("allocate_new_eip").(bool)
if allocateNewEip {
gateway.AllocateNewEip = "on"
} else {
gateway.AllocateNewEip = "off"
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes|goaviatrix.GCPRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes|goaviatrix.OCIRelatedCloudTypes) {
return fmt.Errorf("failed to create transit gateway: 'allocate_new_eip' can only be set to 'false' when cloud_type is AWS (1), GCP (4), Azure (8), OCI (16), AzureGov (32), AWSGov (256), AWSChina (1024), AzureChina (2048) or AWS Top Secret (16384)")
}
if _, ok := d.GetOk("eip"); !ok {
return fmt.Errorf("failed to create gateway: 'eip' must be set when 'allocate_new_eip' is false")
}
azureEipName, azureEipNameOk := d.GetOk("azure_eip_name_resource_group")
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AzureArmRelatedCloudTypes) {
// AVX-9874 Azure EIP has a different format e.g. 'test_ip:rg:104.45.186.20'
if !azureEipNameOk {
return fmt.Errorf("failed to create gateway: 'azure_eip_name_resource_group' must be set when 'allocate_new_eip' is false and cloud_type is Azure (8), AzureGov (32) or AzureChina (2048)")
}
gateway.Eip = fmt.Sprintf("%s:%s", azureEipName.(string), d.Get("eip").(string))
} else {
if azureEipNameOk {
return fmt.Errorf("failed to create gateway: 'azure_eip_name_resource_group' must be empty when cloud_type is not one of Azure (8), AzureGov (32) or AzureChina (2048)")
}
gateway.Eip = d.Get("eip").(string)
}
}
insaneMode := d.Get("insane_mode").(bool)
if insaneMode {
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes) {
return fmt.Errorf("insane_mode is only supported for AWS (1), Azure (8), AzureGov (32), AWSGov (256), AWS China (1024), AzureChina (2048), AWS Top Secret (16384) and AWS Secret (32768)")
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
if d.Get("insane_mode_az").(string) == "" {
return fmt.Errorf("insane_mode_az needed if insane_mode is enabled for AWS (1), AWSGov (256), AWS China(1024), AWS Top Secret (16384) or AWS Secret (32768)")
}
if d.Get("peering_ha_subnet").(string) != "" && d.Get("peering_ha_insane_mode_az").(string) == "" {
return fmt.Errorf("peering_ha_insane_mode_az needed if insane_mode is enabled for AWS (1), AWSGov (256), AWS China(1024), AWS Top Secret (16384) or AWS Secret (32768) and ha_subnet is set")
}
// Append availability zone to subnet
var strs []string
insaneModeAz := d.Get("insane_mode_az").(string)
strs = append(strs, gateway.VpcNet, insaneModeAz)
gateway.VpcNet = strings.Join(strs, "~~")
}
gateway.InsaneMode = "yes"
} else {
gateway.InsaneMode = "no"
}
samlEnabled := d.Get("saml_enabled").(bool)
if samlEnabled {
gateway.SamlEnabled = "yes"
} else {
gateway.SamlEnabled = "no"
}
splitTunnel := d.Get("split_tunnel").(bool)
if splitTunnel {
gateway.SplitTunnel = "yes"
} else {
gateway.SplitTunnel = "no"
}
enableElb := d.Get("enable_elb").(bool)
if enableElb {
gateway.EnableElb = "yes"
}
gateway.EnableLdap = d.Get("enable_ldap").(bool)
vpnStatus := d.Get("vpn_access").(bool)
vpnProtocol := d.Get("vpn_protocol").(string)
if vpnStatus {
gateway.VpnStatus = "yes"
if enableElb && goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
gateway.VpnProtocol = vpnProtocol
} else if enableElb && vpnProtocol == "UDP" && !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
return fmt.Errorf("'UDP' for VPN gateway with ELB is only supported by AWS provider")
} else if !enableElb && vpnProtocol == "TCP" {
return fmt.Errorf("'vpn_protocol' should be left empty or set to 'UDP' for vpn gateway of AWS provider without elb enabled")
}
if gateway.SamlEnabled == "yes" {
if gateway.EnableLdap || gateway.OtpMode != "" {
return fmt.Errorf("ldap and mfa can't be configured if saml is enabled")
}
}
if gateway.OtpMode != "" && gateway.OtpMode != "2" && gateway.OtpMode != "3" {
return fmt.Errorf("otp_mode can only be '2' or '3' or empty string")
}
if gateway.EnableLdap && gateway.OtpMode == "3" {
return fmt.Errorf("ldap can't be configured along with okta authentication")
}
if gateway.EnableLdap {
if gateway.LdapServer == "" {
return fmt.Errorf("ldap server must be set if ldap is enabled")
}
if gateway.LdapBindDn == "" {
return fmt.Errorf("ldap bind dn must be set if ldap is enabled")
}
if gateway.LdapPassword == "" {
return fmt.Errorf("ldap password must be set if ldap is enabled")
}
if gateway.LdapBaseDn == "" {
return fmt.Errorf("ldap base dn must be set if ldap is enabled")
}
if gateway.LdapUserAttr == "" {
return fmt.Errorf("ldap user attribute must be set if ldap is enabled")
}
}
if gateway.OtpMode == "2" {
if gateway.DuoIntegrationKey == "" {
return fmt.Errorf("duo integration key required if otp_mode set to 2")
}
if gateway.DuoSecretKey == "" {
return fmt.Errorf("duo secret key required if otp_mode set to 2")
}
if gateway.DuoAPIHostname == "" {
return fmt.Errorf("duo api hostname required if otp_mode set to 2")
}
if gateway.DuoPushMode != "auto" && gateway.DuoPushMode != "token" && gateway.DuoPushMode != "selective" {
return fmt.Errorf("duo push mode must be set to a valid value (auto, selective, or token)")
}
gateway.AuthMethod = "DUO"
} else if gateway.OtpMode == "3" {
if gateway.OktaToken == "" {
return fmt.Errorf("okta token must be set if otp_mode is set to 3")
}
if gateway.OktaURL == "" {
return fmt.Errorf("okta url must be set if otp_mode is set to 3")
}
gateway.AuthMethod = "okta"
}
} else {
gateway.VpnStatus = "no"
if gateway.EnableElb == "yes" {
return fmt.Errorf("can not enable elb without VPN access enabled")
}
if vpnProtocol != "" {
return fmt.Errorf("'vpn_protocol' should be left empty for non-vpn gateway")
}
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.OCIRelatedCloudTypes) && (gateway.AvailabilityDomain == "" || gateway.FaultDomain == "") {
return fmt.Errorf("'availability_domain' and 'fault_domain' are required for OCI")
}
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.OCIRelatedCloudTypes) && (gateway.AvailabilityDomain != "" || gateway.FaultDomain != "") {
return fmt.Errorf("'availability_domain' and 'fault_domain' are only valid for OCI")
}
peeringHaGwSize := d.Get("peering_ha_gw_size").(string)
peeringHaSubnet := d.Get("peering_ha_subnet").(string)
peeringHaZone := d.Get("peering_ha_zone").(string)
peeringHaAvailabilityDomain := d.Get("peering_ha_availability_domain").(string)
peeringHaFaultDomain := d.Get("peering_ha_fault_domain").(string)
if peeringHaZone != "" && !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.GCPRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes) && !d.Get("enable_public_subnet_filtering").(bool) {
return fmt.Errorf("'peering_ha_zone' is only valid for GCP, Azure and Public Subnet Filtering Gateway if enabling Peering HA")
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.GCPRelatedCloudTypes) && peeringHaZone == "" && peeringHaSubnet != "" {
return fmt.Errorf("'peering_ha_zone' must be set to enable Peering HA on GCP, " +
"cannot enable Peering HA with only 'peering_ha_subnet' enabled")
}
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AzureArmRelatedCloudTypes) && peeringHaZone != "" && peeringHaSubnet == "" {
return fmt.Errorf("'peering_ha_subnet' must be provided to enable HA on Azure, " +
"cannot enable HA with only 'peering_ha_zone'")
}
if peeringHaSubnet == "" && peeringHaZone == "" && peeringHaGwSize != "" {
return fmt.Errorf("'peering_ha_gw_size' is only required if enabling Peering HA")
}
if peeringHaSubnet != "" {
if goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.OCIRelatedCloudTypes) && (peeringHaAvailabilityDomain == "" || peeringHaFaultDomain == "") {
return fmt.Errorf("'peering_ha_availability_domain' and 'peering_ha_fault_domain' are required to enable Peering HA on OCI")
}
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.OCIRelatedCloudTypes) && (peeringHaAvailabilityDomain != "" || peeringHaFaultDomain != "") {
return fmt.Errorf("'peering_ha_availability_domain' and 'peering_ha_fault_domain' are only valid for OCI")
}
}
enableDesignatedGw := d.Get("enable_designated_gateway").(bool)
if enableDesignatedGw {
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
return fmt.Errorf("'designated_gateway' feature is only supported for AWS (1), AWSGov (256), AWSChina (1024), AWS Top Secret (16384) and AWS Secret (32768) providers")
}
if peeringHaSubnet != "" || peeringHaZone != "" {
return fmt.Errorf("can't enable HA for gateway with 'designated_gateway' enabled")
}
gateway.EnableDesignatedGateway = "true"
}
enableEncryptVolume := d.Get("enable_encrypt_volume").(bool)
customerManagedKeys := d.Get("customer_managed_keys").(string)
if enableEncryptVolume && !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
return fmt.Errorf("'enable_encrypt_volume' is only supported for AWS (1), AWSGov (256), AWSChina (1024), AWS Top Secret (16384) and AWS Secret (32768) providers")
}
if customerManagedKeys != "" {
if !enableEncryptVolume {
return fmt.Errorf("'customer_managed_keys' should be empty since Encrypt Volume is not enabled")
}
gateway.CustomerManagedKeys = customerManagedKeys
}
if !enableEncryptVolume && goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
gateway.EncVolume = "no"
}
enableMonitorSubnets := d.Get("enable_monitor_gateway_subnets").(bool)
var excludedInstances []string
for _, v := range d.Get("monitor_exclude_list").(*schema.Set).List() {
excludedInstances = append(excludedInstances, v.(string))
}
// Enable monitor gateway subnets does not work with AWSChina
if enableMonitorSubnets && !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes^goaviatrix.AWSChina) {
return fmt.Errorf("'enable_monitor_gateway_subnets' is only valid for AWS (1), AWSGov (256), AWS Top Secret (16384) or AWS Secret (32768)")
}
if !enableMonitorSubnets && len(excludedInstances) != 0 {
return fmt.Errorf("'monitor_exclude_list' must be empty if 'enable_monitor_gateway_subnets' is false")
}
_, tagsOk := d.GetOk("tags")
if tagsOk {
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes|goaviatrix.AzureArmRelatedCloudTypes) {
return errors.New("failed to create gateway: adding tags is only supported for AWS (1), Azure (8), AzureGov (32), AWSGov (256), AWSChina (1024), AzureChina (2048), AWS Top Secret (16384) and AWS Secret (32768)")
}
tagsMap, err := extractTags(d, gateway.CloudType)
if err != nil {
return fmt.Errorf("error creating tags for gateway: %v", err)
}
tagJson, err := TagsMapToJson(tagsMap)
if err != nil {
return fmt.Errorf("failed to add tags when creating gateway: %v", err)
}
gateway.TagJson = tagJson
}
enableSpotInstance := d.Get("enable_spot_instance").(bool)
spotPrice := d.Get("spot_price").(string)
if enableSpotInstance {
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
return fmt.Errorf("enable_spot_instance only supports AWS related cloud types")
}
gateway.EnableSpotInstance = true
gateway.SpotPrice = spotPrice
}
rxQueueSize := d.Get("rx_queue_size").(string)
if rxQueueSize != "" {
if !goaviatrix.IsCloudType(gateway.CloudType, goaviatrix.AWSRelatedCloudTypes) {
return fmt.Errorf("rx_queue_size only supports AWS related cloud types")
} else {
gateway.RxQueueSize = rxQueueSize
}
}
log.Printf("[INFO] Creating Aviatrix gateway: %#v", gateway)
d.SetId(gateway.GwName)
flag := false
defer resourceAviatrixGatewayReadIfRequired(d, meta, &flag)
if d.Get("enable_public_subnet_filtering").(bool) {
err := client.CreatePublicSubnetFilteringGateway(gateway)
if err != nil {
log.Printf("[INFO] failed to create public subnet filtering gateway: %#v", gateway)
return fmt.Errorf("could not create public subnet filtering gateway: %v", err)
}
if !d.Get("public_subnet_filtering_guard_duty_enforced").(bool) {
err = client.DisableGuardDutyEnforcement(gateway)
if err != nil {
return fmt.Errorf("could not disable guard duty enforcement for public subnet filtering gateway: %v", err)
}
}
} else {
err := client.CreateGateway(gateway)
if err != nil {
log.Printf("[INFO] failed to create Aviatrix gateway: %#v", gateway)
return fmt.Errorf("failed to create Aviatrix gateway: %s", err)
}
}
enableVpnNat := d.Get("enable_vpn_nat").(bool)
if vpnStatus {
if !enableVpnNat {
err := client.DisableVpnNat(gateway)
if err != nil {
return fmt.Errorf("failed to disable VPN NAT: %s", err)
}
}
} else if !enableVpnNat {
return fmt.Errorf("'enable_vpc_nat' is only supported for vpn gateway. Can't modify it for non-vpn gateway")
}
singleAZ := d.Get("single_az_ha").(bool)
if singleAZ && !d.Get("enable_public_subnet_filtering").(bool) {
singleAZGateway := &goaviatrix.Gateway{
GwName: d.Get("gw_name").(string),
SingleAZ: "enabled",
}
log.Printf("[INFO] Enable Single AZ GW HA: %#v", singleAZGateway)
err := client.EnableSingleAZGateway(singleAZGateway)
if err != nil {
return fmt.Errorf("failed to create single AZ GW HA: %s", err)