github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/digitalocean/resource_digitalocean_domain.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/pearkes/digitalocean"
    10  )
    11  
    12  func resourceDigitalOceanDomain() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceDigitalOceanDomainCreate,
    15  		Read:   resourceDigitalOceanDomainRead,
    16  		Delete: resourceDigitalOceanDomainDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"ip_address": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func resourceDigitalOceanDomainCreate(d *schema.ResourceData, meta interface{}) error {
    35  	client := meta.(*digitalocean.Client)
    36  
    37  	// Build up our creation options
    38  	opts := &digitalocean.CreateDomain{
    39  		Name:      d.Get("name").(string),
    40  		IPAddress: d.Get("ip_address").(string),
    41  	}
    42  
    43  	log.Printf("[DEBUG] Domain create configuration: %#v", opts)
    44  	name, err := client.CreateDomain(opts)
    45  	if err != nil {
    46  		return fmt.Errorf("Error creating Domain: %s", err)
    47  	}
    48  
    49  	d.SetId(name)
    50  	log.Printf("[INFO] Domain Name: %s", name)
    51  
    52  	return resourceDigitalOceanDomainRead(d, meta)
    53  }
    54  
    55  func resourceDigitalOceanDomainRead(d *schema.ResourceData, meta interface{}) error {
    56  	client := meta.(*digitalocean.Client)
    57  
    58  	domain, err := client.RetrieveDomain(d.Id())
    59  	if err != nil {
    60  		// If the domain is somehow already destroyed, mark as
    61  		// succesfully gone
    62  		if strings.Contains(err.Error(), "404 Not Found") {
    63  			d.SetId("")
    64  			return nil
    65  		}
    66  
    67  		return fmt.Errorf("Error retrieving domain: %s", err)
    68  	}
    69  
    70  	d.Set("name", domain.Name)
    71  
    72  	return nil
    73  }
    74  
    75  func resourceDigitalOceanDomainDelete(d *schema.ResourceData, meta interface{}) error {
    76  	client := meta.(*digitalocean.Client)
    77  
    78  	log.Printf("[INFO] Deleting Domain: %s", d.Id())
    79  	err := client.DestroyDomain(d.Id())
    80  	if err != nil {
    81  		return fmt.Errorf("Error deleting Domain: %s", err)
    82  	}
    83  
    84  	d.SetId("")
    85  	return nil
    86  }