-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement nsxt_compute_collection data source
Signed-off-by: Kobi Samoray <[email protected]>
- Loading branch information
Showing
6 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters