-
Notifications
You must be signed in to change notification settings - Fork 85
/
data_source_nsxt_manager_cluster_node.go
102 lines (94 loc) · 2.99 KB
/
data_source_nsxt_manager_cluster_node.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/cluster"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
)
func dataSourceNsxtManagerClusterNode() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtManagerClusterNodeRead,
DeprecationMessage: mpObjectDataSourceDeprecationMessage,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique ID of this resource",
Optional: true,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The display name of this resource",
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of this resource",
Optional: true,
Computed: true,
},
"appliance_mgmt_listen_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The IP and port for the appliance management API service on this node",
},
},
}
}
func dataSourceNsxtManagerClusterNodeRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := cluster.NewNodesClient(connector)
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj model.ClusterNodeConfig
if objID != "" {
// Get by id
objGet, err := client.Get(objID)
if err != nil {
return handleDataSourceReadError(d, "ClusterNode", objID, err)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("error obtaining ClusterNode ID or name during read")
} else {
// Get by full name/prefix
objList, err := client.List(nil, nil, nil, nil, nil)
if err != nil {
return handleListError("ClusterNode", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.ClusterNodeConfig
var prefixMatch []model.ClusterNodeConfig
for _, objInList := range objList.Results {
if strings.HasPrefix(*objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
if *objInList.DisplayName == objName {
perfectMatch = append(perfectMatch, objInList)
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("found multiple ClusterNode with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("found multiple ClusterNodes with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("ClusterNode with name '%s' was not found", objName)
}
}
d.SetId(*obj.Id)
d.Set("appliance_mgmt_listen_address", obj.ApplianceMgmtListenAddr)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
return nil
}