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

Add pagination to certificate data source #1075

Merged
merged 1 commit into from
Jan 10, 2024
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
38 changes: 24 additions & 14 deletions nsxt/data_source_nsxt_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package nsxt

import (
"fmt"

"net/http"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -63,24 +62,35 @@ func dataSourceNsxtCertificateRead(d *schema.ResourceData, m interface{}) error

} else if objName != "" {
// Get by name
// TODO use 2nd parameter localVarOptionals for paging
objList, _, err := nsxClient.NsxComponentAdministrationApi.GetCertificates(nsxClient.Context, nil)
if err != nil {
return fmt.Errorf("Error while reading certificates: %v", err)
}
// go over the list to find the correct one
found := false
for _, objInList := range objList.Results {
if objInList.DisplayName == objName {
if found {
return fmt.Errorf("Found multiple certificates with name '%s'", objName)
lister := func(info *paginationInfo) error {
objList, _, err := nsxClient.NsxComponentAdministrationApi.GetCertificates(nsxClient.Context, info.LocalVarOptionals)
if err != nil {
return fmt.Errorf("Error while reading certificates: %v", err)
}

info.PageCount = int64(len(objList.Results))
info.TotalCount = objList.ResultCount
info.Cursor = objList.Cursor

// go over the list to find the correct one
for _, objInList := range objList.Results {
if objInList.DisplayName == objName {
if found {
return fmt.Errorf("Found multiple certificates with name '%s'", objName)
}
obj = objInList
found = true
}
obj = objInList
found = true
}
return nil
}
total, err := handlePagination(lister)
if err != nil {
return err
}
if !found {
return fmt.Errorf("Certificate with name '%s' was not found", objName)
return fmt.Errorf("Certificate with name '%s' was not found among %d certs", objName, total)
}
} else {
return fmt.Errorf("Error obtaining certificate ID or name during read")
Expand Down