-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_source_appgate_trusted_certificate.go
84 lines (73 loc) · 2.51 KB
/
data_source_appgate_trusted_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
package appgate
import (
"context"
"fmt"
"log"
"github.com/appgate/sdp-api-client-go/api/v15/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceAppgateTrustedCertificate() *schema.Resource {
return &schema.Resource{
Read: dataSourceAppgateTrustedCertificateRead,
Schema: map[string]*schema.Schema{
"trusted_certificate_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"trusted_certificate_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func dataSourceAppgateTrustedCertificateRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Data source trusted certificate")
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.TrustedCertificatesApi
trustedCertID, iok := d.GetOk("trusted_certificate_id")
trustedCertName, nok := d.GetOk("trusted_certificate_name")
if !iok && !nok {
return fmt.Errorf("please provide one of trusted_certificate_id or trusted_certificate_name attributes")
}
var reqErr error
var trustedCert *openapi.TrustedCertificate
if iok {
trustedCert, reqErr = findTrustedCertificateByUUID(api, trustedCertID.(string), token)
} else {
trustedCert, reqErr = findTrustedCertificateByName(api, trustedCertName.(string), token)
}
if reqErr != nil {
return reqErr
}
log.Printf("[DEBUG] Got trusted certificate: %+v", trustedCert.Id)
d.SetId(trustedCert.Id)
d.Set("trusted_certificate_name", trustedCert.Name)
d.Set("trusted_certificate_id", trustedCert.Id)
return nil
}
func findTrustedCertificateByUUID(api *openapi.TrustedCertificatesApiService, id string, token string) (*openapi.TrustedCertificate, error) {
log.Printf("[DEBUG] Data source trusted_certificate get by UUID %s", id)
trustedCert, _, err := api.TrustedCertificatesIdGet(context.Background(), id).Authorization(token).Execute()
if err != nil {
return nil, err
}
return &trustedCert, nil
}
func findTrustedCertificateByName(api *openapi.TrustedCertificatesApiService, name string, token string) (*openapi.TrustedCertificate, error) {
log.Printf("[DEBUG] Data trusted_certificate get by name %s", name)
request := api.TrustedCertificatesGet(context.Background())
trustedCert, _, err := request.Query(name).OrderBy("name").Range_("0-1").Authorization(token).Execute()
if err != nil {
return nil, err
}
for _, s := range trustedCert.GetData() {
return &s, nil
}
return nil, fmt.Errorf("Failed to find trusted_certificate %s", name)
}