github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/clc/resource_clc_load_balancer.go (about)

     1  package clc
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	clc "github.com/CenturyLinkCloud/clc-sdk"
     9  	"github.com/CenturyLinkCloud/clc-sdk/lb"
    10  
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceCLCLoadBalancer() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceCLCLoadBalancerCreate,
    18  		Read:   resourceCLCLoadBalancerRead,
    19  		Update: resourceCLCLoadBalancerUpdate,
    20  		Delete: resourceCLCLoadBalancerDelete,
    21  		Schema: map[string]*schema.Schema{
    22  			"name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  			"data_center": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  			},
    30  			"description": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  			},
    34  			// optional
    35  			"status": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  				Default:  "enabled",
    39  			},
    40  			// computed
    41  			"ip_address": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Computed: true,
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func resourceCLCLoadBalancerCreate(d *schema.ResourceData, meta interface{}) error {
    50  	client := meta.(*clc.Client)
    51  	dc := d.Get("data_center").(string)
    52  	name := d.Get("name").(string)
    53  	desc := d.Get("description").(string)
    54  	status := d.Get("status").(string)
    55  	r1 := lb.LoadBalancer{
    56  		Name:        name,
    57  		Description: desc,
    58  		Status:      status,
    59  	}
    60  	l, err := client.LB.Create(dc, r1)
    61  	if err != nil {
    62  		return fmt.Errorf("Failed creating load balancer under %v/%v: %v", dc, name, err)
    63  	}
    64  	d.SetId(l.ID)
    65  	return resource.Retry(1*time.Minute, func() *resource.RetryError {
    66  		_, err := client.LB.Get(dc, l.ID)
    67  		if err != nil {
    68  			return resource.RetryableError(err)
    69  		}
    70  		err = resourceCLCLoadBalancerRead(d, meta)
    71  		if err != nil {
    72  			return resource.NonRetryableError(err)
    73  		}
    74  		return nil
    75  	})
    76  }
    77  
    78  func resourceCLCLoadBalancerRead(d *schema.ResourceData, meta interface{}) error {
    79  	client := meta.(*clc.Client)
    80  	dc := d.Get("data_center").(string)
    81  	id := d.Id()
    82  	resp, err := client.LB.Get(dc, id)
    83  	if err != nil {
    84  		log.Printf("[INFO] Failed finding load balancer %v/%v. Marking destroyed", dc, id)
    85  		d.SetId("")
    86  		return nil
    87  	}
    88  	d.Set("description", resp.Description)
    89  	d.Set("ip_address", resp.IPaddress)
    90  	d.Set("status", resp.Status)
    91  	d.Set("pools", resp.Pools)
    92  	d.Set("links", resp.Links)
    93  	return nil
    94  }
    95  
    96  func resourceCLCLoadBalancerUpdate(d *schema.ResourceData, meta interface{}) error {
    97  	update := lb.LoadBalancer{}
    98  	client := meta.(*clc.Client)
    99  	dc := d.Get("data_center").(string)
   100  	id := d.Id()
   101  
   102  	if d.HasChange("name") {
   103  		update.Name = d.Get("name").(string)
   104  	}
   105  	if d.HasChange("description") {
   106  		update.Description = d.Get("description").(string)
   107  	}
   108  	if d.HasChange("status") {
   109  		update.Status = d.Get("status").(string)
   110  	}
   111  	if update.Name != "" || update.Description != "" || update.Status != "" {
   112  		update.Name = d.Get("name").(string) // required on every PUT
   113  		err := client.LB.Update(dc, id, update)
   114  		if err != nil {
   115  			return fmt.Errorf("Failed updating load balancer under %v/%v: %v", dc, id, err)
   116  		}
   117  	}
   118  	return resourceCLCLoadBalancerRead(d, meta)
   119  }
   120  
   121  func resourceCLCLoadBalancerDelete(d *schema.ResourceData, meta interface{}) error {
   122  	client := meta.(*clc.Client)
   123  	dc := d.Get("data_center").(string)
   124  	id := d.Id()
   125  	err := client.LB.Delete(dc, id)
   126  	if err != nil {
   127  		return fmt.Errorf("Failed deleting loadbalancer %v: %v", id, err)
   128  	}
   129  	return nil
   130  }