forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprovider.go
1320 lines (1187 loc) · 62.8 KB
/
provider.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
// © Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: MPL-2.0
package nsxt
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/http/httputil"
"net/url"
"os"
"regexp"
"strings"
"time"
tf_api "github.com/vmware/terraform-provider-nsxt/api/utl"
"github.com/vmware/terraform-provider-nsxt/nsxt/util"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
api "github.com/vmware/go-vmware-nsxt"
"github.com/vmware/vsphere-automation-sdk-go/runtime/core"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client/middleware/retry"
"github.com/vmware/vsphere-automation-sdk-go/runtime/security"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
"golang.org/x/exp/slices"
)
var defaultRetryOnStatusCodes = []int{400, 409, 429, 500, 503, 504}
// Provider configuration that is shared for policy and MP
type commonProviderConfig struct {
RemoteAuth bool
BearerToken string
ToleratePartialSuccess bool
MaxRetries int
MinRetryInterval int
MaxRetryInterval int
RetryStatusCodes []int
Username string
Password string
LicenseKeys []string
}
type nsxtClients struct {
CommonConfig commonProviderConfig
// NSX Manager client - based on go-vmware-nsxt SDK
NsxtClient *api.APIClient
// Config for the above client
NsxtClientConfig *api.Configuration
// Data for NSX Policy client - based on vsphere-automation-sdk-go SDK
// First offering of Policy SDK does not support concurrent
// operations in single connector. In order to avoid heavy locks,
// we are allocating connector per provider operation.
// TODO: when concurrency support is introduced policy client,
// change this code to allocate single connector for all provider
// operations.
PolicySecurityContext *core.SecurityContextImpl
PolicyHTTPClient *http.Client
Host string
PolicyEnforcementPoint string
PolicyGlobalManager bool
}
// Provider for VMWare NSX-T
func Provider() *schema.Provider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"allow_unverified_ssl": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_ALLOW_UNVERIFIED_SSL", false),
},
"username": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_USERNAME", nil),
},
"password": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_PASSWORD", nil),
Sensitive: true,
},
"remote_auth": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_REMOTE_AUTH", false),
},
"session_auth": {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_SESSION_AUTH", true),
},
"host": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_MANAGER_HOST", nil),
ValidateFunc: validateNsxtProviderHostFormat(),
Description: "The hostname or IP address of the NSX manager.",
},
"client_auth_cert_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_CLIENT_AUTH_CERT_FILE", nil),
},
"client_auth_key_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_CLIENT_AUTH_KEY_FILE", nil),
},
"ca_file": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_CA_FILE", nil),
},
"max_retries": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum number of HTTP client retries",
DefaultFunc: schema.EnvDefaultFunc("NSXT_MAX_RETRIES", 4),
},
"retry_min_delay": {
Type: schema.TypeInt,
Optional: true,
Description: "Minimum delay in milliseconds between retries of a request",
DefaultFunc: schema.EnvDefaultFunc("NSXT_RETRY_MIN_DELAY", 0),
},
"retry_max_delay": {
Type: schema.TypeInt,
Optional: true,
Description: "Maximum delay in milliseconds between retries of a request",
DefaultFunc: schema.EnvDefaultFunc("NSXT_RETRY_MAX_DELAY", 500),
},
"retry_on_status_codes": {
Type: schema.TypeList,
Optional: true,
Description: "HTTP replies status codes to retry on",
Elem: &schema.Schema{
Type: schema.TypeInt,
},
// There is no support for default values/func for list, so it will be handled later
},
"tolerate_partial_success": {
Type: schema.TypeBool,
Optional: true,
Description: "Treat partial success status as success",
DefaultFunc: schema.EnvDefaultFunc("NSXT_TOLERATE_PARTIAL_SUCCESS", false),
},
"vmc_auth_host": {
Type: schema.TypeString,
Optional: true,
Description: "URL for VMC authorization service (CSP)",
DefaultFunc: schema.EnvDefaultFunc("NSXT_VMC_AUTH_HOST", nil),
},
"vmc_token": {
Type: schema.TypeString,
Optional: true,
Description: "Long-living API token for VMC authorization",
DefaultFunc: schema.EnvDefaultFunc("NSXT_VMC_TOKEN", nil),
ConflictsWith: []string{"vmc_client_id", "vmc_client_secret"},
},
"vmc_client_id": {
Type: schema.TypeString,
Optional: true,
Description: "ID of OAuth App associated with the VMC organization",
DefaultFunc: schema.EnvDefaultFunc("NSXT_VMC_CLIENT_ID", nil),
ConflictsWith: []string{"vmc_token"},
RequiredWith: []string{"vmc_client_secret"},
},
"vmc_client_secret": {
Type: schema.TypeString,
Optional: true,
Description: "Secret of OAuth App associated with the VMC organization",
DefaultFunc: schema.EnvDefaultFunc("NSXT_VMC_CLIENT_SECRET", nil),
ConflictsWith: []string{"vmc_token"},
RequiredWith: []string{"vmc_client_id"},
},
"vmc_auth_mode": {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_VMC_AUTH_MODE", "Default"),
ValidateFunc: validation.StringInSlice([]string{"Default", "Bearer", "Basic"}, false),
Description: "Mode for VMC authorization",
},
"enforcement_point": {
Type: schema.TypeString,
Optional: true,
Description: "Enforcement Point for NSXT Policy",
DefaultFunc: schema.EnvDefaultFunc("NSXT_POLICY_ENFORCEMENT_POINT", "default"),
},
"global_manager": {
Type: schema.TypeBool,
Optional: true,
Description: "Is this a policy global manager endpoint",
DefaultFunc: schema.EnvDefaultFunc("NSXT_GLOBAL_MANAGER", false),
},
"license_keys": {
Type: schema.TypeList,
Optional: true,
Description: "license keys",
ConflictsWith: []string{"vmc_token", "vmc_client_id", "vmc_client_secret"},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(
"^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$"),
"Must be a valid nsx license key matching: ^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$"),
},
},
"client_auth_cert": {
Type: schema.TypeString,
Description: "Client certificate passed as string",
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_CLIENT_AUTH_CERT", nil),
},
"client_auth_key": {
Type: schema.TypeString,
Description: "Client certificate key passed as string",
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_CLIENT_AUTH_KEY", nil),
},
"ca": {
Type: schema.TypeString,
Description: "CA certificate passed as string",
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("NSXT_CA", nil),
},
"on_demand_connection": {
Type: schema.TypeBool,
Optional: true,
Description: "Avoid initializing NSX connection on startup",
DefaultFunc: schema.EnvDefaultFunc("NSXT_ON_DEMAND_CONNECTION", false),
},
},
DataSourcesMap: map[string]*schema.Resource{
"nsxt_provider_info": dataSourceNsxtProviderInfo(),
"nsxt_transport_zone": dataSourceNsxtTransportZone(),
"nsxt_switching_profile": removedDataSourceWrapper(dataSourceNsxtSwitchingProfile, "nsxt_switching_profile"),
"nsxt_logical_tier0_router": removedDataSourceWrapper(dataSourceNsxtLogicalTier0Router, "nsxt_logical_tier0_router"),
"nsxt_logical_tier1_router": removedDataSourceWrapper(dataSourceNsxtLogicalTier1Router, "nsxt_logical_tier1_router"),
"nsxt_mac_pool": removedDataSourceWrapper(dataSourceNsxtMacPool, "nsxt_mac_pool"),
"nsxt_ns_group": removedDataSourceWrapper(dataSourceNsxtNsGroup, "nsxt_ns_group"),
"nsxt_ns_groups": removedDataSourceWrapper(dataSourceNsxtNsGroups, "nsxt_ns_groups"),
"nsxt_ns_service": removedDataSourceWrapper(dataSourceNsxtNsService, "nsxt_ns_service"),
"nsxt_ns_services": removedDataSourceWrapper(dataSourceNsxtNsServices, "nsxt_ns_services"),
"nsxt_edge_cluster": dataSourceNsxtEdgeCluster(),
"nsxt_certificate": dataSourceNsxtCertificate(),
"nsxt_ip_pool": removedDataSourceWrapper(dataSourceNsxtIPPool, "nsxt_ip_pool"),
"nsxt_firewall_section": removedDataSourceWrapper(dataSourceNsxtFirewallSection, "nsxt_firewall_section"),
"nsxt_management_cluster": dataSourceNsxtManagementCluster(),
"nsxt_policy_edge_cluster": dataSourceNsxtPolicyEdgeCluster(),
"nsxt_policy_edge_node": dataSourceNsxtPolicyEdgeNode(),
"nsxt_policy_tier0_gateway": dataSourceNsxtPolicyTier0Gateway(),
"nsxt_policy_tier1_gateway": dataSourceNsxtPolicyTier1Gateway(),
"nsxt_policy_service": dataSourceNsxtPolicyService(),
"nsxt_policy_realization_info": dataSourceNsxtPolicyRealizationInfo(),
"nsxt_policy_segment_realization": dataSourceNsxtPolicySegmentRealization(),
"nsxt_policy_transport_zone": dataSourceNsxtPolicyTransportZone(),
"nsxt_policy_ip_discovery_profile": dataSourceNsxtPolicyIPDiscoveryProfile(),
"nsxt_policy_spoofguard_profile": dataSourceNsxtPolicySpoofGuardProfile(),
"nsxt_policy_qos_profile": dataSourceNsxtPolicyQosProfile(),
"nsxt_policy_ipv6_ndra_profile": dataSourceNsxtPolicyIpv6NdraProfile(),
"nsxt_policy_ipv6_dad_profile": dataSourceNsxtPolicyIpv6DadProfile(),
"nsxt_policy_gateway_qos_profile": dataSourceNsxtPolicyGatewayQosProfile(),
"nsxt_policy_segment_security_profile": dataSourceNsxtPolicySegmentSecurityProfile(),
"nsxt_policy_mac_discovery_profile": dataSourceNsxtPolicyMacDiscoveryProfile(),
"nsxt_policy_vm": dataSourceNsxtPolicyVM(),
"nsxt_policy_vms": dataSourceNsxtPolicyVMs(),
"nsxt_policy_lb_app_profile": dataSourceNsxtPolicyLBAppProfile(),
"nsxt_policy_lb_client_ssl_profile": dataSourceNsxtPolicyLBClientSslProfile(),
"nsxt_policy_lb_server_ssl_profile": dataSourceNsxtPolicyLBServerSslProfile(),
"nsxt_policy_lb_monitor": dataSourceNsxtPolicyLBMonitor(),
"nsxt_policy_certificate": dataSourceNsxtPolicyCertificate(),
"nsxt_policy_lb_persistence_profile": dataSourceNsxtPolicyLbPersistenceProfile(),
"nsxt_policy_vni_pool": dataSourceNsxtPolicyVniPool(),
"nsxt_policy_ip_block": dataSourceNsxtPolicyIPBlock(),
"nsxt_policy_ip_pool": dataSourceNsxtPolicyIPPool(),
"nsxt_policy_site": dataSourceNsxtPolicySite(),
"nsxt_policy_gateway_policy": dataSourceNsxtPolicyGatewayPolicy(),
"nsxt_policy_security_policy": dataSourceNsxtPolicySecurityPolicy(),
"nsxt_policy_group": dataSourceNsxtPolicyGroup(),
"nsxt_policy_tags": dataSourceNsxtTags(),
"nsxt_policy_context_profile": dataSourceNsxtPolicyContextProfile(),
"nsxt_policy_dhcp_server": dataSourceNsxtPolicyDhcpServer(),
"nsxt_policy_bfd_profile": dataSourceNsxtPolicyBfdProfile(),
"nsxt_policy_intrusion_service_profile": dataSourceNsxtPolicyIntrusionServiceProfile(),
"nsxt_policy_lb_service": dataSourceNsxtPolicyLbService(),
"nsxt_policy_gateway_locale_service": dataSourceNsxtPolicyGatewayLocaleService(),
"nsxt_policy_bridge_profile": dataSourceNsxtPolicyBridgeProfile(),
"nsxt_policy_ipsec_vpn_local_endpoint": dataSourceNsxtPolicyIPSecVpnLocalEndpoint(),
"nsxt_policy_ipsec_vpn_service": dataSourceNsxtPolicyIPSecVpnService(),
"nsxt_policy_l2_vpn_service": dataSourceNsxtPolicyL2VpnService(),
"nsxt_policy_segment": dataSourceNsxtPolicySegment(),
"nsxt_policy_project": dataSourceNsxtPolicyProject(),
"nsxt_policy_gateway_dns_forwarder": dataSourceNsxtPolicyGatewayDNSForwarder(),
"nsxt_policy_gateway_prefix_list": dataSourceNsxtPolicyGatewayPrefixList(),
"nsxt_policy_gateway_route_map": dataSourceNsxtPolicyGatewayRouteMap(),
"nsxt_policy_uplink_host_switch_profile": dataSourceNsxtUplinkHostSwitchProfile(),
"nsxt_policy_host_transport_node_collection": dataSourceNsxtPolicyHostTransportNodeCollection(),
"nsxt_policy_host_transport_node_collection_realization": dataSourceNsxtPolicyHostTransportNodeCollectionRealization(),
"nsxt_compute_manager": dataSourceNsxtComputeManager(),
"nsxt_transport_node_realization": dataSourceNsxtTransportNodeRealization(),
"nsxt_failure_domain": dataSourceNsxtFailureDomain(),
"nsxt_compute_collection": dataSourceNsxtComputeCollection(),
"nsxt_compute_manager_realization": dataSourceNsxtComputeManagerRealization(),
"nsxt_policy_host_transport_node": dataSourceNsxtPolicyHostTransportNode(),
"nsxt_manager_cluster_node": dataSourceNsxtManagerClusterNode(),
"nsxt_policy_host_transport_node_profile": dataSourceNsxtPolicyHostTransportNodeProfile(),
"nsxt_transport_node": dataSourceNsxtTransportNode(),
"nsxt_discovered_node": dataSourceNsxtDiscoveredNode(),
"nsxt_edge_upgrade_group": dataSourceNsxtEdgeUpgradeGroup(),
"nsxt_host_upgrade_group": dataSourceNsxtHostUpgradeGroup(),
"nsxt_policy_gateway_interface_realization": dataSourceNsxtPolicyGatewayInterfaceRealization(),
"nsxt_upgrade_postcheck": dataSourceNsxtUpgradePostCheck(),
"nsxt_upgrade_prepare_ready": dataSourceNsxtUpgradePrepareReady(),
"nsxt_policy_vtep_ha_host_switch_profile": dataSourceNsxtVtepHAHostSwitchProfile(),
"nsxt_policy_distributed_flood_protection_profile": dataSourceNsxtPolicyDistributedFloodProtectionProfile(),
"nsxt_policy_gateway_flood_protection_profile": dataSourceNsxtPolicyGatewayFloodProtectionProfile(),
"nsxt_manager_info": dataSourceNsxtManagerInfo(),
"nsxt_vpc": dataSourceNsxtVPC(),
"nsxt_vpc_group": dataSourceNsxtVpcGroup(),
"nsxt_vpc_nat": dataSourceNsxtVpcNat(),
"nsxt_vpc_subnet": dataSourceNsxtVpcSubnet(),
"nsxt_vpc_subnet_port": dataSourceNsxtVpcSubnetPort(),
"nsxt_vpc_service_profile": dataSourceNsxtVpcServiceProfile(),
"nsxt_vpc_connectivity_profile": dataSourceNsxtVpcConnectivityProfile(),
"nsxt_policy_transit_gateway": dataSourceNsxtPolicyTransitGateway(),
"nsxt_policy_transit_gateway_nat": dataSourceNsxtPolicyTransitGatewayNat(),
"nsxt_policy_project_ip_address_allocation": dataSourceNsxtProjectIpAddressAllocation(),
"nsxt_vpc_ip_address_allocation": dataSourceNsxtVpcIpAddressAllocation(),
"nsxt_policy_gateway_connection": dataSourceNsxtPolicyGatewayConnection(),
"nsxt_policy_distributed_vlan_connection": dataSourceNsxtPolicyDistributedVlanConnection(),
"nsxt_policy_services": dataSourceNsxtPolicyServices(),
"nsxt_policy_groups": dataSourceNsxtPolicyGroups(),
"nsxt_policy_edge_transport_node": dataSourceNsxtPolicyEdgeTransportNode(),
},
ResourcesMap: map[string]*schema.Resource{
"nsxt_dhcp_relay_profile": removedResourceWrapper(resourceNsxtDhcpRelayProfile, "nsxt_dhcp_relay_profile"),
"nsxt_dhcp_relay_service": removedResourceWrapper(resourceNsxtDhcpRelayService, "nsxt_dhcp_relay_service"),
"nsxt_dhcp_server_profile": removedResourceWrapper(resourceNsxtDhcpServerProfile, "nsxt_dhcp_server_profile"),
"nsxt_logical_dhcp_server": removedResourceWrapper(resourceNsxtLogicalDhcpServer, "nsxt_logical_dhcp_server"),
"nsxt_dhcp_server_ip_pool": removedResourceWrapper(resourceNsxtDhcpServerIPPool, "nsxt_dhcp_server_ip_pool"),
"nsxt_logical_switch": removedResourceWrapper(resourceNsxtLogicalSwitch, "nsxt_logical_switch"),
"nsxt_vlan_logical_switch": removedResourceWrapper(resourceNsxtVlanLogicalSwitch, "nsxt_vlan_logical_switch"),
"nsxt_logical_dhcp_port": removedResourceWrapper(resourceNsxtLogicalDhcpPort, "nsxt_logical_dhcp_port"),
"nsxt_logical_port": removedResourceWrapper(resourceNsxtLogicalPort, "nsxt_logical_port"),
"nsxt_logical_tier0_router": removedResourceWrapper(resourceNsxtLogicalTier0Router, "nsxt_logical_tier0_router"),
"nsxt_logical_tier1_router": removedResourceWrapper(resourceNsxtLogicalTier1Router, "nsxt_logical_tier1_router"),
"nsxt_logical_router_centralized_service_port": removedResourceWrapper(resourceNsxtLogicalRouterCentralizedServicePort, "nsxt_logical_router_centralized_service_port"),
"nsxt_logical_router_downlink_port": removedResourceWrapper(resourceNsxtLogicalRouterDownLinkPort, "nsxt_logical_router_downlink_port"),
"nsxt_logical_router_link_port_on_tier0": removedResourceWrapper(resourceNsxtLogicalRouterLinkPortOnTier0, "nsxt_logical_router_link_port_on_tier0"),
"nsxt_logical_router_link_port_on_tier1": removedResourceWrapper(resourceNsxtLogicalRouterLinkPortOnTier1, "nsxt_logical_router_link_port_on_tier1"),
"nsxt_ip_discovery_switching_profile": removedResourceWrapper(resourceNsxtIPDiscoverySwitchingProfile, "nsxt_ip_discovery_switching_profile"),
"nsxt_mac_management_switching_profile": removedResourceWrapper(resourceNsxtMacManagementSwitchingProfile, "nsxt_mac_management_switching_profile"),
"nsxt_qos_switching_profile": removedResourceWrapper(resourceNsxtQosSwitchingProfile, "nsxt_qos_switching_profile"),
"nsxt_spoofguard_switching_profile": removedResourceWrapper(resourceNsxtSpoofGuardSwitchingProfile, "nsxt_spoofguard_switching_profile"),
"nsxt_switch_security_switching_profile": removedResourceWrapper(resourceNsxtSwitchSecuritySwitchingProfile, "nsxt_switch_security_switching_profile"),
"nsxt_l4_port_set_ns_service": removedResourceWrapper(resourceNsxtL4PortSetNsService, "nsxt_l4_port_set_ns_service"),
"nsxt_algorithm_type_ns_service": removedResourceWrapper(resourceNsxtAlgorithmTypeNsService, "nsxt_algorithm_type_ns_service"),
"nsxt_icmp_type_ns_service": removedResourceWrapper(resourceNsxtIcmpTypeNsService, "nsxt_icmp_type_ns_service"),
"nsxt_igmp_type_ns_service": removedResourceWrapper(resourceNsxtIgmpTypeNsService, "nsxt_igmp_type_ns_service"),
"nsxt_ether_type_ns_service": removedResourceWrapper(resourceNsxtEtherTypeNsService, "nsxt_ether_type_ns_service"),
"nsxt_ip_protocol_ns_service": removedResourceWrapper(resourceNsxtIPProtocolNsService, "nsxt_ip_protocol_ns_service"),
"nsxt_ns_service_group": removedResourceWrapper(resourceNsxtNsServiceGroup, "nsxt_ns_service_group"),
"nsxt_ns_group": removedResourceWrapper(resourceNsxtNsGroup, "nsxt_ns_group"),
"nsxt_firewall_section": removedResourceWrapper(resourceNsxtFirewallSection, "nsxt_firewall_section"),
"nsxt_nat_rule": removedResourceWrapper(resourceNsxtNatRule, "nsxt_nat_rule"),
"nsxt_ip_block": removedResourceWrapper(resourceNsxtIPBlock, "nsxt_ip_block"),
"nsxt_ip_block_subnet": removedResourceWrapper(resourceNsxtIPBlockSubnet, "nsxt_ip_block_subnet"),
"nsxt_ip_pool": removedResourceWrapper(resourceNsxtIPPool, "nsxt_ip_pool"),
"nsxt_ip_pool_allocation_ip_address": removedResourceWrapper(resourceNsxtIPPoolAllocationIPAddress, "nsxt_ip_pool_allocation_ip_address"),
"nsxt_ip_set": removedResourceWrapper(resourceNsxtIPSet, "nsxt_ip_set"),
"nsxt_static_route": removedResourceWrapper(resourceNsxtStaticRoute, "nsxt_static_route"),
"nsxt_vm_tags": removedResourceWrapper(resourceNsxtVMTags, "nsxt_vm_tags"),
"nsxt_lb_icmp_monitor": removedResourceWrapper(resourceNsxtLbIcmpMonitor, "nsxt_lb_icmp_monitor"),
"nsxt_lb_tcp_monitor": removedResourceWrapper(resourceNsxtLbTCPMonitor, "nsxt_lb_tcp_monitor"),
"nsxt_lb_udp_monitor": removedResourceWrapper(resourceNsxtLbUDPMonitor, "nsxt_lb_udp_monitor"),
"nsxt_lb_http_monitor": removedResourceWrapper(resourceNsxtLbHTTPMonitor, "nsxt_lb_http_monitor"),
"nsxt_lb_https_monitor": removedResourceWrapper(resourceNsxtLbHTTPSMonitor, "nsxt_lb_https_monitor"),
"nsxt_lb_passive_monitor": removedResourceWrapper(resourceNsxtLbPassiveMonitor, "nsxt_lb_passive_monitor"),
"nsxt_lb_pool": removedResourceWrapper(resourceNsxtLbPool, "nsxt_lb_pool"),
"nsxt_lb_tcp_virtual_server": removedResourceWrapper(resourceNsxtLbTCPVirtualServer, "nsxt_lb_tcp_virtual_server"),
"nsxt_lb_udp_virtual_server": removedResourceWrapper(resourceNsxtLbUDPVirtualServer, "nsxt_lb_udp_virtual_server"),
"nsxt_lb_http_virtual_server": removedResourceWrapper(resourceNsxtLbHTTPVirtualServer, "nsxt_lb_http_virtual_server"),
"nsxt_lb_http_forwarding_rule": removedResourceWrapper(resourceNsxtLbHTTPForwardingRule, "nsxt_lb_http_forwarding_rule"),
"nsxt_lb_http_request_rewrite_rule": removedResourceWrapper(resourceNsxtLbHTTPRequestRewriteRule, "nsxt_lb_http_request_rewrite_rule"),
"nsxt_lb_http_response_rewrite_rule": removedResourceWrapper(resourceNsxtLbHTTPResponseRewriteRule, "nsxt_lb_http_response_rewrite_rule"),
"nsxt_lb_cookie_persistence_profile": removedResourceWrapper(resourceNsxtLbCookiePersistenceProfile, "nsxt_lb_cookie_persistence_profile"),
"nsxt_lb_source_ip_persistence_profile": removedResourceWrapper(resourceNsxtLbSourceIPPersistenceProfile, "nsxt_lb_source_ip_persistence_profile"),
"nsxt_lb_client_ssl_profile": removedResourceWrapper(resourceNsxtLbClientSslProfile, "nsxt_lb_client_ssl_profile"),
"nsxt_lb_server_ssl_profile": removedResourceWrapper(resourceNsxtLbServerSslProfile, "nsxt_lb_server_ssl_profile"),
"nsxt_lb_service": removedResourceWrapper(resourceNsxtLbService, "nsxt_lb_service"),
"nsxt_lb_fast_tcp_application_profile": removedResourceWrapper(resourceNsxtLbFastTCPApplicationProfile, "nsxt_lb_fast_tcp_application_profile"),
"nsxt_lb_fast_udp_application_profile": removedResourceWrapper(resourceNsxtLbFastUDPApplicationProfile, "nsxt_lb_fast_udp_application_profile"),
"nsxt_lb_http_application_profile": removedResourceWrapper(resourceNsxtLbHTTPApplicationProfile, "nsxt_lb_http_application_profile"),
"nsxt_policy_tier1_gateway": resourceNsxtPolicyTier1Gateway(),
"nsxt_policy_tier1_gateway_interface": resourceNsxtPolicyTier1GatewayInterface(),
"nsxt_policy_tier0_gateway": resourceNsxtPolicyTier0Gateway(),
"nsxt_policy_tier0_gateway_interface": resourceNsxtPolicyTier0GatewayInterface(),
"nsxt_policy_tier0_gateway_ha_vip_config": resourceNsxtPolicyTier0GatewayHAVipConfig(),
"nsxt_policy_group": resourceNsxtPolicyGroup(),
"nsxt_policy_domain": resourceNsxtPolicyDomain(),
"nsxt_policy_security_policy": resourceNsxtPolicySecurityPolicy(),
"nsxt_policy_service": resourceNsxtPolicyService(),
"nsxt_policy_gateway_policy": resourceNsxtPolicyGatewayPolicy(),
"nsxt_policy_predefined_gateway_policy": resourceNsxtPolicyPredefinedGatewayPolicy(),
"nsxt_policy_predefined_security_policy": resourceNsxtPolicyPredefinedSecurityPolicy(),
"nsxt_policy_segment": resourceNsxtPolicySegment(),
"nsxt_policy_vlan_segment": resourceNsxtPolicyVlanSegment(),
"nsxt_policy_fixed_segment": resourceNsxtPolicyFixedSegment(),
"nsxt_policy_static_route": resourceNsxtPolicyStaticRoute(),
"nsxt_policy_gateway_prefix_list": resourceNsxtPolicyGatewayPrefixList(),
"nsxt_policy_vm_tags": resourceNsxtPolicyVMTags(),
"nsxt_policy_nat_rule": resourceNsxtPolicyNATRule(),
"nsxt_policy_ip_block": resourceNsxtPolicyIPBlock(),
"nsxt_policy_lb_pool": resourceNsxtPolicyLBPool(),
"nsxt_policy_ip_pool": resourceNsxtPolicyIPPool(),
"nsxt_policy_ip_pool_block_subnet": resourceNsxtPolicyIPPoolBlockSubnet(),
"nsxt_policy_ip_pool_static_subnet": resourceNsxtPolicyIPPoolStaticSubnet(),
"nsxt_policy_lb_service": resourceNsxtPolicyLBService(),
"nsxt_policy_lb_virtual_server": resourceNsxtPolicyLBVirtualServer(),
"nsxt_policy_ip_address_allocation": resourceNsxtPolicyIPAddressAllocation(),
"nsxt_policy_bgp_neighbor": resourceNsxtPolicyBgpNeighbor(),
"nsxt_policy_bgp_config": resourceNsxtPolicyBgpConfig(),
"nsxt_policy_dhcp_relay": resourceNsxtPolicyDhcpRelayConfig(),
"nsxt_policy_dhcp_server": resourceNsxtPolicyDhcpServer(),
"nsxt_policy_context_profile": resourceNsxtPolicyContextProfile(),
"nsxt_policy_dhcp_v4_static_binding": resourceNsxtPolicyDhcpV4StaticBinding(),
"nsxt_policy_dhcp_v6_static_binding": resourceNsxtPolicyDhcpV6StaticBinding(),
"nsxt_policy_dns_forwarder_zone": resourceNsxtPolicyDNSForwarderZone(),
"nsxt_policy_gateway_dns_forwarder": resourceNsxtPolicyGatewayDNSForwarder(),
"nsxt_policy_gateway_community_list": resourceNsxtPolicyGatewayCommunityList(),
"nsxt_policy_gateway_route_map": resourceNsxtPolicyGatewayRouteMap(),
"nsxt_policy_intrusion_service_policy": resourceNsxtPolicyIntrusionServicePolicy(),
"nsxt_policy_static_route_bfd_peer": resourceNsxtPolicyStaticRouteBfdPeer(),
"nsxt_policy_intrusion_service_profile": resourceNsxtPolicyIntrusionServiceProfile(),
"nsxt_policy_evpn_tenant": resourceNsxtPolicyEvpnTenant(),
"nsxt_policy_evpn_config": resourceNsxtPolicyEvpnConfig(),
"nsxt_policy_evpn_tunnel_endpoint": resourceNsxtPolicyEvpnTunnelEndpoint(),
"nsxt_policy_vni_pool": resourceNsxtPolicyVniPool(),
"nsxt_policy_qos_profile": resourceNsxtPolicyQosProfile(),
"nsxt_policy_ospf_config": resourceNsxtPolicyOspfConfig(),
"nsxt_policy_ospf_area": resourceNsxtPolicyOspfArea(),
"nsxt_policy_gateway_redistribution_config": resourceNsxtPolicyGatewayRedistributionConfig(),
"nsxt_policy_mac_discovery_profile": resourceNsxtPolicyMacDiscoveryProfile(),
"nsxt_policy_ipsec_vpn_ike_profile": resourceNsxtPolicyIPSecVpnIkeProfile(),
"nsxt_policy_ipsec_vpn_tunnel_profile": resourceNsxtPolicyIPSecVpnTunnelProfile(),
"nsxt_policy_ipsec_vpn_dpd_profile": resourceNsxtPolicyIPSecVpnDpdProfile(),
"nsxt_policy_ipsec_vpn_session": resourceNsxtPolicyIPSecVpnSession(),
"nsxt_policy_l2_vpn_session": resourceNsxtPolicyL2VPNSession(),
"nsxt_policy_ipsec_vpn_service": resourceNsxtPolicyIPSecVpnService(),
"nsxt_policy_l2_vpn_service": resourceNsxtPolicyL2VpnService(),
"nsxt_policy_ipsec_vpn_local_endpoint": resourceNsxtPolicyIPSecVpnLocalEndpoint(),
"nsxt_policy_ip_discovery_profile": resourceNsxtPolicyIPDiscoveryProfile(),
"nsxt_policy_context_profile_custom_attribute": resourceNsxtPolicyContextProfileCustomAttribute(),
"nsxt_policy_segment_security_profile": resourceNsxtPolicySegmentSecurityProfile(),
"nsxt_policy_spoof_guard_profile": resourceNsxtPolicySpoofGuardProfile(),
"nsxt_policy_gateway_qos_profile": resourceNsxtPolicyGatewayQosProfile(),
"nsxt_policy_project": resourceNsxtPolicyProject(),
"nsxt_policy_transport_zone": resourceNsxtPolicyTransportZone(),
"nsxt_policy_user_management_role": resourceNsxtPolicyUserManagementRole(),
"nsxt_policy_user_management_role_binding": resourceNsxtPolicyUserManagementRoleBinding(),
"nsxt_policy_ldap_identity_source": resourceNsxtPolicyLdapIdentitySource(),
"nsxt_edge_cluster": resourceNsxtEdgeCluster(),
"nsxt_compute_manager": resourceNsxtComputeManager(),
"nsxt_manager_cluster": resourceNsxtManagerCluster(),
"nsxt_policy_uplink_host_switch_profile": resourceNsxtUplinkHostSwitchProfile(),
"nsxt_node_user": resourceNsxtUsers(),
"nsxt_principal_identity": resourceNsxtPrincipalIdentity(),
"nsxt_edge_transport_node": resourceNsxtEdgeTransportNode(),
"nsxt_failure_domain": resourceNsxtFailureDomain(),
"nsxt_cluster_virtual_ip": resourceNsxtClusterVirualIP(),
"nsxt_policy_host_transport_node_profile": resourceNsxtPolicyHostTransportNodeProfile(),
"nsxt_policy_host_transport_node": resourceNsxtPolicyHostTransportNode(),
"nsxt_edge_high_availability_profile": resourceNsxtEdgeHighAvailabilityProfile(),
"nsxt_policy_host_transport_node_collection": resourceNsxtPolicyHostTransportNodeCollection(),
"nsxt_policy_lb_client_ssl_profile": resourceNsxtPolicyLBClientSslProfile(),
"nsxt_policy_lb_server_ssl_profile": resourceNsxtPolicyLBServerSslProfile(),
"nsxt_policy_lb_http_application_profile": resourceNsxtPolicyLBHttpApplicationProfile(),
"nsxt_policy_lb_fast_tcp_application_profile": resourceNsxtPolicyLBFastTcpApplicationProfile(),
"nsxt_policy_lb_fast_udp_application_profile": resourceNsxtPolicyLBFastUdpApplicationProfile(),
"nsxt_policy_security_policy_rule": resourceNsxtPolicySecurityPolicyRule(),
"nsxt_policy_parent_security_policy": resourceNsxtPolicyParentSecurityPolicy(),
"nsxt_policy_firewall_exclude_list_member": resourceNsxtPolicyFirewallExcludeListMember(),
"nsxt_policy_lb_http_monitor_profile": resourceNsxtPolicyLBHttpMonitorProfile(),
"nsxt_policy_lb_https_monitor_profile": resourceNsxtPolicyLBHttpsMonitorProfile(),
"nsxt_policy_lb_icmp_monitor_profile": resourceNsxtPolicyLBIcmpMonitorProfile(),
"nsxt_policy_lb_passive_monitor_profile": resourceNsxtPolicyLBPassiveMonitorProfile(),
"nsxt_policy_lb_tcp_monitor_profile": resourceNsxtPolicyLBTcpMonitorProfile(),
"nsxt_policy_lb_udp_monitor_profile": resourceNsxtPolicyLBUdpMonitorProfile(),
"nsxt_policy_lb_cookie_persistence_profile": resourceNsxtPolicyLBCookiePersistenceProfile(),
"nsxt_policy_lb_source_ip_persistence_profile": resourceNsxtPolicyLBSourceIpPersistenceProfile(),
"nsxt_policy_lb_generic_persistence_profile": resourceNsxtPolicyLBGenericPersistenceProfile(),
"nsxt_policy_tier0_gateway_gre_tunnel": resourceNsxtPolicyTier0GatewayGRETunnel(),
"nsxt_upgrade_run": resourceNsxtUpgradeRun(),
"nsxt_upgrade_prepare": resourceNsxtUpgradePrepare(),
"nsxt_upgrade_precheck_acknowledge": resourceNsxtUpgradePrecheckAcknowledge(),
"nsxt_policy_vtep_ha_host_switch_profile": resourceNsxtVtepHAHostSwitchProfile(),
"nsxt_policy_site": resourceNsxtPolicySite(),
"nsxt_policy_global_manager": resourceNsxtPolicyGlobalManager(),
"nsxt_policy_metadata_proxy": resourceNsxtPolicyMetadataProxy(),
"nsxt_edge_transport_node_rtep": resourceNsxtEdgeTransportNodeRTEP(),
"nsxt_policy_distributed_flood_protection_profile": resourceNsxtPolicyDistributedFloodProtectionProfile(),
"nsxt_policy_distributed_flood_protection_profile_binding": resourceNsxtPolicyDistributedFloodProtectionProfileBinding(),
"nsxt_policy_gateway_flood_protection_profile": resourceNsxtPolicyGatewayFloodProtectionProfile(),
"nsxt_policy_gateway_flood_protection_profile_binding": resourceNsxtPolicyGatewayFloodProtectionProfileBinding(),
"nsxt_policy_compute_sub_cluster": resourceNsxtPolicyComputeSubCluster(),
"nsxt_policy_tier0_inter_vrf_routing": resourceNsxtPolicyTier0InterVRFRouting(),
"nsxt_vpc_security_policy": resourceNsxtVPCSecurityPolicy(),
"nsxt_vpc_group": resourceNsxtVPCGroup(),
"nsxt_vpc_gateway_policy": resourceNsxtVPCGatewayPolicy(),
"nsxt_vpc_service_profile": resourceNsxtVpcServiceProfile(),
"nsxt_vpc_connectivity_profile": resourceNsxtVpcConnectivityProfile(),
"nsxt_policy_transit_gateway": resourceNsxtPolicyTransitGateway(),
"nsxt_policy_share": resourceNsxtPolicyShare(),
"nsxt_policy_shared_resource": resourceNsxtPolicySharedResource(),
"nsxt_policy_gateway_connection": resourceNsxtPolicyGatewayConnection(),
"nsxt_policy_distributed_vlan_connection": resourceNsxtPolicyDistributedVlanConnection(),
"nsxt_vpc": resourceNsxtVpc(),
"nsxt_vpc_attachment": resourceNsxtVpcAttachment(),
"nsxt_vpc_nat_rule": resourceNsxtPolicyVpcNatRule(),
"nsxt_policy_transit_gateway_attachment": resourceNsxtPolicyTransitGatewayAttachment(),
"nsxt_vpc_external_address": resourceNsxtVpcExternalAddress(),
"nsxt_vpc_ip_address_allocation": resourceNsxtVpcIpAddressAllocation(),
"nsxt_vpc_subnet": resourceNsxtVpcSubnet(),
"nsxt_policy_transit_gateway_nat_rule": resourceNsxtPolicyTransitGatewayNatRule(),
"nsxt_vpc_static_route": resourceNsxtVpcStaticRoutes(),
"nsxt_policy_project_ip_address_allocation": resourceNsxtPolicyProjectIpAddressAllocation(),
"nsxt_vpc_dhcp_v4_static_binding": resourceNsxtVpcSubnetDhcpV4StaticBindingConfig(),
"nsxt_policy_l7_access_profile": resourceNsxtPolicyL7AccessProfile(),
"nsxt_policy_edge_transport_node": resourceNsxtPolicyEdgeTransportNode(),
"nsxt_policy_edge_high_availability_profile": resourceNsxtPolicyEdgeHighAvailabilityProfile(),
"nsxt_policy_edge_cluster": resourceNsxtPolicyEdgeCluster(),
},
ConfigureFunc: providerConfigure,
}
}
func isVMCCredentialSet(d *schema.ResourceData) bool {
// Refresh token
vmcToken := d.Get("vmc_token").(string)
if len(vmcToken) > 0 {
return true
}
// Oauth app
vmcClientID := d.Get("vmc_client_id").(string)
vmcClientSecret := d.Get("vmc_client_secret").(string)
if len(vmcClientSecret) > 0 && len(vmcClientID) > 0 {
return true
}
return false
}
func configureNsxtClient(d *schema.ResourceData, clients *nsxtClients) error {
onDemandConn := d.Get("on_demand_connection").(bool)
clientAuthCertFile := d.Get("client_auth_cert_file").(string)
clientAuthKeyFile := d.Get("client_auth_key_file").(string)
clientAuthCert := d.Get("client_auth_cert").(string)
clientAuthKey := d.Get("client_auth_key").(string)
vmcAuthMode := d.Get("vmc_auth_mode").(string)
if onDemandConn {
// On demand connection option is not supported with old SDK
return nil
}
if (vmcAuthMode == "Basic") || isVMCCredentialSet(d) {
// VMC can operate without token with basic auth, however MP API is not
// available for cloud admin user
return nil
}
needCreds := true
if len(clientAuthCertFile) > 0 {
if len(clientAuthKeyFile) == 0 {
return fmt.Errorf("Please provide key file for client certificate")
}
needCreds = false
}
if len(clientAuthCert) > 0 {
if len(clientAuthKey) == 0 {
return fmt.Errorf("Please provide key for client certificate")
}
// only supported for policy resources
needCreds = false
}
insecure := d.Get("allow_unverified_ssl").(bool)
username := d.Get("username").(string)
password := d.Get("password").(string)
// The correct place to escape special chars would be inside the SDK
// However since the SDK is deprecated, we implement escaping here
// TODO implement this functionality with new mp-sdk
username = url.QueryEscape(username)
password = url.QueryEscape(password)
if needCreds {
if username == "" {
return fmt.Errorf("username must be provided")
}
if password == "" {
return fmt.Errorf("password must be provided")
}
}
host := d.Get("host").(string)
// Remove schema
host = strings.TrimPrefix(host, "https://")
if host == "" {
return fmt.Errorf("host must be provided")
}
caFile := d.Get("ca_file").(string)
caString := d.Get("ca").(string)
sessionAuth := d.Get("session_auth").(bool)
skipSessionAuth := !sessionAuth
retriesConfig := api.ClientRetriesConfiguration{
MaxRetries: clients.CommonConfig.MaxRetries,
RetryMinDelay: clients.CommonConfig.MinRetryInterval,
RetryMaxDelay: clients.CommonConfig.MaxRetryInterval,
RetryOnStatuses: clients.CommonConfig.RetryStatusCodes,
}
clients.NsxtClientConfig = &api.Configuration{
BasePath: "/api/v1",
Host: host,
Scheme: "https",
UserAgent: "terraform-provider-nsxt",
UserName: username,
Password: password,
RemoteAuth: clients.CommonConfig.RemoteAuth,
ClientAuthCertFile: clientAuthCertFile,
ClientAuthKeyFile: clientAuthKeyFile,
CAFile: caFile,
ClientAuthCertString: clientAuthCert,
ClientAuthKeyString: clientAuthKey,
CAString: caString,
Insecure: insecure,
RetriesConfiguration: retriesConfig,
SkipSessionAuth: skipSessionAuth,
}
nsxClient, err := api.NewAPIClient(clients.NsxtClientConfig)
if err != nil {
return err
}
clients.NsxtClient = nsxClient
return nil
}
type jwtToken struct {
IDToken string `json:"id_token"`
TokenType string `json:"token_type"`
ExpiresIn int64 `json:"expires_in"`
Scope string `json:"scope"`
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
type vmcAuthInfo struct {
authHost string
authMode string
accessToken string
clientID string
clientSecret string
}
func getVmcAuthInfo(d *schema.ResourceData) *vmcAuthInfo {
vmcInfo := vmcAuthInfo{
authHost: d.Get("vmc_auth_host").(string),
authMode: d.Get("vmc_auth_mode").(string),
accessToken: d.Get("vmc_token").(string),
clientID: d.Get("vmc_client_id").(string),
clientSecret: d.Get("vmc_client_secret").(string),
}
if len(vmcInfo.authHost) > 0 {
return &vmcInfo
}
// Fill in default auth host + url based on auth method
if len(vmcInfo.accessToken) > 0 {
vmcInfo.authHost = "console.cloud.vmware.com/csp/gateway/am/api/auth/api-tokens/authorize"
} else if len(vmcInfo.clientSecret) > 0 && len(vmcInfo.clientID) > 0 {
vmcInfo.authHost = "console.cloud.vmware.com/csp/gateway/am/api/auth/authorize"
}
return &vmcInfo
}
func (v *vmcAuthInfo) IsZero() bool {
return len(v.accessToken) == 0 && len(v.clientID) == 0 && len(v.clientSecret) == 0
}
func (v *vmcAuthInfo) getAPIToken() (string, error) {
var req *http.Request
// Access token
if len(v.accessToken) > 0 {
payload := strings.NewReader("refresh_token=" + v.accessToken)
req, _ = http.NewRequest("POST", "https://"+v.authHost, payload)
}
// Oauth app
if len(v.clientSecret) > 0 && len(v.clientID) > 0 {
payload := strings.NewReader("grant_type=client_credentials")
req, _ = http.NewRequest("POST", "https://"+v.authHost, payload)
req.SetBasicAuth(v.clientID, v.clientSecret)
}
if req == nil {
return "", fmt.Errorf("invalid VMC auth input")
}
req.Header.Add("content-type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode != 200 {
b, _ := io.ReadAll(res.Body)
return "", fmt.Errorf("unexpected status code %d trying to get auth token. %s", res.StatusCode, string(b))
}
defer res.Body.Close()
token := jwtToken{}
err = json.NewDecoder(res.Body).Decode(&token)
if err != nil {
// Not fatal
log.Printf("[WARNING]: Failed to decode access token from response: %v", err)
}
return token.AccessToken, nil
}
func getConnectorTLSConfig(d *schema.ResourceData) (*tls.Config, error) {
insecure := d.Get("allow_unverified_ssl").(bool)
clientAuthCertFile := d.Get("client_auth_cert_file").(string)
clientAuthKeyFile := d.Get("client_auth_key_file").(string)
caFile := d.Get("ca_file").(string)
clientAuthCert := d.Get("client_auth_cert").(string)
clientAuthKey := d.Get("client_auth_key").(string)
caCert := d.Get("ca").(string)
tlsConfig := tls.Config{InsecureSkipVerify: insecure}
if len(clientAuthCertFile) > 0 {
// cert and key are passed via filesystem
if len(clientAuthKeyFile) == 0 {
return nil, fmt.Errorf("Please provide key file for client certificate")
}
cert, err := tls.LoadX509KeyPair(clientAuthCertFile, clientAuthKeyFile)
if err != nil {
return nil, fmt.Errorf("Failed to load client cert/key pair: %v", err)
}
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &cert, nil
}
}
if len(clientAuthCert) > 0 {
// cert and key are passed as strings
if len(clientAuthKey) == 0 {
return nil, fmt.Errorf("Please provide key for client certificate")
}
cert, err := tls.X509KeyPair([]byte(clientAuthCert), []byte(clientAuthKey))
if err != nil {
return nil, fmt.Errorf("Failed to load client cert/key pair: %v", err)
}
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &cert, nil
}
}
if len(caFile) > 0 {
caCert, err := os.ReadFile(caFile)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig.RootCAs = caCertPool
}
if len(caCert) > 0 {
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
tlsConfig.RootCAs = caCertPool
}
return &tlsConfig, nil
}
func configurePolicyConnectorData(d *schema.ResourceData, clients *nsxtClients) error {
onDemandConn := d.Get("on_demand_connection").(bool)
host := d.Get("host").(string)
username := d.Get("username").(string)
password := d.Get("password").(string)
clientAuthCertFile := d.Get("client_auth_cert_file").(string)
clientAuthCert := d.Get("client_auth_cert").(string)
clientAuthDefined := (len(clientAuthCertFile) > 0) || (len(clientAuthCert) > 0)
policyEnforcementPoint := d.Get("enforcement_point").(string)
policyGlobalManager := d.Get("global_manager").(bool)
vmcInfo := getVmcAuthInfo(d)
isVMC := false
if (vmcInfo.authMode == "Basic") || isVMCCredentialSet(d) {
isVMC = true
if onDemandConn {
return fmt.Errorf("on demand connection option is not supported with VMC")
}
}
if host == "" {
return fmt.Errorf("host must be provided")
}
if !strings.HasPrefix(host, "https://") {
host = fmt.Sprintf("https://%s", host)
}
securityContextNeeded := true
if clientAuthDefined && !clients.CommonConfig.RemoteAuth {
securityContextNeeded = false
}
if securityContextNeeded {
securityCtx, err := getConfiguredSecurityContext(clients, vmcInfo, username, password)
if err != nil {
return err
}
clients.PolicySecurityContext = securityCtx
}
tlsConfig, err := getConnectorTLSConfig(d)
if err != nil {
return err
}
tr := &http.Transport{
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: tlsConfig,
}
httpClient := http.Client{Transport: tr}
clients.PolicyHTTPClient = &httpClient
clients.Host = host
clients.PolicyEnforcementPoint = policyEnforcementPoint
clients.PolicyGlobalManager = policyGlobalManager
if onDemandConn {
// version init will happen on demand
return nil
}
if !isVMC {
err = configureLicenses(getStandalonePolicyConnector(*clients, true), clients.CommonConfig.LicenseKeys)
if err != nil {
return err
}
}
err = initNSXVersion(getStandalonePolicyConnector(*clients, true))
if err != nil && isVMC {
// In case version API does not work for VMC, we workaround by testing version-specific APIs
// TODO - remove this when /node/version API works for all auth methods on VMC
initNSXVersionVMC(*clients)
return nil
}
return err
}
func getConfiguredSecurityContext(clients *nsxtClients, vmcInfo *vmcAuthInfo, username string, password string) (*core.SecurityContextImpl, error) {
securityCtx := core.NewSecurityContextImpl()
if vmcInfo == nil || vmcInfo.IsZero() {
if username == "" {
return nil, fmt.Errorf("username must be provided")
}
if password == "" {
return nil, fmt.Errorf("password must be provided")
}
securityCtx.SetProperty(security.AUTHENTICATION_SCHEME_ID, security.USER_PASSWORD_SCHEME_ID)
securityCtx.SetProperty(security.USER_KEY, username)
securityCtx.SetProperty(security.PASSWORD_KEY, password)
} else {
apiToken, err := vmcInfo.getAPIToken()
if err != nil {
return nil, err
}
// We'll be sending Bearer token anyway even with scp-auth-token auth
// For now, node API is not working on VMC without Bearer token present
clients.CommonConfig.BearerToken = apiToken
if vmcInfo.authMode != "Bearer" {
securityCtx.SetProperty(security.AUTHENTICATION_SCHEME_ID, security.OAUTH_SCHEME_ID)
securityCtx.SetProperty(security.ACCESS_TOKEN, apiToken)
}
}
return securityCtx, nil
}
type customHeaderProcessor struct {
customHeaders *map[string]string
}
func (processor customHeaderProcessor) Process(req *http.Request) error {
for header, value := range *processor.customHeaders {
req.Header.Set(header, value)
}
return nil
}
func newCustomHeaderProcessor(customHeaders *map[string]string) *customHeaderProcessor {
return &customHeaderProcessor{customHeaders: customHeaders}
}
type remoteAuthHeaderProcessor struct {
}
func newRemoteAuthHeaderProcessor() *remoteAuthHeaderProcessor {
return &remoteAuthHeaderProcessor{}
}
func (processor remoteAuthHeaderProcessor) Process(req *http.Request) error {
oldAuthHeader := req.Header.Get("Authorization")
newAuthHeader := strings.Replace(oldAuthHeader, "Basic", "Remote", 1)
req.Header.Set("Authorization", newAuthHeader)
return nil
}
type logRequestProcessor struct {
}
func newLogRequestProcessor() *logRequestProcessor {
return &logRequestProcessor{}
}
func (processor logRequestProcessor) Process(req *http.Request) error {
reqDump, err := httputil.DumpRequestOut(req, true)
if err != nil {
log.Fatal(err)
}
// Replace sensitive information in HTTP headers
authHeaderRegexp := regexp.MustCompile(`(?i)Authorization:.*`)
cspHeaderRegexp := regexp.MustCompile(`(?i)Csp-Auth-Token:.*`)
replaced := authHeaderRegexp.ReplaceAllString(string(reqDump), "<Omitted Authorization header>")
replaced = cspHeaderRegexp.ReplaceAllString(replaced, "<Omitted Csp-Auth-Token header>")
log.Printf("Issuing request towards NSX:\n%s", replaced)
return nil
}
type logResponseAcceptor struct {
}
func newLogResponseAcceptor() *logResponseAcceptor {
return &logResponseAcceptor{}
}
func (processor logResponseAcceptor) Accept(req *http.Response) {
dumpResponse, err := httputil.DumpResponse(req, true)
if err != nil {
log.Fatal(err)
}
log.Printf("Received NSX response:\n%s", dumpResponse)
}
type bearerAuthHeaderProcessor struct {
Token string