github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_cert.go (about)

     1  package heroku
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"log"
     7  
     8  	"github.com/cyberdelia/heroku-go/v3"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceHerokuCert() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceHerokuCertCreate,
    15  		Read:   resourceHerokuCertRead,
    16  		Update: resourceHerokuCertUpdate,
    17  		Delete: resourceHerokuCertDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"app": {
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"certificate_chain": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  
    31  			"private_key": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  			},
    35  
    36  			"cname": {
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  
    41  			"name": {
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func resourceHerokuCertCreate(d *schema.ResourceData, meta interface{}) error {
    50  	client := meta.(*heroku.Service)
    51  
    52  	app := d.Get("app").(string)
    53  	preprocess := true
    54  	opts := heroku.SSLEndpointCreateOpts{
    55  		CertificateChain: d.Get("certificate_chain").(string),
    56  		Preprocess:       &preprocess,
    57  		PrivateKey:       d.Get("private_key").(string)}
    58  
    59  	log.Printf("[DEBUG] SSL Certificate create configuration: %#v, %#v", app, opts)
    60  	a, err := client.SSLEndpointCreate(context.TODO(), app, opts)
    61  	if err != nil {
    62  		return fmt.Errorf("Error creating SSL endpoint: %s", err)
    63  	}
    64  
    65  	d.SetId(a.ID)
    66  	log.Printf("[INFO] SSL Certificate ID: %s", d.Id())
    67  
    68  	return resourceHerokuCertRead(d, meta)
    69  }
    70  
    71  func resourceHerokuCertRead(d *schema.ResourceData, meta interface{}) error {
    72  	client := meta.(*heroku.Service)
    73  
    74  	cert, err := resourceHerokuSSLCertRetrieve(
    75  		d.Get("app").(string), d.Id(), client)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	d.Set("certificate_chain", cert.CertificateChain)
    81  	d.Set("name", cert.Name)
    82  	d.Set("cname", cert.CName)
    83  
    84  	return nil
    85  }
    86  
    87  func resourceHerokuCertUpdate(d *schema.ResourceData, meta interface{}) error {
    88  	client := meta.(*heroku.Service)
    89  
    90  	app := d.Get("app").(string)
    91  
    92  	if d.HasChange("certificate_chain") {
    93  		preprocess := true
    94  		rollback := false
    95  		ad, err := client.SSLEndpointUpdate(
    96  			context.TODO(), app, d.Id(), heroku.SSLEndpointUpdateOpts{
    97  				CertificateChain: d.Get("certificate_chain").(*string),
    98  				Preprocess:       &preprocess,
    99  				PrivateKey:       d.Get("private_key").(*string),
   100  				Rollback:         &rollback})
   101  		if err != nil {
   102  			return err
   103  		}
   104  
   105  		// Store the new ID
   106  		d.SetId(ad.ID)
   107  	}
   108  
   109  	return resourceHerokuCertRead(d, meta)
   110  }
   111  
   112  func resourceHerokuCertDelete(d *schema.ResourceData, meta interface{}) error {
   113  	client := meta.(*heroku.Service)
   114  
   115  	log.Printf("[INFO] Deleting SSL Cert: %s", d.Id())
   116  
   117  	// Destroy the app
   118  	_, err := client.SSLEndpointDelete(context.TODO(), d.Get("app").(string), d.Id())
   119  	if err != nil {
   120  		return fmt.Errorf("Error deleting SSL Cert: %s", err)
   121  	}
   122  
   123  	d.SetId("")
   124  	return nil
   125  }
   126  
   127  func resourceHerokuSSLCertRetrieve(app string, id string, client *heroku.Service) (*heroku.SSLEndpointInfoResult, error) {
   128  	addon, err := client.SSLEndpointInfo(context.TODO(), app, id)
   129  
   130  	if err != nil {
   131  		return nil, fmt.Errorf("Error retrieving SSL Cert: %s", err)
   132  	}
   133  
   134  	return addon, nil
   135  }