From ca02ae783bd727c19b919bb5bf0000e4b980ff22 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Sun, 24 Mar 2024 12:16:56 +0200 Subject: [PATCH] Revise nsxt_management_cluster datasource Management cluster datasource is required to retrieve the managemnet endpoint sha256 thumbprint. This change un-deprecates this DS as it has no replacement and changes the API in use to the NSX golang SDK. Signed-off-by: Kobi Samoray --- nsxt/data_source_nsxt_management_cluster.go | 43 +++++++------------ ...ata_source_nsxt_management_cluster_test.go | 2 +- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/nsxt/data_source_nsxt_management_cluster.go b/nsxt/data_source_nsxt_management_cluster.go index 99501a94e..087836a23 100644 --- a/nsxt/data_source_nsxt_management_cluster.go +++ b/nsxt/data_source_nsxt_management_cluster.go @@ -5,15 +5,15 @@ package nsxt import ( "fmt" - "net/http" + "net" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx" ) func dataSourceNsxtManagementCluster() *schema.Resource { return &schema.Resource{ - Read: dataSourceNsxtManagementClusterRead, - DeprecationMessage: mpObjectDataSourceDeprecationMessage, + Read: dataSourceNsxtManagementClusterRead, Schema: map[string]*schema.Schema{ "id": { Type: schema.TypeString, @@ -30,42 +30,31 @@ func dataSourceNsxtManagementCluster() *schema.Resource { } func dataSourceNsxtManagementClusterRead(d *schema.ResourceData, m interface{}) error { - nsxClient := m.(nsxtClients).NsxtClient - if nsxClient == nil { - return dataSourceNotSupportedError() - } + connector := getPolicyConnector(m) + client := nsx.NewClusterClient(connector) - clusterObj, resp, err := nsxClient.NsxComponentAdministrationApi.ReadClusterConfig(nsxClient.Context) + hostIP, err := net.LookupIP(m.(nsxtClients).Host[len("https://"):]) if err != nil { - return fmt.Errorf("Error while reading cluster configuration: %v", err) - } - if resp != nil && resp.StatusCode != http.StatusOK { - return fmt.Errorf("Unexpected Response while reading cluster configuration. Status Code: %d", resp.StatusCode) + return fmt.Errorf("error while resolving client hostname %s: %v", m.(nsxtClients).Host[len("https://"):], err) } - nodeList, resp, err := nsxClient.NsxComponentAdministrationApi.ListClusterNodeConfigs(nsxClient.Context, nil) + clusterObj, err := client.Get() if err != nil { - return fmt.Errorf("Error while reading cluster node configuration: %v", err) + return fmt.Errorf("error while reading cluster configuration: %v", err) } - if resp != nil && resp.StatusCode != http.StatusOK { - return fmt.Errorf("Unexpected Response while reading cluster node configuration. Status Code: %d", resp.StatusCode) - } - for _, nodeConfig := range nodeList.Results { - if nodeConfig.ManagerRole != nil && nodeConfig.ManagerRole.ApiListenAddr != nil && nodeConfig.ManagerRole.ApiListenAddr.IpAddress == m.(nsxtClients).Host[len("https://"):] { - if nodeConfig.ManagerRole.ApiListenAddr.CertificateSha256Thumbprint == "" { - return fmt.Errorf("Manager node thumbprint not found while reading cluster node configuration") + for _, nodeConfig := range clusterObj.Nodes { + if nodeConfig.ApiListenAddr != nil && *nodeConfig.ApiListenAddr.IpAddress == hostIP[0].String() { + if *nodeConfig.ApiListenAddr.CertificateSha256Thumbprint == "" { + return fmt.Errorf("manager node thumbprint not found while reading cluster node configuration for node %s", *nodeConfig.ApiListenAddr.IpAddress) } - d.Set("node_sha256_thumbprint", nodeConfig.ManagerRole.ApiListenAddr.CertificateSha256Thumbprint) + d.Set("node_sha256_thumbprint", nodeConfig.ApiListenAddr.CertificateSha256Thumbprint) } } - if clusterObj.ClusterId == "" { - return fmt.Errorf("Cluster id not found") - } if d.Get("node_sha256_thumbprint").(string) == "" { - return fmt.Errorf("Cluster node sha256 thumbprint not found") + return fmt.Errorf("cluster node sha256 thumbprint not found for node %s", m.(nsxtClients).Host[len("https://"):]) } + d.SetId(*clusterObj.ClusterId) - d.SetId(clusterObj.ClusterId) return nil } diff --git a/nsxt/data_source_nsxt_management_cluster_test.go b/nsxt/data_source_nsxt_management_cluster_test.go index 0220439ae..7fcf940a6 100644 --- a/nsxt/data_source_nsxt_management_cluster_test.go +++ b/nsxt/data_source_nsxt_management_cluster_test.go @@ -13,7 +13,7 @@ func TestAccDataSourceNsxtManagementCluster_basic(t *testing.T) { testResourceName := "data.nsxt_management_cluster.test" resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccOnlyLocalManager(t); testAccTestDeprecated(t); testAccPreCheck(t) }, + PreCheck: func() { testAccOnlyLocalManager(t); testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ {