github.com/ezbercih/terraform@v0.1.1-0.20140729011846-3c33865e0839/builtin/providers/digitalocean/resource_digitalocean_domain.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/config"
     8  	"github.com/hashicorp/terraform/helper/diff"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/pearkes/digitalocean"
    11  )
    12  
    13  func resource_digitalocean_domain_create(
    14  	s *terraform.ResourceState,
    15  	d *terraform.ResourceDiff,
    16  	meta interface{}) (*terraform.ResourceState, error) {
    17  	p := meta.(*ResourceProvider)
    18  	client := p.client
    19  	// Merge the diff into the state so that we have all the attributes
    20  	// properly.
    21  	rs := s.MergeDiff(d)
    22  
    23  	// Build up our creation options
    24  	opts := digitalocean.CreateDomain{
    25  		Name:      rs.Attributes["name"],
    26  		IPAddress: rs.Attributes["ip_address"],
    27  	}
    28  
    29  	log.Printf("[DEBUG] Domain create configuration: %#v", opts)
    30  
    31  	name, err := client.CreateDomain(&opts)
    32  	if err != nil {
    33  		return nil, fmt.Errorf("Error creating Domain: %s", err)
    34  	}
    35  
    36  	rs.ID = name
    37  	log.Printf("[INFO] Domain Name: %s", name)
    38  
    39  	return rs, nil
    40  }
    41  
    42  func resource_digitalocean_domain_destroy(
    43  	s *terraform.ResourceState,
    44  	meta interface{}) error {
    45  	p := meta.(*ResourceProvider)
    46  	client := p.client
    47  
    48  	log.Printf("[INFO] Deleting Domain: %s", s.ID)
    49  
    50  	err := client.DestroyDomain(s.ID)
    51  
    52  	if err != nil {
    53  		return fmt.Errorf("Error deleting Domain: %s", err)
    54  	}
    55  
    56  	return nil
    57  }
    58  
    59  func resource_digitalocean_domain_refresh(
    60  	s *terraform.ResourceState,
    61  	meta interface{}) (*terraform.ResourceState, error) {
    62  	p := meta.(*ResourceProvider)
    63  	client := p.client
    64  
    65  	domain, err := client.RetrieveDomain(s.ID)
    66  
    67  	if err != nil {
    68  		return s, fmt.Errorf("Error retrieving domain: %s", err)
    69  	}
    70  
    71  	s.Attributes["name"] = domain.Name
    72  
    73  	return s, nil
    74  }
    75  
    76  func resource_digitalocean_domain_diff(
    77  	s *terraform.ResourceState,
    78  	c *terraform.ResourceConfig,
    79  	meta interface{}) (*terraform.ResourceDiff, error) {
    80  
    81  	b := &diff.ResourceBuilder{
    82  		Attrs: map[string]diff.AttrType{
    83  			"name":       diff.AttrTypeCreate,
    84  			"ip_address": diff.AttrTypeCreate,
    85  		},
    86  
    87  		ComputedAttrs: []string{},
    88  	}
    89  
    90  	return b.Diff(s, c)
    91  }
    92  
    93  func resource_digitalocean_domain_validation() *config.Validator {
    94  	return &config.Validator{
    95  		Required: []string{
    96  			"name",
    97  			"ip_address",
    98  		},
    99  		Optional: []string{},
   100  	}
   101  }