forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_heroku_cert.go
133 lines (105 loc) · 3.21 KB
/
resource_heroku_cert.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
package heroku
import (
"context"
"fmt"
"log"
"github.com/cyberdelia/heroku-go/v3"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceHerokuCert() *schema.Resource {
return &schema.Resource{
Create: resourceHerokuCertCreate,
Read: resourceHerokuCertRead,
Update: resourceHerokuCertUpdate,
Delete: resourceHerokuCertDelete,
Schema: map[string]*schema.Schema{
"app": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"certificate_chain": {
Type: schema.TypeString,
Required: true,
},
"private_key": {
Type: schema.TypeString,
Required: true,
},
"cname": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceHerokuCertCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
app := d.Get("app").(string)
preprocess := true
opts := heroku.SSLEndpointCreateOpts{
CertificateChain: d.Get("certificate_chain").(string),
Preprocess: &preprocess,
PrivateKey: d.Get("private_key").(string)}
log.Printf("[DEBUG] SSL Certificate create configuration: %#v, %#v", app, opts)
a, err := client.SSLEndpointCreate(context.TODO(), app, opts)
if err != nil {
return fmt.Errorf("Error creating SSL endpoint: %s", err)
}
d.SetId(a.ID)
log.Printf("[INFO] SSL Certificate ID: %s", d.Id())
return resourceHerokuCertRead(d, meta)
}
func resourceHerokuCertRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
cert, err := resourceHerokuSSLCertRetrieve(
d.Get("app").(string), d.Id(), client)
if err != nil {
return err
}
d.Set("certificate_chain", cert.CertificateChain)
d.Set("name", cert.Name)
d.Set("cname", cert.CName)
return nil
}
func resourceHerokuCertUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
app := d.Get("app").(string)
preprocess := true
rollback := false
opts := heroku.SSLEndpointUpdateOpts{
CertificateChain: heroku.String(d.Get("certificate_chain").(string)),
Preprocess: &preprocess,
PrivateKey: heroku.String(d.Get("private_key").(string)),
Rollback: &rollback}
if d.HasChange("certificate_chain") || d.HasChange("private_key") {
log.Printf("[DEBUG] SSL Certificate update configuration: %#v, %#v", app, opts)
_, err := client.SSLEndpointUpdate(context.TODO(), app, d.Id(), opts)
if err != nil {
return fmt.Errorf("Error updating SSL endpoint: %s", err)
}
}
return resourceHerokuCertRead(d, meta)
}
func resourceHerokuCertDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*heroku.Service)
log.Printf("[INFO] Deleting SSL Cert: %s", d.Id())
// Destroy the app
_, err := client.SSLEndpointDelete(context.TODO(), d.Get("app").(string), d.Id())
if err != nil {
return fmt.Errorf("Error deleting SSL Cert: %s", err)
}
d.SetId("")
return nil
}
func resourceHerokuSSLCertRetrieve(app string, id string, client *heroku.Service) (*heroku.SSLEndpointInfoResult, error) {
addon, err := client.SSLEndpointInfo(context.TODO(), app, id)
if err != nil {
return nil, fmt.Errorf("Error retrieving SSL Cert: %s", err)
}
return addon, nil
}