diff --git a/nsxt/data_source_nsxt_policy_certificate.go b/nsxt/data_source_nsxt_policy_certificate.go index f206c9773..de9d60e9e 100644 --- a/nsxt/data_source_nsxt_policy_certificate.go +++ b/nsxt/data_source_nsxt_policy_certificate.go @@ -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 { @@ -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 }