github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/heroku/resource_heroku_domain.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 resourceHerokuDomain() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceHerokuDomainCreate,
    15  		Read:   resourceHerokuDomainRead,
    16  		Delete: resourceHerokuDomainDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"hostname": {
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"app": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"cname": {
    32  				Type:     schema.TypeString,
    33  				Computed: true,
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func resourceHerokuDomainCreate(d *schema.ResourceData, meta interface{}) error {
    40  	client := meta.(*heroku.Service)
    41  
    42  	app := d.Get("app").(string)
    43  	hostname := d.Get("hostname").(string)
    44  
    45  	log.Printf("[DEBUG] Domain create configuration: %#v, %#v", app, hostname)
    46  
    47  	do, err := client.DomainCreate(context.TODO(), app, heroku.DomainCreateOpts{Hostname: hostname})
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	d.SetId(do.ID)
    53  	d.Set("hostname", do.Hostname)
    54  	d.Set("cname", fmt.Sprintf("%s.herokuapp.com", app))
    55  
    56  	log.Printf("[INFO] Domain ID: %s", d.Id())
    57  	return nil
    58  }
    59  
    60  func resourceHerokuDomainDelete(d *schema.ResourceData, meta interface{}) error {
    61  	client := meta.(*heroku.Service)
    62  
    63  	log.Printf("[INFO] Deleting Domain: %s", d.Id())
    64  
    65  	// Destroy the domain
    66  	_, err := client.DomainDelete(context.TODO(), d.Get("app").(string), d.Id())
    67  	if err != nil {
    68  		return fmt.Errorf("Error deleting domain: %s", err)
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func resourceHerokuDomainRead(d *schema.ResourceData, meta interface{}) error {
    75  	client := meta.(*heroku.Service)
    76  
    77  	app := d.Get("app").(string)
    78  	do, err := client.DomainInfo(context.TODO(), app, d.Id())
    79  	if err != nil {
    80  		return fmt.Errorf("Error retrieving domain: %s", err)
    81  	}
    82  
    83  	d.Set("hostname", do.Hostname)
    84  	d.Set("cname", fmt.Sprintf("%s.herokuapp.com", app))
    85  
    86  	return nil
    87  }