Skip to content

Commit 5aa5075

Browse files
authored
Adding data source for retrieving the tags based on a scope.
Added data source for retrieving the tags based on a scope. Added acceptance tests for different cases of the data source usage. Added documentation for the data source added to be updated in the hashicorp doc.
2 parents 40b4937 + 4c260ad commit 5aa5075

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

nsxt/data_source_nsxt_policy_tags.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package nsxt
6+
7+
import (
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
10+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
11+
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
12+
)
13+
14+
func dataSourceNsxtTags() *schema.Resource {
15+
return &schema.Resource{
16+
Read: dataSourceNsxtTagsRead,
17+
Schema: map[string]*schema.Schema{
18+
"scope": getRequiredStringSchema("The scope of the tags to retrieve."),
19+
"items": {
20+
Type: schema.TypeList,
21+
Description: "List of tags based on the scope.",
22+
Computed: true,
23+
Elem: &schema.Schema{
24+
Type: schema.TypeString,
25+
},
26+
},
27+
},
28+
}
29+
}
30+
31+
func dataSourceNsxtTagsRead(d *schema.ResourceData, m interface{}) error {
32+
connector := getPolicyConnector(m)
33+
scope := d.Get("scope").(string)
34+
tagsList, err := listTags(connector, scope)
35+
if err != nil {
36+
return err
37+
}
38+
39+
var tags []string
40+
for _, tag := range tagsList {
41+
tags = append(tags, *tag.Tag)
42+
}
43+
d.Set("items", tags)
44+
45+
d.SetId(newUUID())
46+
47+
return nil
48+
}
49+
50+
func listTags(connector client.Connector, scope string) ([]model.TagInfo, error) {
51+
client := infra.NewTagsClient(connector)
52+
if client == nil {
53+
return nil, policyResourceNotSupportedError()
54+
}
55+
var cursor *string
56+
var tagsListResults []model.TagInfo
57+
total := 0
58+
for {
59+
tagsList, err := client.List(cursor, nil, nil, nil, nil, &scope, nil, nil, nil, nil)
60+
if err != nil {
61+
return nil, err
62+
}
63+
tagsListResults = append(tagsListResults, tagsList.Results...)
64+
if total == 0 && tagsList.ResultCount != nil {
65+
total = int(*tagsList.ResultCount)
66+
}
67+
68+
cursor = tagsList.Cursor
69+
if len(tagsListResults) >= total {
70+
return tagsListResults, nil
71+
}
72+
}
73+
74+
}
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// © Broadcom. All Rights Reserved.
2+
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: MPL-2.0
4+
5+
package nsxt
6+
7+
import (
8+
"fmt"
9+
"regexp"
10+
"testing"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13+
)
14+
15+
func TestAccDataSourceNsxtPolicyTags_basic(t *testing.T) {
16+
tagName := "testTag"
17+
emptyScopeTag := "testEmptyScopeTag"
18+
transportZone := getOverlayTransportZoneName()
19+
re, _ := regexp.Compile(`.*EmptyScope.*`)
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() {
22+
testAccPreCheck(t)
23+
testAccOnlyLocalManager(t)
24+
},
25+
Providers: testAccProviders,
26+
Steps: []resource.TestStep{
27+
{
28+
Config: testAccNSXPolicyTagsReadTemplate(tagName, emptyScopeTag, transportZone),
29+
Check: resource.ComposeTestCheckFunc(
30+
resource.TestCheckOutput("nsxt_tags", tagName),
31+
resource.TestMatchOutput("empty_nsxt_tags", re),
32+
resource.TestCheckOutput("wildcard_nsxt_tags", tagName),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccNSXPolicyTagsReadTemplate(tagName string, emptyScopeTag string, transportZone string) string {
40+
return fmt.Sprintf(`
41+
resource "nsxt_policy_segment" "segment1" {
42+
display_name = "segment1"
43+
description = "Terraform provisioned Segment"
44+
transport_zone_path = data.nsxt_policy_transport_zone.overlay_transport_zone.path
45+
tag {
46+
scope = "scope-test"
47+
tag = "%s"
48+
}
49+
tag {
50+
tag = "%s"
51+
}
52+
53+
}
54+
55+
data "nsxt_policy_transport_zone" "overlay_transport_zone" {
56+
display_name = "%s"
57+
}
58+
59+
data "nsxt_policy_tags" "tags" {
60+
scope = "scope-test"
61+
depends_on = [nsxt_policy_segment.segment1]
62+
}
63+
64+
output "nsxt_tags" {
65+
value = data.nsxt_policy_tags.tags.items[0]
66+
}
67+
68+
data "nsxt_policy_tags" "emptytags" {
69+
scope = ""
70+
depends_on = [nsxt_policy_segment.segment1]
71+
}
72+
73+
output "empty_nsxt_tags" {
74+
value = join("--",data.nsxt_policy_tags.emptytags.items)
75+
}
76+
77+
data "nsxt_policy_tags" "wildcardscope" {
78+
scope = "*test"
79+
depends_on = [nsxt_policy_segment.segment1]
80+
}
81+
82+
output "wildcard_nsxt_tags" {
83+
value = data.nsxt_policy_tags.wildcardscope.items[0]
84+
}
85+
86+
`, tagName, emptyScopeTag, transportZone)
87+
}

nsxt/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ func Provider() *schema.Provider {
291291
"nsxt_policy_gateway_policy": dataSourceNsxtPolicyGatewayPolicy(),
292292
"nsxt_policy_security_policy": dataSourceNsxtPolicySecurityPolicy(),
293293
"nsxt_policy_group": dataSourceNsxtPolicyGroup(),
294+
"nsxt_policy_tags": dataSourceNsxtTags(),
294295
"nsxt_policy_context_profile": dataSourceNsxtPolicyContextProfile(),
295296
"nsxt_policy_dhcp_server": dataSourceNsxtPolicyDhcpServer(),
296297
"nsxt_policy_bfd_profile": dataSourceNsxtPolicyBfdProfile(),
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
subcategory: "Beta"
3+
layout: "nsxt"
4+
page_title: "NSXT: policy_tags"
5+
description: Tags data source.
6+
---
7+
8+
# nsxt_policy_tags
9+
10+
This data source provides the list of tags with a particular scope.
11+
12+
This data source is applicable to NSX Policy Manager.
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "nsxt_policy_tags" "tags" {
18+
scope = "dev"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
* `scope` - (Required) The scope of the tags to retrieve. Supports starts with, ends with, equals, and contains filters.
25+
Use * as a suffix for "starts with" and a prefix for "ends with". For "contains," wrap the value with *. Use * alone to fetch all tags, irrespective scope.
26+
27+
## Attributes Reference
28+
29+
The following attributes are exported:
30+
31+
* `items` - List of tags with the scope given in the argument.

0 commit comments

Comments
 (0)