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_compute_collection data source #971

Merged
merged 1 commit into from
Sep 21, 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
85 changes: 85 additions & 0 deletions nsxt/data_source_nsxt_compute_collection.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions nsxt/data_source_nsxt_compute_collection_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 8 additions & 0 deletions nsxt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
26 changes: 26 additions & 0 deletions website/docs/d/compute_collection.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions website/docs/d/compute_manager.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.