Skip to content

Commit

Permalink
Merge pull request #1289 from vmware/vpc-group-data
Browse files Browse the repository at this point in the history
Add VPC group data source
  • Loading branch information
annakhm authored Jul 30, 2024
2 parents e2fee37 + 5236c1d commit dbd0a9b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 17 deletions.
30 changes: 30 additions & 0 deletions nsxt/data_source_nsxt_vpc_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright © 2020 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

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

func dataSourceNsxtVpcGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtVpcGroupRead,

Schema: map[string]*schema.Schema{
"id": getDataSourceIDSchema(),
"display_name": getDataSourceDisplayNameSchema(),
"description": getDataSourceDescriptionSchema(),
"path": getPathSchema(),
"context": getContextSchema(true, false, true),
},
}
}

func dataSourceNsxtVpcGroupRead(d *schema.ResourceData, m interface{}) error {
_, err := policyDataSourceResourceRead(d, getPolicyConnector(m), getSessionContext(d, m), "Group", nil)
if err != nil {
return err
}
return nil
}
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ func Provider() *schema.Provider {
"nsxt_policy_gateway_flood_protection_profile": dataSourceNsxtPolicyGatewayFloodProtectionProfile(),
"nsxt_manager_info": dataSourceNsxtManagerInfo(),
"nsxt_vpc": dataSourceNsxtVPC(),
"nsxt_vpc_group": dataSourceNsxtVpcGroup(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
35 changes: 18 additions & 17 deletions nsxt/resource_nsxt_vpc_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAccResourceNsxtVPCGroup_basicImport(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyGroupIPAddressImportTemplate(name, resourceName, true),
Config: testAccNsxtVPCGroupAddressCreateTemplate(name),
},
{
ResourceName: testResourceName,
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestAccResourceNsxtVPCGroup_addressCriteria(t *testing.T) {
},
Steps: []resource.TestStep{
{
Config: testAccNsxtVPCGroupAddressCreateTemplate(name, resourceName, true),
Config: testAccNsxtVPCGroupAddressCreateTemplate(name),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGroupExists(testResourceName, defaultDomain),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
Expand All @@ -69,7 +69,7 @@ func TestAccResourceNsxtVPCGroup_addressCriteria(t *testing.T) {
),
},
{
Config: testAccNsxtVPCGroupAddressUpdateTemplate(name, resourceName, true),
Config: testAccNsxtVPCGroupAddressUpdateTemplate(name),
Check: resource.ComposeTestCheckFunc(
testAccNsxtPolicyGroupExists(testResourceName, defaultDomain),
resource.TestCheckResourceAttr(testResourceName, "display_name", name),
Expand Down Expand Up @@ -112,13 +112,10 @@ func testAccNsxtVPCGroupCheckDestroy(state *terraform.State, displayName string)
return nil
}

func testAccNsxtVPCGroupAddressCreateTemplate(name, resourceName string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
}
func testAccNsxtVPCGroupAddressCreateTemplate(name string) string {
context := testAccNsxtPolicyMultitenancyContext()
return fmt.Sprintf(`
resource "%s" "test" {
resource "nsxt_vpc_group" "test" {
%s
display_name = "%s"
description = "Acceptance Test"
Expand Down Expand Up @@ -149,16 +146,20 @@ resource "%s" "test" {
tag = "tag2"
}
}
`, resourceName, context, name)
data "nsxt_vpc_group" "test" {
%s
display_name = "%s"
depends_on = [nsxt_vpc_group.test]
}
`, context, name, context, name)
}

func testAccNsxtVPCGroupAddressUpdateTemplate(name, resourceName string, withContext bool) string {
context := ""
if withContext {
context = testAccNsxtPolicyMultitenancyContext()
}
func testAccNsxtVPCGroupAddressUpdateTemplate(name string) string {
context := testAccNsxtPolicyMultitenancyContext()
return fmt.Sprintf(`
resource "%s" "test" {
resource "nsxt_vpc_group" "test" {
%s
display_name = "%s"
description = "Acceptance Test"
Expand All @@ -169,5 +170,5 @@ resource "%s" "test" {
}
}
}
`, resourceName, context, name)
`, context, name)
}
39 changes: 39 additions & 0 deletions website/docs/d/vpc_group.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
subcategory: "VPC"
layout: "nsxt"
page_title: "NSXT: vpc_group"
description: VPC Group data source.
---

# nsxt_vpc_group

This data source provides information about an inventory Group configured under VPC on NSX.

This data source is applicable to NSX Policy Manager.

## Example Usage

```hcl
data "nsxt_vpc_group" "test" {
context {
project_id = "dev"
vpc_id = "test"
}
display_name = "group1"
}
```

## Argument Reference

* `id` - (Optional) The ID of Group to retrieve.
* `display_name` - (Optional) The Display Name prefix of the Group to retrieve.
* `context` - (Required) The context which the object belongs to
* `project_id` - (Required) The ID of the project which the object belongs to
* `vpc_id` - (Required) The ID of the VPC which the object belongs to

## 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 dbd0a9b

Please sign in to comment.