@@ -5,15 +5,15 @@ package nsxt
5
5
6
6
import (
7
7
"fmt"
8
- "net/http "
8
+ "net"
9
9
10
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11
+ "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
11
12
)
12
13
13
14
func dataSourceNsxtManagementCluster () * schema.Resource {
14
15
return & schema.Resource {
15
- Read : dataSourceNsxtManagementClusterRead ,
16
- DeprecationMessage : mpObjectDataSourceDeprecationMessage ,
16
+ Read : dataSourceNsxtManagementClusterRead ,
17
17
Schema : map [string ]* schema.Schema {
18
18
"id" : {
19
19
Type : schema .TypeString ,
@@ -30,42 +30,31 @@ func dataSourceNsxtManagementCluster() *schema.Resource {
30
30
}
31
31
32
32
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 )
37
35
38
- clusterObj , resp , err := nsxClient . NsxComponentAdministrationApi . ReadClusterConfig ( nsxClient . Context )
36
+ hostIP , err := net . LookupIP ( m .( nsxtClients ). Host [ len ( "https://" ):] )
39
37
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 )
44
39
}
45
40
46
- nodeList , resp , err := nsxClient . NsxComponentAdministrationApi . ListClusterNodeConfigs ( nsxClient . Context , nil )
41
+ clusterObj , err := client . Get ( )
47
42
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 )
49
44
}
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 )
57
49
}
58
- d .Set ("node_sha256_thumbprint" , nodeConfig .ManagerRole . ApiListenAddr .CertificateSha256Thumbprint )
50
+ d .Set ("node_sha256_thumbprint" , nodeConfig .ApiListenAddr .CertificateSha256Thumbprint )
59
51
}
60
52
}
61
53
62
- if clusterObj .ClusterId == "" {
63
- return fmt .Errorf ("Cluster id not found" )
64
- }
65
54
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://" ):] )
67
56
}
57
+ d .SetId (* clusterObj .ClusterId )
68
58
69
- d .SetId (clusterObj .ClusterId )
70
59
return nil
71
60
}
0 commit comments