Skip to content

Commit

Permalink
Merge pull request #870 from vmware/segment-ds
Browse files Browse the repository at this point in the history
Support segment data source
  • Loading branch information
annakhm authored Apr 17, 2023
2 parents 0fe633a + 2aeaaec commit 0ac9163
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 0 deletions.
32 changes: 32 additions & 0 deletions nsxt/data_source_nsxt_policy_segment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceNsxtPolicySegment() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtPolicySegmentRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"display_name": getDataSourceExtendedDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
"path": getPathSchema(),
},
}
}

func dataSourceNsxtPolicySegmentRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

_, err := policyDataSourceResourceRead(d, connector, isPolicyGlobalManager(m), "Segment", nil)
if err != nil {
return err
}

return nil
}
112 changes: 112 additions & 0 deletions nsxt/data_source_nsxt_policy_segment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"testing"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
gm_infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/global_infra"
gm_model "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-gm/model"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

func TestAccDataSourceNsxtPolicySegment_basic(t *testing.T) {
name := getAccTestDataSourceName()
testResourceName := "data.nsxt_policy_segment.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccDataSourceNsxtPolicySegmentDeleteByName(name)
},
Steps: []resource.TestStep{
{
PreConfig: func() {
if err := testAccDataSourceNsxtPolicySegmentCreate(name); err != nil {
panic(err)
}
},
Config: testAccNsxtPolicySegmentReadTemplate(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
resource.TestCheckResourceAttr(testResourceName, "description", name),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
),
},
},
})
}

func testAccDataSourceNsxtPolicySegmentCreate(name string) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}

displayName := name
description := name
obj := model.Segment{
Description: &description,
DisplayName: &displayName,
}

// Generate a random ID for the resource
uuid, _ := uuid.NewRandom()
id := uuid.String()

if testAccIsGlobalManager() {
gmObj, convErr := convertModelBindingType(obj, model.SegmentBindingType(), gm_model.SegmentBindingType())
if convErr != nil {
return convErr
}

client := gm_infra.NewSegmentsClient(connector)
err = client.Patch(id, gmObj.(gm_model.Segment))

} else {
client := infra.NewSegmentsClient(connector)
err = client.Patch(id, obj)
}
if err != nil {
return fmt.Errorf("Error during Segment creation: %v", err)
}
return nil
}

func testAccDataSourceNsxtPolicySegmentDeleteByName(name string) error {
connector, err := testAccGetPolicyConnector()
if err != nil {
return fmt.Errorf("Error during test client initialization: %v", err)
}

// Find the object by name
objID, err := testGetObjIDByName(name, "Segment")
if err != nil {
return nil
}
if testAccIsGlobalManager() {
client := gm_infra.NewSegmentsClient(connector)
err = client.Delete(objID)
} else {
client := infra.NewSegmentsClient(connector)
err = client.Delete(objID)
}
if err != nil {
return fmt.Errorf("Error during Segment deletion: %v", err)
}
return nil
}

func testAccNsxtPolicySegmentReadTemplate(name string) string {
return fmt.Sprintf(`
data "nsxt_policy_segment" "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 @@ -266,6 +266,7 @@ func Provider() *schema.Provider {
"nsxt_policy_ipsec_vpn_local_endpoint": dataSourceNsxtPolicyIPSecVpnLocalEndpoint(),
"nsxt_policy_ipsec_vpn_service": dataSourceNsxtPolicyIPSecVpnService(),
"nsxt_policy_l2_vpn_service": dataSourceNsxtPolicyL2VpnService(),
"nsxt_policy_segment": dataSourceNsxtPolicySegment(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
33 changes: 33 additions & 0 deletions website/docs/d/policy_segment.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "Policy - Segments"
layout: "nsxt"
page_title: "NSXT: policy_segment"
description: Policy Segment data source.
---

# nsxt_policy_segment

This data source provides information about policy Segment configured on NSX.
This data source is applicable to NSX Global Manager, NSX Policy Manager and VMC.

## Example Usage

```hcl
data "nsxt_policy_segment" "test" {
display_name = "segment1"
}
```

## Argument Reference

* `id` - (Optional) The ID of Segment to retrieve. If ID is specified, no additional argument should be configured.

* `display_name` - (Optional) The Display Name prefix of the Segment to retrieve.

## Attributes Reference

In addition to arguments listed above, the following attributes are exported:

* `description` - The description of the resource.

* `path` - The NSX path of the policy resource.

0 comments on commit 0ac9163

Please sign in to comment.