Skip to content

Commit b2b525f

Browse files
authored
Merge pull request #1160 from ksamoray/ds_management_cluster
Revise nsxt_management_cluster datasource
2 parents e7ce162 + ca02ae7 commit b2b525f

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

nsxt/data_source_nsxt_management_cluster.go

+16-27
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ package nsxt
55

66
import (
77
"fmt"
8-
"net/http"
8+
"net"
99

1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
1112
)
1213

1314
func dataSourceNsxtManagementCluster() *schema.Resource {
1415
return &schema.Resource{
15-
Read: dataSourceNsxtManagementClusterRead,
16-
DeprecationMessage: mpObjectDataSourceDeprecationMessage,
16+
Read: dataSourceNsxtManagementClusterRead,
1717
Schema: map[string]*schema.Schema{
1818
"id": {
1919
Type: schema.TypeString,
@@ -30,42 +30,31 @@ func dataSourceNsxtManagementCluster() *schema.Resource {
3030
}
3131

3232
func dataSourceNsxtManagementClusterRead(d *schema.ResourceData, m interface{}) error {
33-
nsxClient := m.(nsxtClients).NsxtClient
34-
if nsxClient == nil {
35-
return dataSourceNotSupportedError()
36-
}
33+
connector := getPolicyConnector(m)
34+
client := nsx.NewClusterClient(connector)
3735

38-
clusterObj, resp, err := nsxClient.NsxComponentAdministrationApi.ReadClusterConfig(nsxClient.Context)
36+
hostIP, err := net.LookupIP(m.(nsxtClients).Host[len("https://"):])
3937
if err != nil {
40-
return fmt.Errorf("Error while reading cluster configuration: %v", err)
41-
}
42-
if resp != nil && resp.StatusCode != http.StatusOK {
43-
return fmt.Errorf("Unexpected Response while reading cluster configuration. Status Code: %d", resp.StatusCode)
38+
return fmt.Errorf("error while resolving client hostname %s: %v", m.(nsxtClients).Host[len("https://"):], err)
4439
}
4540

46-
nodeList, resp, err := nsxClient.NsxComponentAdministrationApi.ListClusterNodeConfigs(nsxClient.Context, nil)
41+
clusterObj, err := client.Get()
4742
if err != nil {
48-
return fmt.Errorf("Error while reading cluster node configuration: %v", err)
43+
return fmt.Errorf("error while reading cluster configuration: %v", err)
4944
}
50-
if resp != nil && resp.StatusCode != http.StatusOK {
51-
return fmt.Errorf("Unexpected Response while reading cluster node configuration. Status Code: %d", resp.StatusCode)
52-
}
53-
for _, nodeConfig := range nodeList.Results {
54-
if nodeConfig.ManagerRole != nil && nodeConfig.ManagerRole.ApiListenAddr != nil && nodeConfig.ManagerRole.ApiListenAddr.IpAddress == m.(nsxtClients).Host[len("https://"):] {
55-
if nodeConfig.ManagerRole.ApiListenAddr.CertificateSha256Thumbprint == "" {
56-
return fmt.Errorf("Manager node thumbprint not found while reading cluster node configuration")
45+
for _, nodeConfig := range clusterObj.Nodes {
46+
if nodeConfig.ApiListenAddr != nil && *nodeConfig.ApiListenAddr.IpAddress == hostIP[0].String() {
47+
if *nodeConfig.ApiListenAddr.CertificateSha256Thumbprint == "" {
48+
return fmt.Errorf("manager node thumbprint not found while reading cluster node configuration for node %s", *nodeConfig.ApiListenAddr.IpAddress)
5749
}
58-
d.Set("node_sha256_thumbprint", nodeConfig.ManagerRole.ApiListenAddr.CertificateSha256Thumbprint)
50+
d.Set("node_sha256_thumbprint", nodeConfig.ApiListenAddr.CertificateSha256Thumbprint)
5951
}
6052
}
6153

62-
if clusterObj.ClusterId == "" {
63-
return fmt.Errorf("Cluster id not found")
64-
}
6554
if d.Get("node_sha256_thumbprint").(string) == "" {
66-
return fmt.Errorf("Cluster node sha256 thumbprint not found")
55+
return fmt.Errorf("cluster node sha256 thumbprint not found for node %s", m.(nsxtClients).Host[len("https://"):])
6756
}
57+
d.SetId(*clusterObj.ClusterId)
6858

69-
d.SetId(clusterObj.ClusterId)
7059
return nil
7160
}

nsxt/data_source_nsxt_management_cluster_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestAccDataSourceNsxtManagementCluster_basic(t *testing.T) {
1313
testResourceName := "data.nsxt_management_cluster.test"
1414

1515
resource.ParallelTest(t, resource.TestCase{
16-
PreCheck: func() { testAccOnlyLocalManager(t); testAccTestDeprecated(t); testAccPreCheck(t) },
16+
PreCheck: func() { testAccOnlyLocalManager(t); testAccPreCheck(t) },
1717
Providers: testAccProviders,
1818
Steps: []resource.TestStep{
1919
{

0 commit comments

Comments
 (0)