From d7733b99680d3caa8c4c418a87221c59f4264d33 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Tue, 19 Sep 2023 22:40:39 +0000 Subject: [PATCH] Improve Compute Manager data source Since usually only one compute manager is configured, it is useful to retrieve its ID without providing any filters. This PR enables this. Signed-off-by: Anna Khmelnitsky --- nsxt/data_source_nsxt_compute_manager.go | 15 +++++--- nsxt/data_source_nsxt_compute_manager_test.go | 38 ++++++++++++++++--- website/docs/d/compute_manager.html.markdown | 7 ++-- 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/nsxt/data_source_nsxt_compute_manager.go b/nsxt/data_source_nsxt_compute_manager.go index dadf81be3..bc9ba4278 100644 --- a/nsxt/data_source_nsxt_compute_manager.go +++ b/nsxt/data_source_nsxt_compute_manager.go @@ -18,7 +18,7 @@ func dataSourceNsxtComputeManager() *schema.Resource { Schema: map[string]*schema.Schema{ "id": getDataSourceIDSchema(), - "display_name": getDisplayNameSchema(), + "display_name": getDataSourceExtendedDisplayNameSchema(), "description": getDescriptionSchema(), "server": { Type: schema.TypeString, @@ -45,8 +45,6 @@ func dataSourceNsxtComputeManagerRead(d *schema.ResourceData, m interface{}) err return fmt.Errorf("failed to read ComputeManager %s: %v", objID, err) } obj = objGet - } else if objName == "" { - return fmt.Errorf("error obtaining Compute Manager ID or name during read") } else { // Get by full name/prefix objList, err := client.List(nil, nil, nil, nil, nil, nil, nil) @@ -57,6 +55,11 @@ func dataSourceNsxtComputeManagerRead(d *schema.ResourceData, m interface{}) err var perfectMatch []model.ComputeManager var prefixMatch []model.ComputeManager for _, objInList := range objList.Results { + if len(objName) == 0 { + // We want to grab single compute manager + perfectMatch = append(perfectMatch, objInList) + continue + } if strings.HasPrefix(*objInList.DisplayName, objName) { prefixMatch = append(prefixMatch, objInList) } @@ -66,16 +69,16 @@ func dataSourceNsxtComputeManagerRead(d *schema.ResourceData, m interface{}) err } if len(perfectMatch) > 0 { if len(perfectMatch) > 1 { - return fmt.Errorf("found multiple Compute Managers with name '%s'", objName) + return fmt.Errorf("found multiple Compute Managers matching the criteria") } obj = perfectMatch[0] } else if len(prefixMatch) > 0 { if len(prefixMatch) > 1 { - return fmt.Errorf("found multiple Compute Managers with name starting with '%s'", objName) + return fmt.Errorf("found multiple Compute Managers matching the criteria") } obj = prefixMatch[0] } else { - return fmt.Errorf("Compute Manager with name '%s' was not found", objName) + return fmt.Errorf("No Compute Manager matches the criteria") } } diff --git a/nsxt/data_source_nsxt_compute_manager_test.go b/nsxt/data_source_nsxt_compute_manager_test.go index 80ea21acd..6add184cd 100644 --- a/nsxt/data_source_nsxt_compute_manager_test.go +++ b/nsxt/data_source_nsxt_compute_manager_test.go @@ -11,24 +11,44 @@ import ( ) func TestAccDataSourceNsxtComputeManager_basic(t *testing.T) { - ComputeManagerName := getComputeManagerName() - testResourceName := "data.nsxt_edge_cluster.test" + computeManagerName := getComputeManagerName() + testResourceName := "data.nsxt_compute_manager.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccOnlyLocalManager(t) - testAccTestMP(t) testAccPreCheck(t) testAccEnvDefined(t, "NSXT_TEST_COMPUTE_MANAGER") }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccNSXComputeManagerReadTemplate(ComputeManagerName), + Config: testAccNSXComputeManagerReadTemplate(computeManagerName), Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(testResourceName, "display_name", ComputeManagerName), + resource.TestCheckResourceAttr(testResourceName, "display_name", computeManagerName), + resource.TestCheckResourceAttrSet(testResourceName, "id"), + resource.TestCheckResourceAttrSet(testResourceName, "server"), + ), + }, + }, + }) +} + +func TestAccDataSourceNsxtComputeManager_single(t *testing.T) { + testResourceName := "data.nsxt_compute_manager.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { + testAccOnlyLocalManager(t) + testAccPreCheck(t) + }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccNSXComputeManagerSingleReadTemplate(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(testResourceName, "display_name"), resource.TestCheckResourceAttrSet(testResourceName, "id"), - resource.TestCheckResourceAttrSet(testResourceName, "description"), resource.TestCheckResourceAttrSet(testResourceName, "server"), ), }, @@ -42,3 +62,9 @@ data "nsxt_compute_manager" "test" { display_name = "%s" }`, name) } + +func testAccNSXComputeManagerSingleReadTemplate() string { + return ` +data "nsxt_compute_manager" "test" { +}` +} diff --git a/website/docs/d/compute_manager.html.markdown b/website/docs/d/compute_manager.html.markdown index aa06e3659..49e895f99 100644 --- a/website/docs/d/compute_manager.html.markdown +++ b/website/docs/d/compute_manager.html.markdown @@ -5,7 +5,7 @@ page_title: "NSXT: compute_manager" description: A Compute Manager data source. --- -# nsxt_mac_pool +# nsxt_compute_manager This data source provides information about a Compute Manager configured on NSX. @@ -13,7 +13,6 @@ This data source provides information about a Compute Manager configured on NSX. ```hcl data "nsxt_compute_manager" "test_vcenter" { - display_name = "test-vcenter" } ``` @@ -26,5 +25,5 @@ data "nsxt_compute_manager" "test_vcenter" { In addition to arguments listed above, the following attributes are exported: -* `description` - The description of the MAC pool. -* `server` - IP address or hostname of compute manager. \ No newline at end of file +* `description` - The description of the resource. +* `server` - IP address or hostname of the resource.