github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/consul/resource_consul_node.go (about)

     1  package consul
     2  
     3  import (
     4  	"fmt"
     5  
     6  	consulapi "github.com/hashicorp/consul/api"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceConsulNode() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceConsulNodeCreate,
    13  		Update: resourceConsulNodeCreate,
    14  		Read:   resourceConsulNodeRead,
    15  		Delete: resourceConsulNodeDelete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"address": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  				ForceNew: true,
    22  			},
    23  
    24  			"datacenter": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Optional: true,
    27  				Computed: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"name": &schema.Schema{
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"token": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  func resourceConsulNodeCreate(d *schema.ResourceData, meta interface{}) error {
    46  	client := meta.(*consulapi.Client)
    47  	catalog := client.Catalog()
    48  
    49  	var dc string
    50  	if v, ok := d.GetOk("datacenter"); ok {
    51  		dc = v.(string)
    52  	} else {
    53  		var err error
    54  		if dc, err = getDC(d, client); err != nil {
    55  			return err
    56  		}
    57  	}
    58  
    59  	var token string
    60  	if v, ok := d.GetOk("token"); ok {
    61  		token = v.(string)
    62  	}
    63  
    64  	// Setup the operations using the datacenter
    65  	wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
    66  
    67  	address := d.Get("address").(string)
    68  	name := d.Get("name").(string)
    69  
    70  	registration := &consulapi.CatalogRegistration{
    71  		Address:    address,
    72  		Datacenter: dc,
    73  		Node:       name,
    74  	}
    75  
    76  	if _, err := catalog.Register(registration, &wOpts); err != nil {
    77  		return fmt.Errorf("Failed to register Consul catalog node with name '%s' at address '%s' in %s: %v",
    78  			name, address, dc, err)
    79  	}
    80  
    81  	// Update the resource
    82  	qOpts := consulapi.QueryOptions{Datacenter: dc}
    83  	if _, _, err := catalog.Node(name, &qOpts); err != nil {
    84  		return fmt.Errorf("Failed to read Consul catalog node with name '%s' at address '%s' in %s: %v",
    85  			name, address, dc, err)
    86  	} else {
    87  		d.Set("datacenter", dc)
    88  	}
    89  
    90  	d.SetId(fmt.Sprintf("%s-%s", name, address))
    91  
    92  	return nil
    93  }
    94  
    95  func resourceConsulNodeRead(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*consulapi.Client)
    97  	catalog := client.Catalog()
    98  
    99  	// Get the DC, error if not available.
   100  	var dc string
   101  	if v, ok := d.GetOk("datacenter"); ok {
   102  		dc = v.(string)
   103  	}
   104  
   105  	name := d.Get("name").(string)
   106  
   107  	// Setup the operations using the datacenter
   108  	qOpts := consulapi.QueryOptions{Datacenter: dc}
   109  
   110  	if _, _, err := catalog.Node(name, &qOpts); err != nil {
   111  		return fmt.Errorf("Failed to get name '%s' from Consul catalog: %v", name, err)
   112  	}
   113  
   114  	return nil
   115  }
   116  
   117  func resourceConsulNodeDelete(d *schema.ResourceData, meta interface{}) error {
   118  	client := meta.(*consulapi.Client)
   119  	catalog := client.Catalog()
   120  
   121  	var dc string
   122  	if v, ok := d.GetOk("datacenter"); ok {
   123  		dc = v.(string)
   124  	} else {
   125  		var err error
   126  		if dc, err = getDC(d, client); err != nil {
   127  			return err
   128  		}
   129  	}
   130  
   131  	var token string
   132  	if v, ok := d.GetOk("token"); ok {
   133  		token = v.(string)
   134  	}
   135  
   136  	// Setup the operations using the datacenter
   137  	wOpts := consulapi.WriteOptions{Datacenter: dc, Token: token}
   138  
   139  	address := d.Get("address").(string)
   140  	name := d.Get("name").(string)
   141  
   142  	deregistration := consulapi.CatalogDeregistration{
   143  		Address:    address,
   144  		Datacenter: dc,
   145  		Node:       name,
   146  	}
   147  
   148  	if _, err := catalog.Deregister(&deregistration, &wOpts); err != nil {
   149  		return fmt.Errorf("Failed to deregister Consul catalog node with name '%s' at address '%s' in %s: %v",
   150  			name, address, dc, err)
   151  	}
   152  
   153  	// Clear the ID
   154  	d.SetId("")
   155  	return nil
   156  }