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)
|
|
|
|
|
|
|
|
if d.HasChange("certificate_chain") {
|
|
|
|
preprocess := true
|
|
|
|
rollback := false
|
|
|
|
ad, err := client.SSLEndpointUpdate(
|
2017-03-10 13:00:03 +01:00
|
|
|
context.TODO(), app, d.Id(), heroku.SSLEndpointUpdateOpts{
|
2014-11-27 12:12:34 +01:00
|
|
|
CertificateChain: d.Get("certificate_chain").(*string),
|
|
|
|
Preprocess: &preprocess,
|
|
|
|
PrivateKey: d.Get("private_key").(*string),
|
|
|
|
Rollback: &rollback})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the new ID
|
|
|
|
d.SetId(ad.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|