Skip to content

Commit

Permalink
Add gateway interface realizaiton info
Browse files Browse the repository at this point in the history
Signed-off-by: graysonwu <[email protected]>
  • Loading branch information
GraysonWu committed Feb 8, 2024
1 parent 691b55b commit b97fa11
Show file tree
Hide file tree
Showing 3 changed files with 307 additions and 74 deletions.
167 changes: 167 additions & 0 deletions nsxt/data_source_nsxt_policy_gateway_interface_realization_info.go
Original file line number Diff line number Diff line change
@@ -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)
}
149 changes: 75 additions & 74 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
Loading

0 comments on commit b97fa11

Please sign in to comment.