-
Notifications
You must be signed in to change notification settings - Fork 137
/
resource_fastly_tls_platform_certificate.go
179 lines (158 loc) · 5.62 KB
/
resource_fastly_tls_platform_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package fastly
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"time"
"github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceFastlyTLSPlatformCertificate() *schema.Resource {
return &schema.Resource{
CreateContext: resourceFastlyTLSPlatformCertificateCreate,
ReadContext: resourceFastlyTLSPlatformCertificateRead,
UpdateContext: resourceFastlyTLSPlatformCertificateUpdate,
DeleteContext: resourceFastlyTLSPlatformCertificateDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"certificate_body": {
Type: schema.TypeString,
Description: "PEM-formatted certificate.",
Required: true,
ValidateDiagFunc: validatePEMBlock("CERTIFICATE"),
},
"intermediates_blob": {
Type: schema.TypeString,
Description: "PEM-formatted certificate chain from the `certificate_body` to its root.",
Required: true,
ValidateDiagFunc: validatePEMBlocks("CERTIFICATE"),
},
"configuration_id": {
Type: schema.TypeString,
Description: "ID of TLS configuration to be used to terminate TLS traffic.",
Required: true,
ForceNew: true,
},
"allow_untrusted_root": {
Type: schema.TypeBool,
Description: "Disable checking whether the root of the certificate chain is trusted. Useful for development purposes to allow use of self-signed CAs. Defaults to false. Write-only on create.",
Optional: true,
Default: false,
},
"not_after": {
Type: schema.TypeString,
Description: "Timestamp (GMT) when the certificate will expire.",
Computed: true,
},
"not_before": {
Type: schema.TypeString,
Description: "Timestamp (GMT) when the certificate will become valid.",
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Description: "Timestamp (GMT) when the certificate was created.",
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Description: "Timestamp (GMT) when the certificate was last updated.",
Computed: true,
},
"replace": {
Type: schema.TypeBool,
Description: "A recommendation from Fastly indicating the key associated with this certificate is in need of rotation.",
Computed: true,
},
"domains": {
Type: schema.TypeSet,
Description: "All the domains (including wildcard domains) that are listed in any certificate's Subject Alternative Names (SAN) list.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func resourceFastlyTLSPlatformCertificateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
input := &fastly.CreateBulkCertificateInput{
CertBlob: d.Get("certificate_body").(string),
IntermediatesBlob: d.Get("intermediates_blob").(string),
Configurations: []*fastly.TLSConfiguration{{
ID: d.Get("configuration_id").(string),
}},
AllowUntrusted: d.Get("allow_untrusted_root").(bool),
}
certificate, err := conn.CreateBulkCertificate(input)
if err != nil {
return diag.FromErr(err)
}
d.SetId(certificate.ID)
return resourceFastlyTLSPlatformCertificateRead(ctx, d, meta)
}
func resourceFastlyTLSPlatformCertificateRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
var diags diag.Diagnostics
certificate, err := conn.GetBulkCertificate(&fastly.GetBulkCertificateInput{
ID: d.Id(),
})
if err != nil {
return diag.FromErr(err)
}
var domains []string
for _, domain := range certificate.Domains {
domains = append(domains, domain.ID)
}
if certificate.Replace {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Fastly recommends that this certificate (%s) be replaced", certificate.ID),
})
}
if err := d.Set("configuration_id", certificate.Configurations[0].ID); err != nil {
return diag.FromErr(err)
}
if err := d.Set("not_after", certificate.NotAfter.Format(time.RFC3339)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("not_before", certificate.NotBefore.Format(time.RFC3339)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("created_at", certificate.CreatedAt.Format(time.RFC3339)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("updated_at", certificate.UpdatedAt.Format(time.RFC3339)); err != nil {
return diag.FromErr(err)
}
if err := d.Set("replace", certificate.Replace); err != nil {
return diag.FromErr(err)
}
if err := d.Set("domains", domains); err != nil {
return diag.FromErr(err)
}
return diags
}
func resourceFastlyTLSPlatformCertificateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
_, err := conn.UpdateBulkCertificate(&fastly.UpdateBulkCertificateInput{
ID: d.Id(),
CertBlob: d.Get("certificate_body").(string),
IntermediatesBlob: d.Get("intermediates_blob").(string),
AllowUntrusted: d.Get("allow_untrusted_root").(bool),
})
if err != nil {
return diag.FromErr(err)
}
return resourceFastlyTLSPlatformCertificateRead(ctx, d, meta)
}
func resourceFastlyTLSPlatformCertificateDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*FastlyClient).conn
if err := conn.DeleteBulkCertificate(&fastly.DeleteBulkCertificateInput{
ID: d.Id(),
}); err != nil {
return diag.FromErr(err)
}
return nil
}