-
Notifications
You must be signed in to change notification settings - Fork 132
/
resource_fastly_tls_activation.go
128 lines (111 loc) · 3.42 KB
/
resource_fastly_tls_activation.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
package fastly
import (
"github.com/fastly/go-fastly/v3/fastly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"time"
)
func resourceFastlyTLSActivation() *schema.Resource {
return &schema.Resource{
Create: resourceFastlyTLSActivationCreate,
Read: resourceFastlyTLSActivationRead,
Update: resourceFastlyTLSActivationUpdate,
Delete: resourceFastlyTLSActivationDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"certificate_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of certificate to use. Must have the `domain` specified in the certificate's Subject Alternative Names.",
},
"configuration_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "ID of TLS configuration to be used to terminate TLS traffic, or use the default one if missing.",
},
"domain": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Domain to enable TLS on. Must be assigned to an existing Fastly Service.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "Time-stamp (GMT) when TLS was enabled.",
},
},
}
}
func resourceFastlyTLSActivationCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
var configuration *fastly.TLSConfiguration
if v, ok := d.GetOk("configuration_id"); ok {
configuration = &fastly.TLSConfiguration{ID: v.(string)}
}
activation, err := conn.CreateTLSActivation(&fastly.CreateTLSActivationInput{
Certificate: &fastly.CustomTLSCertificate{ID: d.Get("certificate_id").(string)},
Configuration: configuration,
Domain: &fastly.TLSDomain{ID: d.Get("domain").(string)},
})
if err != nil {
return err
}
d.SetId(activation.ID)
return resourceFastlyTLSActivationRead(d, meta)
}
func resourceFastlyTLSActivationRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
activation, err := conn.GetTLSActivation(&fastly.GetTLSActivationInput{
ID: d.Id(),
})
if err != nil {
return err
}
err = d.Set("certificate_id", activation.Certificate.ID)
if err != nil {
return err
}
err = d.Set("configuration_id", activation.Configuration.ID)
if err != nil {
return err
}
err = d.Set("domain", activation.Domain.ID)
if err != nil {
return err
}
err = d.Set("created_at", activation.CreatedAt.Format(time.RFC3339))
if err != nil {
return err
}
return nil
}
func resourceFastlyTLSActivationUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
_, err := conn.UpdateTLSActivation(&fastly.UpdateTLSActivationInput{
ID: d.Id(),
Certificate: &fastly.CustomTLSCertificate{ID: d.Get("certificate_id").(string)},
})
if err != nil {
return err
}
return resourceFastlyTLSActivationRead(d, meta)
}
func resourceFastlyTLSActivationDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*FastlyClient).conn
err := conn.DeleteTLSActivation(&fastly.DeleteTLSActivationInput{
ID: d.Id(),
})
if err != nil {
if httpErr, ok := err.(*fastly.HTTPError); ok && httpErr.IsNotFound() {
log.Printf("[WARN] Error deleting TLS activation (%s), not found. Was a TLS subscription enabled on the same domain?\n", d.Id())
return nil
}
return err
}
return nil
}