github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/heroku/resource_heroku_cert.go (about)

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