2014-11-27 12:12:34 +01:00
|
|
|
package heroku
|
|
|
|
|
|
|
|
import (
|
2017-03-10 13:00:03 +01:00
|
|
|
"context"
|
2014-11-27 12:12:34 +01:00
|
|
|
"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{
|
2017-03-10 13:00:03 +01:00
|
|
|
"app": {
|
2014-11-27 12:12:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
"certificate_chain": {
|
2014-11-27 12:12:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
"private_key": {
|
2014-11-27 12:12:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
"cname": {
|
2014-11-27 12:12:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
"name": {
|
2014-11-27 12:12:34 +01:00
|
|
|
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)
|
2017-03-10 13:00:03 +01:00
|
|
|
a, err := client.SSLEndpointCreate(context.TODO(), app, opts)
|
2014-11-27 12:12:34 +01:00
|
|
|
if err != nil {
|
2016-02-04 16:57:44 +01:00
|
|
|
return fmt.Errorf("Error creating SSL endpoint: %s", err)
|
2014-11-27 12:12:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-12-15 15:10:59 +01:00
|
|
|
cert, err := resourceHerokuSSLCertRetrieve(
|
2014-11-27 12:12:34 +01:00
|
|
|
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)
|
2017-05-10 22:52:55 +02:00
|
|
|
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}
|
2014-11-27 12:12:34 +01:00
|
|
|
|
2017-05-10 22:52:55 +02:00
|
|
|
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)
|
2014-11-27 12:12:34 +01:00
|
|
|
if err != nil {
|
2017-05-10 22:52:55 +02:00
|
|
|
return fmt.Errorf("Error updating SSL endpoint: %s", err)
|
2014-11-27 12:12:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2017-03-10 13:00:03 +01:00
|
|
|
_, err := client.SSLEndpointDelete(context.TODO(), d.Get("app").(string), d.Id())
|
2014-11-27 12:12:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting SSL Cert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:00:03 +01:00
|
|
|
func resourceHerokuSSLCertRetrieve(app string, id string, client *heroku.Service) (*heroku.SSLEndpointInfoResult, error) {
|
|
|
|
addon, err := client.SSLEndpointInfo(context.TODO(), app, id)
|
2014-11-27 12:12:34 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Error retrieving SSL Cert: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addon, nil
|
|
|
|
}
|