Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pagination in policy group data source #586

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions nsxt/data_source_nsxt_policy_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)
Expand All @@ -26,6 +27,32 @@ func dataSourceNsxtPolicyGroup() *schema.Resource {
}
}

func listPolicyGroups(domain string, connector *client.RestConnector) ([]model.Group, error) {
// Local Manager only
client := domains.NewDefaultGroupsClient(connector)

var results []model.Group
var cursor *string
total := 0

for {
groups, err := client.List(domain, cursor, nil, nil, nil, nil, nil, nil)
if err != nil {
return results, err
}
results = append(results, groups.Results...)
if total == 0 && groups.ResultCount != nil {
// first response
total = int(*groups.ResultCount)
}

cursor = groups.Cursor
if len(results) >= total {
return results, nil
}
}
}

func dataSourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error {
if isPolicyGlobalManager(m) {
domain := d.Get("domain").(string)
Expand Down Expand Up @@ -56,14 +83,15 @@ func dataSourceNsxtPolicyGroupRead(d *schema.ResourceData, m interface{}) error
return fmt.Errorf("Error obtaining Group ID or name during read")
} else {
// Get by full name/prefix
objList, err := client.List(domain, nil, nil, nil, nil, nil, nil, nil)
objList, err := listPolicyGroups(domain, connector)
if err != nil {
return handleListError("Group", err)
}

// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.Group
var prefixMatch []model.Group
for _, objInList := range objList.Results {
for _, objInList := range objList {
if strings.HasPrefix(*objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
Expand Down