github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/clc/resource_clc_load_balancer_pool.go (about)

     1  package clc
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strconv"
     7  
     8  	clc "github.com/CenturyLinkCloud/clc-sdk"
     9  	"github.com/CenturyLinkCloud/clc-sdk/lb"
    10  
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceCLCLoadBalancerPool() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceCLCLoadBalancerPoolCreate,
    17  		Read:   resourceCLCLoadBalancerPoolRead,
    18  		Update: resourceCLCLoadBalancerPoolUpdate,
    19  		Delete: resourceCLCLoadBalancerPoolDelete,
    20  		Schema: map[string]*schema.Schema{
    21  			// pool args
    22  			"port": &schema.Schema{
    23  				Type:     schema.TypeInt,
    24  				Required: true,
    25  			},
    26  			"data_center": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  			"load_balancer": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  			},
    34  			"method": &schema.Schema{
    35  				Type:     schema.TypeString,
    36  				Optional: true,
    37  				Default:  "roundRobin",
    38  			},
    39  			"persistence": &schema.Schema{
    40  				Type:     schema.TypeString,
    41  				Optional: true,
    42  				Default:  "standard",
    43  			},
    44  			"nodes": &schema.Schema{
    45  				Type:     schema.TypeList,
    46  				Required: true,
    47  				Elem:     &schema.Schema{Type: schema.TypeMap},
    48  			},
    49  		},
    50  	}
    51  }
    52  
    53  func resourceCLCLoadBalancerPoolCreate(d *schema.ResourceData, meta interface{}) error {
    54  	client := meta.(*clc.Client)
    55  	dc := d.Get("data_center").(string)
    56  	lbid := d.Get("load_balancer").(string)
    57  
    58  	s1 := d.Get("method").(string)
    59  	m := lb.LeastConn
    60  	if s1 == string(lb.RoundRobin) {
    61  		m = lb.RoundRobin
    62  	}
    63  	s2 := d.Get("persistence").(string)
    64  	p := lb.Standard
    65  	if s2 == string(lb.Sticky) {
    66  		p = lb.Sticky
    67  	}
    68  	r2 := lb.Pool{
    69  		Port:        d.Get("port").(int),
    70  		Method:      m,
    71  		Persistence: p,
    72  	}
    73  	lbp, err := client.LB.CreatePool(dc, lbid, r2)
    74  	if err != nil {
    75  		return fmt.Errorf("Failed creating pool under %v/%v: %v", dc, lbid, err)
    76  	}
    77  	d.SetId(lbp.ID)
    78  	return resourceCLCLoadBalancerPoolUpdate(d, meta)
    79  }
    80  
    81  func resourceCLCLoadBalancerPoolRead(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*clc.Client)
    83  	dc := d.Get("data_center").(string)
    84  	lbid := d.Get("load_balancer").(string)
    85  	id := d.Id()
    86  	pool, err := client.LB.GetPool(dc, lbid, id)
    87  	if err != nil {
    88  		log.Printf("[INFO] Failed fetching pool %v/%v. Marking destroyed", lbid, d.Id())
    89  		d.SetId("")
    90  		return nil
    91  	}
    92  	nodes, err := client.LB.GetAllNodes(dc, lbid, id)
    93  	nodes2 := make([]lb.Node, len(nodes))
    94  	for i, n := range nodes {
    95  		nodes2[i] = *n
    96  	}
    97  	pool.Nodes = nodes2
    98  	d.Set("port", pool.Port)
    99  	d.Set("method", pool.Method)
   100  	d.Set("persistence", pool.Persistence)
   101  	d.Set("nodes", pool.Nodes)
   102  	d.Set("links", pool.Links)
   103  	return nil
   104  }
   105  
   106  func resourceCLCLoadBalancerPoolUpdate(d *schema.ResourceData, meta interface{}) error {
   107  	client := meta.(*clc.Client)
   108  	dc := d.Get("data_center").(string)
   109  	lbid := d.Get("load_balancer").(string)
   110  	id := d.Id()
   111  	pool, err := client.LB.GetPool(dc, lbid, d.Id())
   112  	pool.Port = 0 // triggers empty value => omission from POST
   113  
   114  	if d.HasChange("method") {
   115  		d.SetPartial("method")
   116  		pool.Method = lb.Method(d.Get("method").(string))
   117  	}
   118  	if d.HasChange("persistence") {
   119  		d.SetPartial("persistence")
   120  		pool.Persistence = lb.Persistence(d.Get("persistence").(string))
   121  	}
   122  	err = client.LB.UpdatePool(dc, lbid, id, *pool)
   123  	if err != nil {
   124  		return fmt.Errorf("Failed updating pool %v: %v", id, err)
   125  	}
   126  
   127  	if d.HasChange("nodes") {
   128  		d.SetPartial("nodes")
   129  		nodes, err := parseNodes(d)
   130  		if err != nil {
   131  			return err
   132  		}
   133  		err = client.LB.UpdateNodes(dc, lbid, id, nodes...)
   134  		if err != nil {
   135  			return fmt.Errorf("Failed updating pool nodes %v: %v", id, err)
   136  		}
   137  	}
   138  	return resourceCLCLoadBalancerPoolRead(d, meta)
   139  }
   140  
   141  func resourceCLCLoadBalancerPoolDelete(d *schema.ResourceData, meta interface{}) error {
   142  	client := meta.(*clc.Client)
   143  	dc := d.Get("data_center").(string)
   144  	lbid := d.Get("load_balancer").(string)
   145  	id := d.Id()
   146  	err := client.LB.DeletePool(dc, lbid, id)
   147  	if err != nil {
   148  		return fmt.Errorf("Failed deleting pool %v: %v", id, err)
   149  	}
   150  	return nil
   151  }
   152  
   153  func parseNodes(d *schema.ResourceData) ([]lb.Node, error) {
   154  	var nodes []lb.Node
   155  	raw := d.Get("nodes")
   156  	if raw == nil {
   157  		log.Println("WARNING: pool missing nodes")
   158  		return nil, nil
   159  	}
   160  	if arr, ok := raw.([]interface{}); ok {
   161  		for _, v := range arr {
   162  			m := v.(map[string]interface{})
   163  			p, err := strconv.Atoi(m["privatePort"].(string))
   164  			if err != nil {
   165  				log.Printf("[WARN] Failed parsing port '%v'. skipping", m["privatePort"])
   166  				continue
   167  			}
   168  			n := lb.Node{
   169  				Status:      m["status"].(string),
   170  				IPaddress:   m["ipAddress"].(string),
   171  				PrivatePort: p,
   172  			}
   173  			nodes = append(nodes, n)
   174  		}
   175  	} else {
   176  		return nil, fmt.Errorf("Failed parsing nodes from pool spec: %v", raw)
   177  	}
   178  	return nodes, nil
   179  }