Skip to content

Commit

Permalink
Merge pull request vmware#1160 from ksamoray/ds_management_cluster
Browse files Browse the repository at this point in the history
Revise nsxt_management_cluster datasource
  • Loading branch information
ksamoray authored Mar 28, 2024
2 parents e7ce162 + ca02ae7 commit b2b525f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 28 deletions.
43 changes: 16 additions & 27 deletions nsxt/data_source_nsxt_management_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion nsxt/data_source_nsxt_management_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
{
Expand Down

0 comments on commit b2b525f

Please sign in to comment.