Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement nsxt_manager_cluster_node data source #1024

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions nsxt/data_source_nsxt_manager_cluster_node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}
41 changes: 41 additions & 0 deletions nsxt/data_source_nsxt_manager_cluster_node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceNsxtManagerClusterNode_basic(t *testing.T) {
testResourceName := "data.nsxt_manager_cluster_node.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccOnlyLocalManager(t)
testAccPreCheck(t)
testAccNSXVersion(t, "4.1.0")
testAccEnvDefined(t, "NSXT_TEST_MANAGER_CLUSTER_NODE")
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyManagerClusterNodeReadTemplate(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(testResourceName, "display_name"),
resource.TestCheckResourceAttrSet(testResourceName, "appliance_mgmt_listen_address"),
),
},
},
})
}

func testAccNsxtPolicyManagerClusterNodeReadTemplate() string {
return fmt.Sprintf(`
data "nsxt_manager_cluster_node" "test" {
display_name = "%s"
}`, getTestManagerClusterNode())
}
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func Provider() *schema.Provider {
"nsxt_compute_collection": dataSourceNsxtComputeCollection(),
"nsxt_compute_manager_realization": dataSourceNsxtComputeManagerRealization(),
"nsxt_policy_host_transport_node": dataSourceNsxtPolicyHostTransportNode(),
"nsxt_manager_cluster_node": dataSourceNsxtManagerClusterNode(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
4 changes: 4 additions & 0 deletions nsxt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ func getTestLdapBaseDN() string {
return os.Getenv("NSXT_TEST_LDAP_BASE_DN")
}

func getTestManagerClusterNode() string {
return os.Getenv("NSXT_TEST_MANAGER_CLUSTER_NODE")
}

func testAccEnvDefined(t *testing.T, envVar string) {
if len(os.Getenv(envVar)) == 0 {
t.Skipf("This test requires %s environment variable to be set", envVar)
Expand Down