diff --git a/nsxt/data_source_nsxt_policy_gateway_interface_realization_info.go b/nsxt/data_source_nsxt_policy_gateway_interface_realization_info.go new file mode 100644 index 000000000..fe80e7685 --- /dev/null +++ b/nsxt/data_source_nsxt_policy_gateway_interface_realization_info.go @@ -0,0 +1,167 @@ +/* Copyright © 2024 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + "strings" + "time" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" + + realizedstate "github.com/vmware/terraform-provider-nsxt/api/infra/realized_state" +) + +func dataSourceNsxtPolicyGatewayInterfaceRealizationInfo() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtPolicyGatewayInterfaceRealizationInfoRead, + + Schema: map[string]*schema.Schema{ + "id": getDataSourceIDSchema(), + "context": getContextSchema(), + "gateway_path": { + Type: schema.TypeString, + Description: "The path for the gateway", + Required: true, + ValidateFunc: validatePolicyPath(), + }, + "display_name": { + Type: schema.TypeString, + Description: "The display name of the gateway interface", + Optional: true, + }, + "timeout": { + Type: schema.TypeInt, + Description: "Realization timeout in seconds", + Optional: true, + Default: 1200, + ValidateFunc: validation.IntAtLeast(1), + }, + "delay": { + Type: schema.TypeInt, + Description: "Initial delay to start realization checks in seconds", + Optional: true, + Default: 1, + ValidateFunc: validation.IntAtLeast(0), + }, + "state": { + Type: schema.TypeString, + Description: "The state of the realized gateway interface", + Computed: true, + }, + "ip_address": { + Type: schema.TypeList, + Description: "IP addresses of the gateway interface", + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "mac_address": { + Type: schema.TypeString, + Description: "MAC address of the gateway interface", + Computed: true, + }, + }, + } +} + +func dataSourceNsxtPolicyGatewayInterfaceRealizationInfoRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + client := realizedstate.NewRealizedEntitiesClient(getSessionContext(d, m), connector) + + id := d.Get("id").(string) + gatewayPath := d.Get("gateway_path").(string) + displayName := d.Get("display_name").(string) + delay := d.Get("delay").(int) + timeout := d.Get("timeout").(int) + + pendingStates := []string{"UNKNOWN", "UNREALIZED"} + targetStates := []string{"REALIZED", "ERROR"} + stateConf := &resource.StateChangeConf{ + Pending: pendingStates, + Target: targetStates, + Refresh: func() (interface{}, string, error) { + result, err := client.List(gatewayPath, nil) + if err != nil { + return result, "", err + } + + var perfectMatch []model.GenericPolicyRealizedResource + var containsMatch []model.GenericPolicyRealizedResource + for _, objInList := range result.Results { + if id != "" && objInList.Id != nil && id == *objInList.Id { + if objInList.State == nil { + return result, "UNKNOWN", nil + } + setGatewayInterfaceRealizationInfoInSchema(objInList, d) + return result, *objInList.State, nil + } else if displayName != "" && objInList.DisplayName != nil { + if displayName == *objInList.DisplayName { + perfectMatch = append(perfectMatch, objInList) + } else if strings.Contains(*objInList.DisplayName, displayName) { + containsMatch = append(containsMatch, objInList) + } + } else { + // If neither ID nor displayName is provided, return the one with IPAddresses, considering + // it is the most common case. + for _, ext := range objInList.ExtendedAttributes { + if ext.Key != nil && *ext.Key == "IpAddresses" { + setGatewayInterfaceRealizationInfoInSchema(objInList, d) + return result, *objInList.State, nil + } + } + } + } + + if len(perfectMatch) > 1 { + return result, "", fmt.Errorf("Found multiple gateway interfaces with name '%s'", displayName) + } + if len(perfectMatch) > 0 { + if perfectMatch[0].State == nil { + return result, "UNKNOWN", nil + } + setGatewayInterfaceRealizationInfoInSchema(perfectMatch[0], d) + return result, *perfectMatch[0].State, nil + } + + if len(containsMatch) > 1 { + return result, "", fmt.Errorf("Found multiple gateway interfaces whose name contains '%s'", displayName) + } + if len(containsMatch) > 0 { + if containsMatch[0].State == nil { + return result, "UNKNOWN", nil + } + setGatewayInterfaceRealizationInfoInSchema(containsMatch[0], d) + return result, *containsMatch[0].State, nil + } + + return result, "UNKNOWN", nil + }, + Timeout: time.Duration(timeout) * time.Second, + MinTimeout: 1 * time.Second, + Delay: time.Duration(delay) * time.Second, + } + _, err := stateConf.WaitForState() + if err != nil { + return fmt.Errorf("Failed to get gateway interface realization information for %s: %v", gatewayPath, err) + } + return nil +} + +func setGatewayInterfaceRealizationInfoInSchema(realizedResource model.GenericPolicyRealizedResource, d *schema.ResourceData) { + for _, ext := range realizedResource.ExtendedAttributes { + if *ext.Key == "IpAddresses" && len(ext.Values) > 0 { + d.Set("ip_address", ext.Values) + } + if *ext.Key == "MacAddress" && len(ext.Values) > 0 { + d.Set("mac_address", ext.Values[0]) + } + } + d.Set("state", *realizedResource.State) + d.SetId(*realizedResource.Id) +} diff --git a/nsxt/provider.go b/nsxt/provider.go index 6783bed44..9e647c06f 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -242,80 +242,81 @@ func Provider() *schema.Provider { }, DataSourcesMap: map[string]*schema.Resource{ - "nsxt_provider_info": dataSourceNsxtProviderInfo(), - "nsxt_transport_zone": dataSourceNsxtTransportZone(), - "nsxt_switching_profile": dataSourceNsxtSwitchingProfile(), - "nsxt_logical_tier0_router": dataSourceNsxtLogicalTier0Router(), - "nsxt_logical_tier1_router": dataSourceNsxtLogicalTier1Router(), - "nsxt_mac_pool": dataSourceNsxtMacPool(), - "nsxt_ns_group": dataSourceNsxtNsGroup(), - "nsxt_ns_groups": dataSourceNsxtNsGroups(), - "nsxt_ns_service": dataSourceNsxtNsService(), - "nsxt_ns_services": dataSourceNsxtNsServices(), - "nsxt_edge_cluster": dataSourceNsxtEdgeCluster(), - "nsxt_certificate": dataSourceNsxtCertificate(), - "nsxt_ip_pool": dataSourceNsxtIPPool(), - "nsxt_firewall_section": dataSourceNsxtFirewallSection(), - "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_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_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": dataSourceNsxtEdgeTransportNode(), - "nsxt_discover_node": dataSourceNsxtDiscoverNode(), - "nsxt_edge_upgrade_group": dataSourceNsxtEdgeUpgradeGroup(), - "nsxt_host_upgrade_group": dataSourceNsxtHostUpgradeGroup(), + "nsxt_provider_info": dataSourceNsxtProviderInfo(), + "nsxt_transport_zone": dataSourceNsxtTransportZone(), + "nsxt_switching_profile": dataSourceNsxtSwitchingProfile(), + "nsxt_logical_tier0_router": dataSourceNsxtLogicalTier0Router(), + "nsxt_logical_tier1_router": dataSourceNsxtLogicalTier1Router(), + "nsxt_mac_pool": dataSourceNsxtMacPool(), + "nsxt_ns_group": dataSourceNsxtNsGroup(), + "nsxt_ns_groups": dataSourceNsxtNsGroups(), + "nsxt_ns_service": dataSourceNsxtNsService(), + "nsxt_ns_services": dataSourceNsxtNsServices(), + "nsxt_edge_cluster": dataSourceNsxtEdgeCluster(), + "nsxt_certificate": dataSourceNsxtCertificate(), + "nsxt_ip_pool": dataSourceNsxtIPPool(), + "nsxt_firewall_section": dataSourceNsxtFirewallSection(), + "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_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_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": dataSourceNsxtEdgeTransportNode(), + "nsxt_discover_node": dataSourceNsxtDiscoverNode(), + "nsxt_edge_upgrade_group": dataSourceNsxtEdgeUpgradeGroup(), + "nsxt_host_upgrade_group": dataSourceNsxtHostUpgradeGroup(), + "nsxt_policy_gateway_interface_realization_info": dataSourceNsxtPolicyGatewayInterfaceRealizationInfo(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/website/docs/d/policy_gateway_interface_realization_info.html.markdown b/website/docs/d/policy_gateway_interface_realization_info.html.markdown new file mode 100644 index 000000000..2ec49c6eb --- /dev/null +++ b/website/docs/d/policy_gateway_interface_realization_info.html.markdown @@ -0,0 +1,65 @@ +--- +subcategory: "Realization" +layout: "nsxt" +page_title: "NSXT: policy_gateway_interface_realization_info" +description: A gateway interface realization information. +--- + +# nsxt_policy_gateway_interface_realization_info + +This data source provides information about the realization of a Tier0/Tier1 gateway interface resource on NSX manager. This data source will wait until realization is determined as either success or error. It is recommended to use this data source if further configuration depends on resource realization. + +This data source is applicable to NSX Policy Manager, NSX Global Manager and Multi-Tenancy. + +## Example Usage + +```hcl +data "nsxt_policy_tier1_gateway" "tier1_gw" { + display_name = "tier1_gw" +} + +data "nsxt_policy_gateway_interface_realization_info" "info" { + gateway_path = data.nsxt_policy_tier1_gateway.tier1_gw.path + display_name = "pepsi-it_t1-t1_lrp" + timeout = 60 +} +``` + +## Example Usage - Multi-Tenancy + +```hcl +data "nsxt_policy_project" "demoproj" { + display_name = "demoproj" +} + +data "nsxt_policy_tier1_gateway" "tier1_gw" { + display_name = "tier1_gw" +} + +data "nsxt_policy_gateway_interface_realization_info" "info" { + context { + project_id = data.nsxt_policy_project.demoproj.id + } + gateway_path = data.nsxt_policy_tier1_gateway.tier1_gw.path + display_name = "pepsi-it_t1-t1_lrp" + timeout = 60 +} +``` + +## Argument Reference + +* `gateway_path` - (Required) The policy path of the resource. +* `id` - (Optional) The ID of gateway interface. +* `display_name` - (Optional) The display name of the resource. If neither ID nor display name are set, the realized gateway interface with IP addresses will be retrieved. +* `delay` - (Optional) Delay (in seconds) before realization polling is started. Default is set to 1. +* `timeout` - (Optional) Timeout (in seconds) for realization polling. Default is set to 1200. +* `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: + +* `state` - The realization state of the resource: "REALIZED", "UNKNOWN", "UNREALIZED" or "ERROR". +* `ip_address` - The IP addresses of the realized object. +* `mac_address` - The MAC addresses of the realized object.