forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_nsxt_management_cluster.go
71 lines (62 loc) · 2.41 KB
/
data_source_nsxt_management_cluster.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Copyright © 2020 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"net/http"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceNsxtManagementCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtManagementClusterRead,
DeprecationMessage: mpObjectDataSourceDeprecationMessage,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique identifier of this cluster.",
Computed: true,
},
"node_sha256_thumbprint": {
Type: schema.TypeString,
Description: "SHA256 of certificate thumbprint of this manager node.",
Computed: true,
},
},
}
}
func dataSourceNsxtManagementClusterRead(d *schema.ResourceData, m interface{}) error {
nsxClient := m.(nsxtClients).NsxtClient
if nsxClient == nil {
return dataSourceNotSupportedError()
}
clusterObj, resp, err := nsxClient.NsxComponentAdministrationApi.ReadClusterConfig(nsxClient.Context)
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)
}
nodeList, resp, err := nsxClient.NsxComponentAdministrationApi.ListClusterNodeConfigs(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading cluster node 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")
}
d.Set("node_sha256_thumbprint", nodeConfig.ManagerRole.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")
}
d.SetId(clusterObj.ClusterId)
return nil
}