-
Notifications
You must be signed in to change notification settings - Fork 10
/
data_source_appgate_certificate_authority.go
96 lines (88 loc) · 2.33 KB
/
data_source_appgate_certificate_authority.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
93
94
95
96
package appgate
import (
"context"
"log"
"github.com/appgate/sdp-api-client-go/api/v14/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceAppgateCertificateAuthority() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceAppgateCertificateAuthorityRead,
Schema: map[string]*schema.Schema{
"pem": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Get the current CA Certificate in PEM format.",
},
"version": {
Type: schema.TypeInt,
Computed: true,
},
"serial": {
Type: schema.TypeString,
Computed: true,
},
"issuer": {
Type: schema.TypeString,
Computed: true,
},
"subject": {
Type: schema.TypeString,
Computed: true,
},
"valid_from": {
Type: schema.TypeString,
Computed: true,
},
"valid_to": {
Type: schema.TypeString,
Computed: true,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"certificate": {
Type: schema.TypeString,
Computed: true,
},
"subject_public_key": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceAppgateCertificateAuthorityRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[DEBUG] Data source CA Certificate.")
var diags diag.Diagnostics
api := meta.(*Client).API.CAApi
if pem, ok := d.GetOk("pem"); pem.(bool) && ok {
cert, _, err := api.CertificateAuthorityCaPemGet(ctx).Execute()
if err != nil {
return diag.FromErr(err)
}
setCertificate(d, cert)
} else {
cert, _, err := api.CertificateAuthorityCaGet(ctx).Execute()
if err != nil {
return diag.FromErr(err)
}
setCertificate(d, cert)
}
return diags
}
func setCertificate(d *schema.ResourceData, cert openapi.CertificateDetails) {
d.Set("version", int(cert.GetVersion()))
d.Set("serial", cert.GetSerial())
d.Set("issuer", cert.GetIssuer())
d.Set("subject", cert.GetSubject())
d.Set("valid_from", cert.GetValidFrom().String())
d.Set("valid_to", cert.GetValidTo().String())
d.Set("fingerprint", cert.GetFingerprint())
d.Set("certificate", cert.GetCertificate())
d.Set("subject_public_key", cert.GetSubjectPublicKey())
d.SetId(cert.GetFingerprint())
}