Skip to content

Commit

Permalink
Fix policy certificate data source
Browse files Browse the repository at this point in the history
The data source used incorrect resource name and thus failed to
work on global manager. We switch to search-based data source
implementation.
  • Loading branch information
annakhm committed Aug 3, 2021
1 parent abaee84 commit 1f3d744
Showing 1 changed file with 3 additions and 60 deletions.
63 changes: 3 additions & 60 deletions nsxt/data_source_nsxt_policy_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
package nsxt

import (
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model"
)

func dataSourceNsxtPolicyCertificate() *schema.Resource {
Expand All @@ -26,64 +21,12 @@ func dataSourceNsxtPolicyCertificate() *schema.Resource {
}

func dataSourceNsxtPolicyCertificateRead(d *schema.ResourceData, m interface{}) error {
if isPolicyGlobalManager(m) {
_, err := policyDataSourceResourceRead(d, getPolicyConnector(m), true, "Certificate", nil)
if err != nil {
return err
}
return nil
}
connector := getPolicyConnector(m)
client := infra.NewDefaultCertificatesClient(connector)

objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
details := false
var obj model.TlsCertificate
if objID != "" {
// Get by id
objGet, err := client.Get(objID, &details)
if err != nil {
return handleDataSourceReadError(d, "Certificate", objID, err)
}
obj = objGet
} else if objName == "" {
return fmt.Errorf("Error obtaining Certificate ID or name during read")
} else {
// Get by full name/prefix
objList, err := client.List(nil, &details, nil, nil, nil, nil, nil)
if err != nil {
return handleListError("Certificate", err)
}
// go over the list to find the correct one (prefer a perfect match. If not - prefix match)
var perfectMatch []model.TlsCertificate
var prefixMatch []model.TlsCertificate
for _, objInList := range objList.Results {
if strings.HasPrefix(*objInList.DisplayName, objName) {
prefixMatch = append(prefixMatch, objInList)
}
if *objInList.DisplayName == objName {
perfectMatch = append(perfectMatch, objInList)
}
}
if len(perfectMatch) > 0 {
if len(perfectMatch) > 1 {
return fmt.Errorf("Found multiple Certificates with name '%s'", objName)
}
obj = perfectMatch[0]
} else if len(prefixMatch) > 0 {
if len(prefixMatch) > 1 {
return fmt.Errorf("Found multiple Certificates with name starting with '%s'", objName)
}
obj = prefixMatch[0]
} else {
return fmt.Errorf("Certificate with name '%s' was not found", objName)
}
_, err := policyDataSourceResourceRead(d, connector, isPolicyGlobalManager(m), "TlsCertificate", nil)
if err != nil {
return err
}

d.SetId(*obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
d.Set("path", obj.Path)
return nil
}

0 comments on commit 1f3d744

Please sign in to comment.