-
Notifications
You must be signed in to change notification settings - Fork 21
/
data_source_certificate_manager.go
120 lines (102 loc) · 3.19 KB
/
data_source_certificate_manager.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package ionoscloud
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
certmanager "github.com/ionos-cloud/sdk-go-cert-manager"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services"
"github.com/ionos-cloud/terraform-provider-ionoscloud/v6/services/cert"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceCertificate() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCertificateRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"certificate": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"certificate_chain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
Timeouts: &resourceDefaultTimeouts,
}
}
func dataSourceCertificateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(services.SdkBundle).CertManagerClient
var name, idStr string
id, idOk := d.GetOk("id")
if idOk {
idStr = id.(string)
}
t, nameOk := d.GetOk("name")
if nameOk {
name = t.(string)
}
var certificate certmanager.CertificateDto
var err error
if !idOk && !nameOk {
return diag.FromErr(fmt.Errorf("either id, or name must be set"))
}
if idOk {
certificate, _, err = client.GetCertificate(ctx, idStr)
if err != nil {
return diag.FromErr(fmt.Errorf("error getting certificate with id %s %w", idStr, err))
}
if nameOk {
if certificate.Properties != nil && certificate.Properties.Name != nil &&
strings.EqualFold(*certificate.Properties.Name, name) {
return diag.FromErr(fmt.Errorf("name of cert (UUID=%s, name=%s) does not match expected name: %s",
*certificate.Id, *certificate.Properties.Name, name))
}
}
if certificate.Properties != nil {
log.Printf("[INFO] Got certificate [Name=%s]", *certificate.Properties.Name)
}
} else {
log.Printf("[INFO] Using data source for certificate with name: %s", name)
certificates, _, err := client.ListCertificates(ctx)
if err != nil {
return diag.FromErr(fmt.Errorf("an error occured while fetching certificates: %w ", err))
}
var results []certmanager.CertificateDto
if certificates.Items != nil {
var certsFound []certmanager.CertificateDto
for _, certItem := range *certificates.Items {
if certItem.Properties != nil && certItem.Properties.Name != nil && *certItem.Properties.Name == name {
certsFound = append(certsFound, certItem)
}
}
if certsFound == nil {
return diag.FromErr(fmt.Errorf("no certificate found with the specified criteria: name = %s", name))
} else {
results = certsFound
}
}
if results == nil || len(results) == 0 {
return diag.FromErr(fmt.Errorf("no certificate found with the specified criteria: name = %s", name))
} else if len(results) > 1 {
return diag.FromErr(fmt.Errorf("more than one certificate found with the specified criteria: name = %s", name))
} else {
certificate = results[0]
}
}
if err := cert.SetCertificateData(d, &certificate); err != nil {
return diag.FromErr(err)
}
return nil
}