This repository has been archived by the owner on Aug 18, 2020. It is now read-only.
forked from vmware/terraform-provider-nsxt
-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_source_nsxt_certificate.go
92 lines (82 loc) · 2.4 KB
/
data_source_nsxt_certificate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* Copyright © 2017 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */
package nsxt
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/vmware/go-vmware-nsxt/trust"
"net/http"
)
func dataSourceNsxtCertificate() *schema.Resource {
return &schema.Resource{
Read: dataSourceNsxtCertificateRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "Unique ID of this resource",
Optional: true,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Description: "The display name of this resource",
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Description: "Description of this resource",
Optional: true,
Computed: true,
},
},
}
}
func dataSourceNsxtCertificateRead(d *schema.ResourceData, m interface{}) error {
// Read cerificate by name or id
nsxClient := m.(nsxtClients).NsxtClient
if nsxClient == nil {
return dataSourceNotSupportedError()
}
objID := d.Get("id").(string)
objName := d.Get("display_name").(string)
var obj trust.Certificate
if objID != "" {
// Get by id
objGet, resp, err := nsxClient.NsxComponentAdministrationApi.GetCertificate(nsxClient.Context, objID, nil)
if resp != nil && resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("certificate %s was not found", objID)
}
if err != nil {
return fmt.Errorf("Error while reading certificate %s: %v", objID, err)
}
obj = objGet
} 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)
}
obj = objInList
found = true
}
}
if !found {
return fmt.Errorf("Certificate with name '%s' was not found", objName)
}
} else {
return fmt.Errorf("Error obtaining certificate ID or name during read")
}
d.SetId(obj.Id)
d.Set("display_name", obj.DisplayName)
d.Set("description", obj.Description)
return nil
}