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