From 25081997fb99c1f3de68f4044a6f5ed79d1f7a07 Mon Sep 17 00:00:00 2001 From: Kobi Samoray Date: Mon, 18 Sep 2023 17:39:28 +0300 Subject: [PATCH] Implement nsxt_compute_collection data source Signed-off-by: Kobi Samoray --- nsxt/data_source_nsxt_compute_collection.go | 85 +++++++++++++++++++ ...ata_source_nsxt_compute_collection_test.go | 40 +++++++++ nsxt/provider.go | 1 + nsxt/utils_test.go | 8 ++ .../docs/d/compute_collection.html.markdown | 26 ++++++ website/docs/d/compute_manager.html.markdown | 4 +- 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 nsxt/data_source_nsxt_compute_collection.go create mode 100644 nsxt/data_source_nsxt_compute_collection_test.go create mode 100644 website/docs/d/compute_collection.html.markdown diff --git a/nsxt/data_source_nsxt_compute_collection.go b/nsxt/data_source_nsxt_compute_collection.go new file mode 100644 index 000000000..9089b3958 --- /dev/null +++ b/nsxt/data_source_nsxt_compute_collection.go @@ -0,0 +1,85 @@ +/* Copyright © 2023 VMware, Inc. All Rights Reserved. + SPDX-License-Identifier: MPL-2.0 */ + +package nsxt + +import ( + "fmt" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/fabric" + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model" +) + +func dataSourceNsxtComputeCollection() *schema.Resource { + return &schema.Resource{ + Read: dataSourceNsxtComputeCollectionRead, + + Schema: map[string]*schema.Schema{ + "id": getDataSourceIDSchema(), + "display_name": getDisplayNameSchema(), + "origin_type": { + Type: schema.TypeString, + Optional: true, + Description: "ComputeCollection type like VC_Cluster. Here the Compute Manager type prefix would help in differentiating similar named Compute Collection types from different Compute Managers", + }, + "origin_id": { + Type: schema.TypeString, + Optional: true, + Description: "Id of the compute manager from where this Compute Collection was discovered", + }, + "cm_local_id": { + Type: schema.TypeString, + Optional: true, + Description: "Local Id of the compute collection in the Compute Manager", + }, + }, + } +} + +func nullIfEmpty(s string) *string { + if s == "" { + return nil + } + return &s +} + +func dataSourceNsxtComputeCollectionRead(d *schema.ResourceData, m interface{}) error { + connector := getPolicyConnector(m) + client := fabric.NewComputeCollectionsClient(connector) + objID := d.Get("id").(string) + + objName := nullIfEmpty(d.Get("display_name").(string)) + objOrigin := nullIfEmpty(d.Get("origin_id").(string)) + objType := nullIfEmpty(d.Get("origin_type").(string)) + objLocalID := nullIfEmpty(d.Get("cm_local_id").(string)) + + var obj model.ComputeCollection + + if objID != "" { + // Get by id + objGet, err := client.Get(objID) + if err != nil { + return fmt.Errorf("failed to read ComputeCollection %s: %v", objID, err) + } + obj = objGet + } else { + objList, err := client.List(objLocalID, nil, nil, objName, nil, nil, nil, objOrigin, objType, nil, nil, nil, nil) + if err != nil { + return fmt.Errorf("failed to read Compute Collections: %v", err) + } else if *objList.ResultCount == 0 { + return fmt.Errorf("no Compute Collections that matched the specified parameters found") + } else if *objList.ResultCount > 1 { + return fmt.Errorf("found multiple Compute Collections that matched specified parameters") + } + obj = objList.Results[0] + } + + d.SetId(*obj.ExternalId) + d.Set("display_name", obj.DisplayName) + d.Set("origin_type", obj.OriginType) + d.Set("origin_id", obj.OriginId) + d.Set("cm_local_id", obj.CmLocalId) + + return nil +} diff --git a/nsxt/data_source_nsxt_compute_collection_test.go b/nsxt/data_source_nsxt_compute_collection_test.go new file mode 100644 index 000000000..342d819c4 --- /dev/null +++ b/nsxt/data_source_nsxt_compute_collection_test.go @@ -0,0 +1,40 @@ +/* Copyright © 2017 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 TestAccDataSourceNsxtComputeCollection_basic(t *testing.T) { + ComputeCollectionName := getComputeCollectionName() + testResourceName := "data.nsxt_compute_collection.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccOnlyLocalManager(t); testAccTestMP(t); testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccNSXComputeCollectionReadTemplate(ComputeCollectionName), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(testResourceName, "display_name", ComputeCollectionName), + resource.TestCheckResourceAttr(testResourceName, "origin_type", "VC_Cluster"), + resource.TestCheckResourceAttrSet(testResourceName, "id"), + resource.TestCheckResourceAttrSet(testResourceName, "origin_id"), + resource.TestCheckResourceAttrSet(testResourceName, "cm_local_id"), + ), + }, + }, + }) +} + +func testAccNSXComputeCollectionReadTemplate(name string) string { + return fmt.Sprintf(` +data "nsxt_compute_collection" "test" { + display_name = "%s" +}`, name) +} diff --git a/nsxt/provider.go b/nsxt/provider.go index aa7592d60..c0f77437e 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -295,6 +295,7 @@ func Provider() *schema.Provider { "nsxt_compute_manager": dataSourceNsxtComputeManager(), "nsxt_transport_node_realization": dataSourceNsxtTransportNodeRealization(), "nsxt_failure_domain": dataSourceNsxtFailureDomain(), + "nsxt_compute_collection": dataSourceNsxtComputeCollection(), }, ResourcesMap: map[string]*schema.Resource{ diff --git a/nsxt/utils_test.go b/nsxt/utils_test.go index b08809b15..e75d0c346 100644 --- a/nsxt/utils_test.go +++ b/nsxt/utils_test.go @@ -114,6 +114,14 @@ func getEdgeClusterName() string { return name } +func getComputeCollectionName() string { + name := os.Getenv("NSXT_TEST_COMPUTE_COLLECTION") + if name == "" { + name = edgeClusterDefaultName + } + return name +} + func getComputeManagerName() string { return os.Getenv("NSXT_TEST_COMPUTE_MANAGER") } diff --git a/website/docs/d/compute_collection.html.markdown b/website/docs/d/compute_collection.html.markdown new file mode 100644 index 000000000..940b94e1e --- /dev/null +++ b/website/docs/d/compute_collection.html.markdown @@ -0,0 +1,26 @@ +--- +subcategory: "Fabric" +layout: "nsxt" +page_title: "NSXT: compute_collection" +description: A Compute Collection data source. +--- + +# nsxt_mac_pool + +This data source provides information about a Compute Collection configured on NSX. + +## Example Usage + +```hcl +data "nsxt_compute_collection" "test_cluster" { + display_name = "Compute_Cluster" +} +``` + +## Argument Reference + +* `id` - (Optional) The ID of Compute Collection to retrieve. +* `display_name` - (Optional) The Display Name of the Compute Collection to retrieve. +* `origin_type` - (Optional) ComputeCollection type like VC_Cluster. Here the Compute Manager type prefix would help in differentiating similar named Compute Collection types from different Compute Managers. +* `origin_id` - (Optional) Id of the compute manager from where this Compute Collection was discovered. +* `cm_local_id` - (Optional) Local Id of the compute collection in the Compute Manager. diff --git a/website/docs/d/compute_manager.html.markdown b/website/docs/d/compute_manager.html.markdown index aa06e3659..45228666b 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. @@ -26,5 +26,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. +* `description` - The description of the Compute Manager. * `server` - IP address or hostname of compute manager. \ No newline at end of file