From 5b876884c4264b33ab804cae4704bdbeae9cab5b Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Sun, 12 May 2024 17:40:53 +0300 Subject: [PATCH] Add VPC data source Signed-off-by: Kobi Samoray --- ...data_source_nsxt_policy_context_profile.go | 2 +- nsxt/data_source_nsxt_policy_dhcp_server.go | 2 +- ...cy_distributed_flood_protection_profile.go | 2 +- ...ource_nsxt_policy_gateway_dns_forwarder.go | 2 +- ...policy_gateway_flood_protection_profile.go | 2 +- ...xt_policy_gateway_interface_realization.go | 2 +- ...urce_nsxt_policy_gateway_locale_service.go | 2 +- .../data_source_nsxt_policy_gateway_policy.go | 2 +- ..._source_nsxt_policy_gateway_qos_profile.go | 2 +- nsxt/data_source_nsxt_policy_group.go | 2 +- ...e_nsxt_policy_intrusion_service_profile.go | 2 +- nsxt/data_source_nsxt_policy_ip_block.go | 2 +- nsxt/data_source_nsxt_policy_ip_block_test.go | 12 +- ...source_nsxt_policy_ip_discovery_profile.go | 2 +- nsxt/data_source_nsxt_policy_ip_pool.go | 2 +- ...ata_source_nsxt_policy_ipv6_dad_profile.go | 2 +- ...ta_source_nsxt_policy_ipv6_ndra_profile.go | 2 +- ...ource_nsxt_policy_mac_discovery_profile.go | 2 +- nsxt/data_source_nsxt_policy_project.go | 41 +++--- nsxt/data_source_nsxt_policy_qos_profile.go | 2 +- ...ata_source_nsxt_policy_realization_info.go | 2 +- ...data_source_nsxt_policy_security_policy.go | 2 +- nsxt/data_source_nsxt_policy_segment.go | 2 +- ..._source_nsxt_policy_segment_realization.go | 2 +- ...ce_nsxt_policy_segment_security_profile.go | 2 +- nsxt/data_source_nsxt_policy_service.go | 2 +- ...a_source_nsxt_policy_spoofguard_profile.go | 2 +- nsxt/data_source_nsxt_policy_tier1_gateway.go | 2 +- nsxt/data_source_nsxt_policy_vm.go | 2 +- nsxt/data_source_nsxt_policy_vms.go | 2 +- nsxt/data_source_nsxt_policy_vpc.go | 57 ++++++++ nsxt/data_source_policy_vpc_test.go | 127 ++++++++++++++++++ nsxt/policy_common.go | 4 +- nsxt/provider.go | 1 + nsxt/resource_nsxt_policy_context_profile.go | 2 +- ...policy_context_profile_custom_attribute.go | 2 +- nsxt/resource_nsxt_policy_dhcp_relay.go | 2 +- nsxt/resource_nsxt_policy_dhcp_server.go | 2 +- ...urce_nsxt_policy_dhcp_v4_static_binding.go | 2 +- ...urce_nsxt_policy_dhcp_v6_static_binding.go | 2 +- ...cy_distributed_flood_protection_profile.go | 2 +- ...ibuted_flood_protection_profile_binding.go | 2 +- ...resource_nsxt_policy_dns_forwarder_zone.go | 2 +- ...ource_nsxt_policy_gateway_dns_forwarder.go | 2 +- ...ateway_flood_protection_profile_binding.go | 2 +- nsxt/resource_nsxt_policy_group.go | 2 +- ...e_nsxt_policy_intrusion_service_profile.go | 2 +- ...ource_nsxt_policy_ip_address_allocation.go | 2 +- nsxt/resource_nsxt_policy_ip_block.go | 2 +- ...source_nsxt_policy_ip_discovery_profile.go | 2 +- nsxt/resource_nsxt_policy_ip_pool.go | 2 +- ...source_nsxt_policy_ip_pool_block_subnet.go | 2 +- ...ource_nsxt_policy_ip_pool_static_subnet.go | 2 +- ...ource_nsxt_policy_mac_discovery_profile.go | 2 +- nsxt/resource_nsxt_policy_nat_rule.go | 2 +- ...e_nsxt_policy_predefined_gateway_policy.go | 2 +- ..._nsxt_policy_predefined_security_policy.go | 4 +- nsxt/resource_nsxt_policy_qos_profile.go | 2 +- ...ce_nsxt_policy_segment_security_profile.go | 2 +- nsxt/resource_nsxt_policy_service.go | 2 +- ...esource_nsxt_policy_spoof_guard_profile.go | 2 +- nsxt/resource_nsxt_policy_static_route.go | 2 +- nsxt/resource_nsxt_policy_tier1_gateway.go | 2 +- ...rce_nsxt_policy_tier1_gateway_interface.go | 2 +- nsxt/resource_nsxt_policy_vm_tags.go | 2 +- nsxt/segment_common.go | 2 +- nsxt/utils.go | 27 +--- website/docs/d/policy_vpc.html.markdown | 45 +++++++ 68 files changed, 325 insertions(+), 111 deletions(-) create mode 100644 nsxt/data_source_nsxt_policy_vpc.go create mode 100644 nsxt/data_source_policy_vpc_test.go create mode 100644 website/docs/d/policy_vpc.html.markdown diff --git a/nsxt/data_source_nsxt_policy_context_profile.go b/nsxt/data_source_nsxt_policy_context_profile.go index c2e95a0e6..012acae31 100644 --- a/nsxt/data_source_nsxt_policy_context_profile.go +++ b/nsxt/data_source_nsxt_policy_context_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyContextProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_dhcp_server.go b/nsxt/data_source_nsxt_policy_dhcp_server.go index f99011aaf..7df02cfc7 100644 --- a/nsxt/data_source_nsxt_policy_dhcp_server.go +++ b/nsxt/data_source_nsxt_policy_dhcp_server.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyDhcpServer() *schema.Resource { "display_name": getDataSourceExtendedDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go b/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go index 176d1dc17..d8daafc1b 100644 --- a/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go +++ b/nsxt/data_source_nsxt_policy_distributed_flood_protection_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyDistributedFloodProtectionProfile() *schema.Resource { "display_name": getDataSourceExtendedDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go b/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go index ed95c8889..927d00cca 100644 --- a/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go +++ b/nsxt/data_source_nsxt_policy_gateway_dns_forwarder.go @@ -19,7 +19,7 @@ func dataSourceNsxtPolicyGatewayDNSForwarder() *schema.Resource { "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), "gateway_path": getPolicyPathSchema(false, false, "Gateway path"), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go b/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go index 95f432ec7..9e40cc2fa 100644 --- a/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go +++ b/nsxt/data_source_nsxt_policy_gateway_flood_protection_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyGatewayFloodProtectionProfile() *schema.Resource { "display_name": getDataSourceExtendedDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_gateway_interface_realization.go b/nsxt/data_source_nsxt_policy_gateway_interface_realization.go index c2ce61587..1b3b46e1a 100644 --- a/nsxt/data_source_nsxt_policy_gateway_interface_realization.go +++ b/nsxt/data_source_nsxt_policy_gateway_interface_realization.go @@ -22,7 +22,7 @@ func dataSourceNsxtPolicyGatewayInterfaceRealization() *schema.Resource { Schema: map[string]*schema.Schema{ "id": getDataSourceIDSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "gateway_path": { Type: schema.TypeString, Description: "The path for the gateway", diff --git a/nsxt/data_source_nsxt_policy_gateway_locale_service.go b/nsxt/data_source_nsxt_policy_gateway_locale_service.go index 47cf7ed36..459655b7f 100644 --- a/nsxt/data_source_nsxt_policy_gateway_locale_service.go +++ b/nsxt/data_source_nsxt_policy_gateway_locale_service.go @@ -28,7 +28,7 @@ func dataSourceNsxtPolicyGatewayLocaleService() *schema.Resource { Optional: true, Computed: true, }, - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_gateway_policy.go b/nsxt/data_source_nsxt_policy_gateway_policy.go index 93f3dae0a..7777e1527 100644 --- a/nsxt/data_source_nsxt_policy_gateway_policy.go +++ b/nsxt/data_source_nsxt_policy_gateway_policy.go @@ -30,7 +30,7 @@ func dataSourceNsxtPolicyGatewayPolicy() *schema.Resource { "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), "domain": getDataSourceDomainNameSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "category": { Type: schema.TypeString, Description: "Category", diff --git a/nsxt/data_source_nsxt_policy_gateway_qos_profile.go b/nsxt/data_source_nsxt_policy_gateway_qos_profile.go index 929590c20..5e2d7d12c 100644 --- a/nsxt/data_source_nsxt_policy_gateway_qos_profile.go +++ b/nsxt/data_source_nsxt_policy_gateway_qos_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyGatewayQosProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_group.go b/nsxt/data_source_nsxt_policy_group.go index 981723785..d61636ff0 100644 --- a/nsxt/data_source_nsxt_policy_group.go +++ b/nsxt/data_source_nsxt_policy_group.go @@ -17,7 +17,7 @@ func dataSourceNsxtPolicyGroup() *schema.Resource { "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), "domain": getDomainNameSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_intrusion_service_profile.go b/nsxt/data_source_nsxt_policy_intrusion_service_profile.go index d9b2cb404..a001d04c4 100644 --- a/nsxt/data_source_nsxt_policy_intrusion_service_profile.go +++ b/nsxt/data_source_nsxt_policy_intrusion_service_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyIntrusionServiceProfile() *schema.Resource { "display_name": getDataSourceExtendedDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_ip_block.go b/nsxt/data_source_nsxt_policy_ip_block.go index 62af64b64..90d80112f 100644 --- a/nsxt/data_source_nsxt_policy_ip_block.go +++ b/nsxt/data_source_nsxt_policy_ip_block.go @@ -22,7 +22,7 @@ func dataSourceNsxtPolicyIPBlock() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_ip_block_test.go b/nsxt/data_source_nsxt_policy_ip_block_test.go index 782bf5e0f..46c4e8a6b 100644 --- a/nsxt/data_source_nsxt_policy_ip_block_test.go +++ b/nsxt/data_source_nsxt_policy_ip_block_test.go @@ -42,7 +42,7 @@ func testAccDataSourceNsxtPolicyIPBlockBasic(t *testing.T, withContext bool, pre Steps: []resource.TestStep{ { PreConfig: func() { - if err := testAccDataSourceNsxtPolicyIPBlockCreate(name); err != nil { + if err := testAccDataSourceNsxtPolicyIPBlockCreate(name, newUUID(), "4001::/64", false); err != nil { t.Error(err) } }, @@ -57,7 +57,7 @@ func testAccDataSourceNsxtPolicyIPBlockBasic(t *testing.T, withContext bool, pre }) } -func testAccDataSourceNsxtPolicyIPBlockCreate(name string) error { +func testAccDataSourceNsxtPolicyIPBlockCreate(name, id, cidr string, isPrivate bool) error { connector, err := testAccGetPolicyConnector() if err != nil { return fmt.Errorf("Error during test client initialization: %v", err) @@ -66,15 +66,15 @@ func testAccDataSourceNsxtPolicyIPBlockCreate(name string) error { displayName := name description := name - cidr := "4001::/64" obj := model.IpAddressBlock{ Description: &description, DisplayName: &displayName, Cidr: &cidr, } - - // Generate a random ID for the resource - id := newUUID() + if isPrivate { + visibility := model.IpAddressBlock_VISIBILITY_PRIVATE + obj.Visibility = &visibility + } err = client.Patch(id, obj) if err != nil { diff --git a/nsxt/data_source_nsxt_policy_ip_discovery_profile.go b/nsxt/data_source_nsxt_policy_ip_discovery_profile.go index ed320ce21..2043f0748 100644 --- a/nsxt/data_source_nsxt_policy_ip_discovery_profile.go +++ b/nsxt/data_source_nsxt_policy_ip_discovery_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyIPDiscoveryProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_ip_pool.go b/nsxt/data_source_nsxt_policy_ip_pool.go index 3a378608e..e52e8251c 100644 --- a/nsxt/data_source_nsxt_policy_ip_pool.go +++ b/nsxt/data_source_nsxt_policy_ip_pool.go @@ -19,7 +19,7 @@ func dataSourceNsxtPolicyIPPool() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "realized_id": { Type: schema.TypeString, Description: "The ID of the realized resource", diff --git a/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go b/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go index d9302f02e..7bfdaf0c7 100644 --- a/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go +++ b/nsxt/data_source_nsxt_policy_ipv6_dad_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyIpv6DadProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go b/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go index 0bb066b3c..aae7d1dba 100644 --- a/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go +++ b/nsxt/data_source_nsxt_policy_ipv6_ndra_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyIpv6NdraProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_mac_discovery_profile.go b/nsxt/data_source_nsxt_policy_mac_discovery_profile.go index ecfaaeccb..8bce8ae8c 100644 --- a/nsxt/data_source_nsxt_policy_mac_discovery_profile.go +++ b/nsxt/data_source_nsxt_policy_mac_discovery_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyMacDiscoveryProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_project.go b/nsxt/data_source_nsxt_policy_project.go index 90c1e3ead..2e2a08d36 100644 --- a/nsxt/data_source_nsxt_policy_project.go +++ b/nsxt/data_source_nsxt_policy_project.go @@ -27,25 +27,7 @@ func dataSourceNsxtPolicyProject() *schema.Resource { Type: schema.TypeString, Optional: true, }, - "site_info": { - Type: schema.TypeList, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "edge_cluster_paths": { - Type: schema.TypeList, - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - Optional: true, - }, - "site_path": { - Type: schema.TypeString, - Optional: true, - }, - }, - }, - Optional: true, - }, + "site_info": getSiteInfoSchema(), "tier0_gateway_paths": { Type: schema.TypeList, Elem: &schema.Schema{ @@ -57,6 +39,27 @@ func dataSourceNsxtPolicyProject() *schema.Resource { } } +func getSiteInfoSchema() *schema.Schema { + return &schema.Schema{ + Type: schema.TypeList, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "edge_cluster_paths": { + Type: schema.TypeList, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + Optional: true, + }, + "site_path": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + Optional: true, + } +} func dataSourceNsxtPolicyProjectRead(d *schema.ResourceData, m interface{}) error { connector := getPolicyConnector(m) client := infra.NewProjectsClient(connector) diff --git a/nsxt/data_source_nsxt_policy_qos_profile.go b/nsxt/data_source_nsxt_policy_qos_profile.go index 94f5a01c0..11cb37a3e 100644 --- a/nsxt/data_source_nsxt_policy_qos_profile.go +++ b/nsxt/data_source_nsxt_policy_qos_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyQosProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_realization_info.go b/nsxt/data_source_nsxt_policy_realization_info.go index f07263c00..d438cf38d 100644 --- a/nsxt/data_source_nsxt_policy_realization_info.go +++ b/nsxt/data_source_nsxt_policy_realization_info.go @@ -27,7 +27,7 @@ func dataSourceNsxtPolicyRealizationInfo() *schema.Resource { Required: true, ValidateFunc: validatePolicyPath(), }, - "context": getContextSchema(), + "context": getContextSchema(false, false), "entity_type": { Type: schema.TypeString, Description: "The entity type of the realized resource", diff --git a/nsxt/data_source_nsxt_policy_security_policy.go b/nsxt/data_source_nsxt_policy_security_policy.go index 42acaa1b3..9a269120d 100644 --- a/nsxt/data_source_nsxt_policy_security_policy.go +++ b/nsxt/data_source_nsxt_policy_security_policy.go @@ -25,7 +25,7 @@ func dataSourceNsxtPolicySecurityPolicy() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "domain": getDataSourceDomainNameSchema(), "is_default": { Type: schema.TypeBool, diff --git a/nsxt/data_source_nsxt_policy_segment.go b/nsxt/data_source_nsxt_policy_segment.go index 05077817f..3c0a5396e 100644 --- a/nsxt/data_source_nsxt_policy_segment.go +++ b/nsxt/data_source_nsxt_policy_segment.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicySegment() *schema.Resource { "display_name": getDataSourceExtendedDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_segment_realization.go b/nsxt/data_source_nsxt_policy_segment_realization.go index 0e7dde479..8d1d3ac1a 100644 --- a/nsxt/data_source_nsxt_policy_segment_realization.go +++ b/nsxt/data_source_nsxt_policy_segment_realization.go @@ -22,7 +22,7 @@ func dataSourceNsxtPolicySegmentRealization() *schema.Resource { Schema: map[string]*schema.Schema{ "id": getDataSourceIDSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "path": { Type: schema.TypeString, Description: "The path for the policy segment", diff --git a/nsxt/data_source_nsxt_policy_segment_security_profile.go b/nsxt/data_source_nsxt_policy_segment_security_profile.go index 41c85802a..9120159a0 100644 --- a/nsxt/data_source_nsxt_policy_segment_security_profile.go +++ b/nsxt/data_source_nsxt_policy_segment_security_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicySegmentSecurityProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_service.go b/nsxt/data_source_nsxt_policy_service.go index a421b37dd..5e38c80c6 100644 --- a/nsxt/data_source_nsxt_policy_service.go +++ b/nsxt/data_source_nsxt_policy_service.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicyService() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_spoofguard_profile.go b/nsxt/data_source_nsxt_policy_spoofguard_profile.go index a29b3fe3f..ccacfc936 100644 --- a/nsxt/data_source_nsxt_policy_spoofguard_profile.go +++ b/nsxt/data_source_nsxt_policy_spoofguard_profile.go @@ -16,7 +16,7 @@ func dataSourceNsxtPolicySpoofGuardProfile() *schema.Resource { "display_name": getDataSourceDisplayNameSchema(), "description": getDataSourceDescriptionSchema(), "path": getPathSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_tier1_gateway.go b/nsxt/data_source_nsxt_policy_tier1_gateway.go index ad0f66ee1..7463d4ba2 100644 --- a/nsxt/data_source_nsxt_policy_tier1_gateway.go +++ b/nsxt/data_source_nsxt_policy_tier1_gateway.go @@ -26,7 +26,7 @@ func dataSourceNsxtPolicyTier1Gateway() *schema.Resource { Optional: true, Computed: true, }, - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_vm.go b/nsxt/data_source_nsxt_policy_vm.go index 1af5fed2b..04606baa0 100644 --- a/nsxt/data_source_nsxt_policy_vm.go +++ b/nsxt/data_source_nsxt_policy_vm.go @@ -26,7 +26,7 @@ func dataSourceNsxtPolicyVM() *schema.Resource { "external_id": getDataSourceStringSchema("External ID of the Virtual Machine"), "bios_id": getDataSourceStringSchema("BIOS UUID of the Virtual Machine"), "instance_id": getDataSourceStringSchema("Instance UUID of the Virtual Machine"), - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_vms.go b/nsxt/data_source_nsxt_policy_vms.go index ba558808d..3f9040396 100644 --- a/nsxt/data_source_nsxt_policy_vms.go +++ b/nsxt/data_source_nsxt_policy_vms.go @@ -58,7 +58,7 @@ func dataSourceNsxtPolicyVMs() *schema.Resource { Type: schema.TypeString, }, }, - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/data_source_nsxt_policy_vpc.go b/nsxt/data_source_nsxt_policy_vpc.go new file mode 100644 index 000000000..8718edab8 --- /dev/null +++ b/nsxt/data_source_nsxt_policy_vpc.go @@ -0,0 +1,57 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/runtime/bindings" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" +) + +func dataSourceNsxtPolicyVPC() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtPolicyVPCRead, + Schema: map[string]*schema.Schema{ + "id": getDataSourceIDSchema(), + "display_name": getDataSourceDisplayNameSchema(), + "description": getDataSourceDescriptionSchema(), + "path": getPathSchema(), + "context": getContextSchema(true, false), + "site_info": getSiteInfoSchema(), + "default_gateway_path": getPathSchema(), + "short_id": { + Type: schema.TypeString, + Optional: true, + }, + }, + } +} + +func dataSourceNsxtPolicyVPCRead(d *schema.ResourceData, m interface{}) error { + obj, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Vpc", nil) + if err != nil { + return err + } + + converter := bindings.NewTypeConverter() + dataValue, errors := converter.ConvertToGolang(obj, model.VpcBindingType()) + if len(errors) > 0 { + return errors[0] + } + vpc := dataValue.(model.Vpc) + + d.Set("default_gateway_path", vpc.DefaultGatewayPath) + d.Set("short_id", vpc.ShortId) + + var siteInfosList []map[string]interface{} + for _, item := range vpc.SiteInfos { + data := make(map[string]interface{}) + data["edge_cluster_paths"] = item.EdgeClusterPaths + data["site_path"] = item.SitePath + siteInfosList = append(siteInfosList, data) + } + d.Set("site_info", siteInfosList) + + return nil +} diff --git a/nsxt/data_source_policy_vpc_test.go b/nsxt/data_source_policy_vpc_test.go new file mode 100644 index 000000000..23d7caa0b --- /dev/null +++ b/nsxt/data_source_policy_vpc_test.go @@ -0,0 +1,127 @@ +/* Copyright © 2024 Broadcom, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "os" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects" +) + +func TestAccDataSourceNsxtPolicyVPC_basic_multitenancy(t *testing.T) { + name := getAccTestDataSourceName() + testResourceName := "data.nsxt_policy_vpc.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccOnlyMultitenancy(t) + testAccPreCheck(t) + testAccNSXVersion(t, "4.1.2") + }, + Providers: testAccProviders, + CheckDestroy: func(state *terraform.State) error { + return testAccDataSourceNsxtPolicyVPCDeleteByName(name) + }, + Steps: []resource.TestStep{ + { + PreConfig: func() { + if err := testAccDataSourceNsxtPolicyVPCCreate(name); err != nil { + t.Error(err) + } + }, + Config: testAccNsxtPolicyVPCReadTemplate(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(testResourceName, "display_name", name), + resource.TestCheckResourceAttr(testResourceName, "description", name), + resource.TestCheckResourceAttrSet(testResourceName, "path"), + resource.TestCheckResourceAttrSet(testResourceName, "short_id"), + ), + }, + }, + }) +} + +func testAccDataSourceNsxtPolicyVPCCreate(name string) error { + + connector, err := testAccGetPolicyConnector() + if err != nil { + return fmt.Errorf("error during test client initialization: %v", err) + } + + ipBlockID := newUUID() + err = testAccDataSourceNsxtPolicyIPBlockCreate(name, ipBlockID, "192.168.240.0/24", true) + if err != nil { + return err + } + + client := projects.NewVpcsClient(connector) + projID := os.Getenv("NSXT_PROJECT_ID") + + displayName := name + description := name + addrType := model.Vpc_IP_ADDRESS_TYPE_IPV4 + enableDhcp := false + disableGateway := true + obj := model.Vpc{ + Description: &description, + DisplayName: &displayName, + IpAddressType: &addrType, + DhcpConfig: &model.DhcpConfig{EnableDhcp: &enableDhcp}, + ServiceGateway: &model.ServiceGateway{Disable: &disableGateway}, + PrivateIpv4Blocks: []string{fmt.Sprintf("/orgs/default/projects/%s/infra/ip-blocks/%s", projID, ipBlockID)}, + } + + // Generate a random ID for the resource + id := newUUID() + + err = client.Patch(defaultOrgID, projID, id, obj) + if err != nil { + return handleCreateError("VPC", id, err) + } + return nil +} + +func testAccDataSourceNsxtPolicyVPCDeleteByName(name string) error { + connector, err := testAccGetPolicyConnector() + if err != nil { + return fmt.Errorf("error during test client initialization: %v", err) + } + + client := projects.NewVpcsClient(connector) + projID := os.Getenv("NSXT_PROJECT_ID") + + // Find the object by name + objList, err := client.List(defaultOrgID, projID, nil, nil, nil, nil, nil, nil) + if err != nil { + return handleListError("VPC", err) + } + for _, objInList := range objList.Results { + if *objInList.DisplayName == name { + err := client.Delete(defaultOrgID, projID, *objInList.Id) + if err != nil { + return handleDeleteError("VPC", *objInList.Id, err) + } + return testAccDataSourceNsxtPolicyIPBlockDeleteByName(name) + } + } + return fmt.Errorf("error while deleting VPC '%s': resource not found", name) +} + +func testAccNsxtPolicyVPCReadTemplate(name string) string { + context := testAccNsxtPolicyMultitenancyContext() + return fmt.Sprintf(` +data "nsxt_policy_ip_block" "test" { +%s + display_name = "%s" +} +data "nsxt_policy_vpc" "test" { +%s + display_name = "%s" +}`, context, name, context, name) +} diff --git a/nsxt/policy_common.go b/nsxt/policy_common.go index 46caa646e..4cba5eac2 100644 --- a/nsxt/policy_common.go +++ b/nsxt/policy_common.go @@ -298,7 +298,7 @@ func getSecurityPolicyAndGatewayRuleSchema(scopeRequired bool, isIds bool, nsxID } // Using computed context here, because context is required for consistency and // if it's not provided it can be derived from policy_path. - ruleSchema["context"] = getComputedContextSchema() + ruleSchema["context"] = getContextSchema(false, true) } else { ruleSchema["sequence_number"] = &schema.Schema{ Type: schema.TypeInt, @@ -328,7 +328,7 @@ func getPolicySecurityPolicySchema(isIds, withContext, withRule bool) map[string "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "domain": getDomainNameSchema(), "category": { Type: schema.TypeString, diff --git a/nsxt/provider.go b/nsxt/provider.go index 8beb5962a..0eef507a4 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -325,6 +325,7 @@ func Provider() *schema.Provider { "nsxt_policy_distributed_flood_protection_profile": dataSourceNsxtPolicyDistributedFloodProtectionProfile(), "nsxt_policy_gateway_flood_protection_profile": dataSourceNsxtPolicyGatewayFloodProtectionProfile(), "nsxt_manager_info": dataSourceNsxtManagerInfo(), + "nsxt_policy_vpc": dataSourceNsxtPolicyVPC(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/nsxt/resource_nsxt_policy_context_profile.go b/nsxt/resource_nsxt_policy_context_profile.go index 366589425..bb8523fa6 100644 --- a/nsxt/resource_nsxt_policy_context_profile.go +++ b/nsxt/resource_nsxt_policy_context_profile.go @@ -60,7 +60,7 @@ func resourceNsxtPolicyContextProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "app_id": getContextProfilePolicyAppIDAttributesSchema(), "custom_url": getContextProfilePolicyCustomURLAttributesSchema(), "domain_name": getContextProfilePolicyOtherAttributesSchema(), diff --git a/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go b/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go index 658077607..19623432e 100644 --- a/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go +++ b/nsxt/resource_nsxt_policy_context_profile_custom_attribute.go @@ -42,7 +42,7 @@ func resourceNsxtPolicyContextProfileCustomAttribute() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "context": getContextSchema(), + "context": getContextSchema(false, false), "key": { Type: schema.TypeString, Description: "Key for attribute", diff --git a/nsxt/resource_nsxt_policy_dhcp_relay.go b/nsxt/resource_nsxt_policy_dhcp_relay.go index 0b52d8753..cfbdf794f 100644 --- a/nsxt/resource_nsxt_policy_dhcp_relay.go +++ b/nsxt/resource_nsxt_policy_dhcp_relay.go @@ -32,7 +32,7 @@ func resourceNsxtPolicyDhcpRelayConfig() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "server_addresses": { Type: schema.TypeList, Required: true, diff --git a/nsxt/resource_nsxt_policy_dhcp_server.go b/nsxt/resource_nsxt_policy_dhcp_server.go index 5bca26a8e..a0d295b64 100644 --- a/nsxt/resource_nsxt_policy_dhcp_server.go +++ b/nsxt/resource_nsxt_policy_dhcp_server.go @@ -33,7 +33,7 @@ func resourceNsxtPolicyDhcpServer() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "edge_cluster_path": getPolicyPathSchema(false, false, "Edge Cluster path"), "lease_time": { Type: schema.TypeInt, diff --git a/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go b/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go index 34e73c29a..77213a8cf 100644 --- a/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go +++ b/nsxt/resource_nsxt_policy_dhcp_v4_static_binding.go @@ -38,7 +38,7 @@ func resourceNsxtPolicyDhcpV4StaticBinding() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "segment_path": getPolicyPathSchema(true, true, "segment path"), "gateway_address": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go b/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go index 7ea8cef16..ac314af33 100644 --- a/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go +++ b/nsxt/resource_nsxt_policy_dhcp_v6_static_binding.go @@ -34,7 +34,7 @@ func resourceNsxtPolicyDhcpV6StaticBinding() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "segment_path": getPolicyPathSchema(true, true, "segment path"), "dns_nameservers": { Type: schema.TypeList, diff --git a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go index a1212fe4f..ae643c4ca 100644 --- a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go +++ b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile.go @@ -39,7 +39,7 @@ func getFloodProtectionProfile() map[string]*schema.Schema { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "icmp_active_flow_limit": { Type: schema.TypeInt, Description: "Active ICMP connections limit", diff --git a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go index 04fd1a3b8..3ad961366 100644 --- a/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go +++ b/nsxt/resource_nsxt_policy_distributed_flood_protection_profile_binding.go @@ -30,7 +30,7 @@ func resourceNsxtPolicyDistributedFloodProtectionProfileBinding() *schema.Resour "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "profile_path": { Type: schema.TypeString, Description: "The path of the flood protection profile", diff --git a/nsxt/resource_nsxt_policy_dns_forwarder_zone.go b/nsxt/resource_nsxt_policy_dns_forwarder_zone.go index 5f43fe731..1534710e7 100644 --- a/nsxt/resource_nsxt_policy_dns_forwarder_zone.go +++ b/nsxt/resource_nsxt_policy_dns_forwarder_zone.go @@ -33,7 +33,7 @@ func resourceNsxtPolicyDNSForwarderZone() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "dns_domain_names": getDomainNamesSchema(), "source_ip": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go b/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go index bd5023fa3..1e641c11f 100644 --- a/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go +++ b/nsxt/resource_nsxt_policy_gateway_dns_forwarder.go @@ -41,7 +41,7 @@ func resourceNsxtPolicyGatewayDNSForwarder() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "gateway_path": getPolicyPathSchema(true, true, "Policy path for the Gateway"), "listener_ip": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go b/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go index c898fb998..85d405981 100644 --- a/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go +++ b/nsxt/resource_nsxt_policy_gateway_flood_protection_profile_binding.go @@ -33,7 +33,7 @@ func resourceNsxtPolicyGatewayFloodProtectionProfileBinding() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "profile_path": { Type: schema.TypeString, Description: "The path of the flood protection profile", diff --git a/nsxt/resource_nsxt_policy_group.go b/nsxt/resource_nsxt_policy_group.go index 80ddc0ab4..53c88d9e3 100644 --- a/nsxt/resource_nsxt_policy_group.go +++ b/nsxt/resource_nsxt_policy_group.go @@ -100,7 +100,7 @@ func resourceNsxtPolicyGroup() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "domain": getDomainNameSchema(), "group_type": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_intrusion_service_profile.go b/nsxt/resource_nsxt_policy_intrusion_service_profile.go index 5fef82f78..46d1a4907 100644 --- a/nsxt/resource_nsxt_policy_intrusion_service_profile.go +++ b/nsxt/resource_nsxt_policy_intrusion_service_profile.go @@ -56,7 +56,7 @@ func resourceNsxtPolicyIntrusionServiceProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "criteria": { Type: schema.TypeList, Description: "Filtering criteria for the IDS Profile", diff --git a/nsxt/resource_nsxt_policy_ip_address_allocation.go b/nsxt/resource_nsxt_policy_ip_address_allocation.go index 7139ddcb3..fda553ac2 100644 --- a/nsxt/resource_nsxt_policy_ip_address_allocation.go +++ b/nsxt/resource_nsxt_policy_ip_address_allocation.go @@ -39,7 +39,7 @@ func resourceNsxtPolicyIPAddressAllocation() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "pool_path": getPolicyPathSchema(true, true, "The path of the IP Pool for this allocation"), "allocation_ip": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_ip_block.go b/nsxt/resource_nsxt_policy_ip_block.go index 8cf666557..d477992cb 100644 --- a/nsxt/resource_nsxt_policy_ip_block.go +++ b/nsxt/resource_nsxt_policy_ip_block.go @@ -38,7 +38,7 @@ func resourceNsxtPolicyIPBlock() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "cidr": { Type: schema.TypeString, Description: "Network address and the prefix length which will be associated with a layer-2 broadcast domain", diff --git a/nsxt/resource_nsxt_policy_ip_discovery_profile.go b/nsxt/resource_nsxt_policy_ip_discovery_profile.go index fc1fdda15..54cd73753 100644 --- a/nsxt/resource_nsxt_policy_ip_discovery_profile.go +++ b/nsxt/resource_nsxt_policy_ip_discovery_profile.go @@ -33,7 +33,7 @@ func resourceNsxtPolicyIPDiscoveryProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "arp_nd_binding_timeout": { Type: schema.TypeInt, Description: "ARP and ND cache timeout (in minutes)", diff --git a/nsxt/resource_nsxt_policy_ip_pool.go b/nsxt/resource_nsxt_policy_ip_pool.go index 74c331f3b..abc5a47b8 100644 --- a/nsxt/resource_nsxt_policy_ip_pool.go +++ b/nsxt/resource_nsxt_policy_ip_pool.go @@ -32,7 +32,7 @@ func resourceNsxtPolicyIPPool() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "realized_id": { Type: schema.TypeString, Description: "The ID of the realized resource", diff --git a/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go b/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go index 6a38b0762..e82e46987 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go +++ b/nsxt/resource_nsxt_policy_ip_pool_block_subnet.go @@ -40,7 +40,7 @@ func resourceNsxtPolicyIPPoolBlockSubnet() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "auto_assign_gateway": { Type: schema.TypeBool, Description: "If true, the first IP in the range will be reserved for gateway", diff --git a/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go b/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go index 60b57cbf8..76908251b 100644 --- a/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go +++ b/nsxt/resource_nsxt_policy_ip_pool_static_subnet.go @@ -31,7 +31,7 @@ func resourceNsxtPolicyIPPoolStaticSubnet() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "pool_path": getPolicyPathSchema(true, true, "Policy path to the IP Pool for this Subnet"), "allocation_range": getAllocationRangeListSchema(true, "A collection of IPv4 or IPv6 IP ranges"), "cidr": { diff --git a/nsxt/resource_nsxt_policy_mac_discovery_profile.go b/nsxt/resource_nsxt_policy_mac_discovery_profile.go index 6a61f0b86..c50de7eba 100644 --- a/nsxt/resource_nsxt_policy_mac_discovery_profile.go +++ b/nsxt/resource_nsxt_policy_mac_discovery_profile.go @@ -38,7 +38,7 @@ func resourceNsxtPolicyMacDiscoveryProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "mac_change_enabled": { Type: schema.TypeBool, Optional: true, diff --git a/nsxt/resource_nsxt_policy_nat_rule.go b/nsxt/resource_nsxt_policy_nat_rule.go index 5a50617c0..24d0e56b6 100644 --- a/nsxt/resource_nsxt_policy_nat_rule.go +++ b/nsxt/resource_nsxt_policy_nat_rule.go @@ -56,7 +56,7 @@ func resourceNsxtPolicyNATRule() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "gateway_path": getPolicyGatewayPathSchema(), "action": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_predefined_gateway_policy.go b/nsxt/resource_nsxt_policy_predefined_gateway_policy.go index 717dbcf5c..f47c294d3 100644 --- a/nsxt/resource_nsxt_policy_predefined_gateway_policy.go +++ b/nsxt/resource_nsxt_policy_predefined_gateway_policy.go @@ -42,7 +42,7 @@ func getPolicyPredefinedGatewayPolicySchema() map[string]*schema.Schema { "rule": getSecurityPolicyAndGatewayRulesSchema(true, false, false), "default_rule": getGatewayPolicyDefaultRulesSchema(), "revision": getRevisionSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), } } diff --git a/nsxt/resource_nsxt_policy_predefined_security_policy.go b/nsxt/resource_nsxt_policy_predefined_security_policy.go index 42025ada0..cda88613d 100644 --- a/nsxt/resource_nsxt_policy_predefined_security_policy.go +++ b/nsxt/resource_nsxt_policy_predefined_security_policy.go @@ -39,7 +39,7 @@ func getSecurityPolicyDefaultRulesSchema() *schema.Schema { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "nsx_id": getComputedNsxIDSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "scope": { Type: schema.TypeString, Description: "Scope for this rule", @@ -85,7 +85,7 @@ func getPolicyPredefinedSecurityPolicySchema() map[string]*schema.Schema { "rule": getSecurityPolicyAndGatewayRulesSchema(false, false, false), "default_rule": getSecurityPolicyDefaultRulesSchema(), "revision": getRevisionSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), } } diff --git a/nsxt/resource_nsxt_policy_qos_profile.go b/nsxt/resource_nsxt_policy_qos_profile.go index 2124952a2..6742ad0f8 100644 --- a/nsxt/resource_nsxt_policy_qos_profile.go +++ b/nsxt/resource_nsxt_policy_qos_profile.go @@ -37,7 +37,7 @@ func resourceNsxtPolicyQosProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "class_of_service": { Type: schema.TypeInt, Description: "Class of service", diff --git a/nsxt/resource_nsxt_policy_segment_security_profile.go b/nsxt/resource_nsxt_policy_segment_security_profile.go index 262f78c62..1dc67c501 100644 --- a/nsxt/resource_nsxt_policy_segment_security_profile.go +++ b/nsxt/resource_nsxt_policy_segment_security_profile.go @@ -33,7 +33,7 @@ func resourceNsxtPolicySegmentSecurityProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "bpdu_filter_allow": { Type: schema.TypeSet, Elem: &schema.Schema{ diff --git a/nsxt/resource_nsxt_policy_service.go b/nsxt/resource_nsxt_policy_service.go index 154b33cd0..87bb1be88 100644 --- a/nsxt/resource_nsxt_policy_service.go +++ b/nsxt/resource_nsxt_policy_service.go @@ -36,7 +36,7 @@ func resourceNsxtPolicyService() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "icmp_entry": { Type: schema.TypeSet, diff --git a/nsxt/resource_nsxt_policy_spoof_guard_profile.go b/nsxt/resource_nsxt_policy_spoof_guard_profile.go index 7e3e13092..bb89103b2 100644 --- a/nsxt/resource_nsxt_policy_spoof_guard_profile.go +++ b/nsxt/resource_nsxt_policy_spoof_guard_profile.go @@ -32,7 +32,7 @@ func resourceNsxtPolicySpoofGuardProfile() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "address_binding_allowlist": { Type: schema.TypeBool, Optional: true, diff --git a/nsxt/resource_nsxt_policy_static_route.go b/nsxt/resource_nsxt_policy_static_route.go index 81c31523a..3619f4e3e 100644 --- a/nsxt/resource_nsxt_policy_static_route.go +++ b/nsxt/resource_nsxt_policy_static_route.go @@ -37,7 +37,7 @@ func resourceNsxtPolicyStaticRoute() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "gateway_path": getPolicyGatewayPathSchema(), "network": { Type: schema.TypeString, diff --git a/nsxt/resource_nsxt_policy_tier1_gateway.go b/nsxt/resource_nsxt_policy_tier1_gateway.go index 65b9b7ba3..5283a9f44 100644 --- a/nsxt/resource_nsxt_policy_tier1_gateway.go +++ b/nsxt/resource_nsxt_policy_tier1_gateway.go @@ -143,7 +143,7 @@ func resourceNsxtPolicyTier1Gateway() *schema.Resource { ValidateFunc: validation.StringInSlice(t1TypeValues, false), Optional: true, }, - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/resource_nsxt_policy_tier1_gateway_interface.go b/nsxt/resource_nsxt_policy_tier1_gateway_interface.go index 84009fc53..9e09af1e1 100644 --- a/nsxt/resource_nsxt_policy_tier1_gateway_interface.go +++ b/nsxt/resource_nsxt_policy_tier1_gateway_interface.go @@ -37,7 +37,7 @@ func resourceNsxtPolicyTier1GatewayInterface() *schema.Resource { "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "gateway_path": getPolicyPathSchema(true, true, "Policy path for tier1 gateway"), "segment_path": getPolicyPathSchema(true, true, "Policy path for connected segment"), "subnets": getGatewayInterfaceSubnetsSchema(), diff --git a/nsxt/resource_nsxt_policy_vm_tags.go b/nsxt/resource_nsxt_policy_vm_tags.go index 8ef62643f..5c3761290 100644 --- a/nsxt/resource_nsxt_policy_vm_tags.go +++ b/nsxt/resource_nsxt_policy_vm_tags.go @@ -55,7 +55,7 @@ func resourceNsxtPolicyVMTags() *schema.Resource { }, }, }, - "context": getContextSchema(), + "context": getContextSchema(false, false), }, } } diff --git a/nsxt/segment_common.go b/nsxt/segment_common.go index 533fcaae1..e54aa5898 100644 --- a/nsxt/segment_common.go +++ b/nsxt/segment_common.go @@ -266,7 +266,7 @@ func getPolicyCommonSegmentSchema(vlanRequired bool, isFixed bool) map[string]*s "description": getDescriptionSchema(), "revision": getRevisionSchema(), "tag": getTagsSchema(), - "context": getContextSchema(), + "context": getContextSchema(false, false), "advanced_config": { Type: schema.TypeList, Description: "Advanced segment configuration", diff --git a/nsxt/utils.go b/nsxt/utils.go index 677b947ea..59f2864a2 100644 --- a/nsxt/utils.go +++ b/nsxt/utils.go @@ -702,11 +702,13 @@ func handlePagination(lister func(*paginationInfo) error) (int64, error) { return total, nil } -func getContextSchema() *schema.Schema { +func getContextSchema(isRequired, isComputed bool) *schema.Schema { return &schema.Schema{ Type: schema.TypeList, Description: "Resource context", - Optional: true, + Optional: !isRequired, + Required: isRequired, + Computed: isComputed, MaxItems: 1, ForceNew: true, Elem: &schema.Resource{ @@ -723,27 +725,6 @@ func getContextSchema() *schema.Schema { } } -func getComputedContextSchema() *schema.Schema { - return &schema.Schema{ - Type: schema.TypeList, - Description: "Resource context", - Optional: true, - Computed: true, - MaxItems: 1, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "project_id": { - Type: schema.TypeString, - Description: "Id of the project which the resource belongs to.", - Required: true, - ForceNew: true, - }, - }, - }, - } -} - func getCustomizedMPTagsFromSchema(d *schema.ResourceData, schemaName string) []mp_model.Tag { tags := d.Get(schemaName).(*schema.Set).List() tagList := make([]mp_model.Tag, 0) diff --git a/website/docs/d/policy_vpc.html.markdown b/website/docs/d/policy_vpc.html.markdown new file mode 100644 index 000000000..9c265e456 --- /dev/null +++ b/website/docs/d/policy_vpc.html.markdown @@ -0,0 +1,45 @@ +--- +subcategory: "Multitenancy" +layout: "nsxt" +page_title: "NSXT: policy_vpc" +description: Policy VPC data source. +--- + +# nsxt_policy_vpc + +This data source provides information about policy VPC configured on NSX. +This data source is applicable to NSX Policy Manager. + +## Example Usage + +```hcl +data "nsxt_policy_project" "demoproj" { + display_name = "demoproj" +} + +data "nsxt_policy_vpc" "test" { + context { + project_id = data.nsxt_policy_project.demoproj.id + } + display_name = "vpc1" +} +``` + +## Argument Reference + +* `id` - (Optional) The ID of VPC to retrieve. If ID is specified, no additional argument should be configured. +* `display_name` - (Optional) The Display Name prefix of the VPC to retrieve. +* `context` - (Optional) The context which the object belongs to + * `project_id` - (Required) The ID of the project which the object belongs to + +## Attributes Reference + +In addition to arguments listed above, the following attributes are exported: + +* `description` - The description of the resource. +* `path` - The NSX path of the policy resource. +* `short_id` - Defaults to id if id is less than equal to 8 characters or defaults to random generated id if not set. +* `site_info` - Information related to sites applicable for given Project. + * `edge_cluster_paths` - The edge cluster on which the networking elements for the Org will be created. + * `site_path` - This represents the path of the site which is managed by Global Manager. For the local manager, if set, this needs to point to 'default'. +* `default_gateway_path` - The tier 0 has to be pre-created before Project is created.