From 4a97230aa0d0b147213bd60310c76b4aa2534d1e Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Thu, 9 Nov 2023 17:36:55 +0200 Subject: [PATCH] Implement nsxt_manager_cluster_node data source A data source which exposes manager cluster nodes Signed-off-by: Kobi Samoray --- nsxt/data_source_nsxt_manager_cluster_node.go | 102 ++++++++++++++++++ ...a_source_nsxt_manager_cluster_node_test.go | 41 +++++++ nsxt/provider.go | 1 + nsxt/utils_test.go | 4 + 4 files changed, 148 insertions(+) create mode 100644 nsxt/data_source_nsxt_manager_cluster_node.go create mode 100644 nsxt/data_source_nsxt_manager_cluster_node_test.go diff --git a/nsxt/data_source_nsxt_manager_cluster_node.go b/nsxt/data_source_nsxt_manager_cluster_node.go new file mode 100644 index 000000000..0e922046e --- /dev/null +++ b/nsxt/data_source_nsxt_manager_cluster_node.go @@ -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 +} diff --git a/nsxt/data_source_nsxt_manager_cluster_node_test.go b/nsxt/data_source_nsxt_manager_cluster_node_test.go new file mode 100644 index 000000000..961623bd0 --- /dev/null +++ b/nsxt/data_source_nsxt_manager_cluster_node_test.go @@ -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()) +} diff --git a/nsxt/provider.go b/nsxt/provider.go index 43dec2f7e..6c448fb60 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -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{ diff --git a/nsxt/utils_test.go b/nsxt/utils_test.go index e5539dc3d..13cd6ef72 100644 --- a/nsxt/utils_test.go +++ b/nsxt/utils_test.go @@ -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)