From 5236c1d1d2065f1ecea5587befe982a8106a8d07 Mon Sep 17 00:00:00 2001 From: Anna Khmelnitsky Date: Tue, 30 Jul 2024 00:10:16 +0000 Subject: [PATCH] Add VPC group data source Test for this data source is covered together with VPC group resource test. Signed-off-by: Anna Khmelnitsky --- nsxt/data_source_nsxt_vpc_group.go | 30 ++++++++++++++++++++ nsxt/provider.go | 1 + nsxt/resource_nsxt_vpc_group_test.go | 35 ++++++++++++----------- website/docs/d/vpc_group.html.markdown | 39 ++++++++++++++++++++++++++ 4 files changed, 88 insertions(+), 17 deletions(-) create mode 100644 nsxt/data_source_nsxt_vpc_group.go create mode 100644 website/docs/d/vpc_group.html.markdown diff --git a/nsxt/data_source_nsxt_vpc_group.go b/nsxt/data_source_nsxt_vpc_group.go new file mode 100644 index 000000000..4519ffbba --- /dev/null +++ b/nsxt/data_source_nsxt_vpc_group.go @@ -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 +} diff --git a/nsxt/provider.go b/nsxt/provider.go index 66e28d00d..54e90142c 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -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{ diff --git a/nsxt/resource_nsxt_vpc_group_test.go b/nsxt/resource_nsxt_vpc_group_test.go index cbb44e594..9a0a2b0c3 100644 --- a/nsxt/resource_nsxt_vpc_group_test.go +++ b/nsxt/resource_nsxt_vpc_group_test.go @@ -25,7 +25,7 @@ func TestAccResourceNsxtVPCGroup_basicImport(t *testing.T) { }, Steps: []resource.TestStep{ { - Config: testAccNsxtPolicyGroupIPAddressImportTemplate(name, resourceName, true), + Config: testAccNsxtVPCGroupAddressCreateTemplate(name), }, { ResourceName: testResourceName, @@ -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), @@ -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), @@ -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" @@ -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" @@ -169,5 +170,5 @@ resource "%s" "test" { } } } -`, resourceName, context, name) +`, context, name) } diff --git a/website/docs/d/vpc_group.html.markdown b/website/docs/d/vpc_group.html.markdown new file mode 100644 index 000000000..a010c1815 --- /dev/null +++ b/website/docs/d/vpc_group.html.markdown @@ -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.