-
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.
Merge pull request #870 from vmware/segment-ds
Support segment data source
- Loading branch information
Showing
4 changed files
with
178 additions
and
0 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,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 | ||
} |
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,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) | ||
} |
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,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. |