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

     1  package alicloud
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"strings"
     7  )
     8  
     9  func resourceAliyunSlbAttachment() *schema.Resource {
    10  	return &schema.Resource{
    11  		Create: resourceAliyunSlbAttachmentCreate,
    12  		Read:   resourceAliyunSlbAttachmentRead,
    13  		Update: resourceAliyunSlbAttachmentUpdate,
    14  		Delete: resourceAliyunSlbAttachmentDelete,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  
    18  			"slb_id": &schema.Schema{
    19  				Type:     schema.TypeString,
    20  				Required: true,
    21  			},
    22  
    23  			"instances": &schema.Schema{
    24  				Type:     schema.TypeSet,
    25  				Elem:     &schema.Schema{Type: schema.TypeString},
    26  				Required: true,
    27  				Set:      schema.HashString,
    28  			},
    29  
    30  			"backend_servers": &schema.Schema{
    31  				Type:     schema.TypeString,
    32  				Optional: true,
    33  				Computed: true,
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func resourceAliyunSlbAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
    40  
    41  	slbId := d.Get("slb_id").(string)
    42  
    43  	slbconn := meta.(*AliyunClient).slbconn
    44  
    45  	loadBalancer, err := slbconn.DescribeLoadBalancerAttribute(slbId)
    46  	if err != nil {
    47  		if notFoundError(err) {
    48  			d.SetId("")
    49  			return fmt.Errorf("Special SLB Id not found: %#v", err)
    50  		}
    51  
    52  		return err
    53  	}
    54  
    55  	d.SetId(loadBalancer.LoadBalancerId)
    56  
    57  	return resourceAliyunSlbAttachmentUpdate(d, meta)
    58  }
    59  
    60  func resourceAliyunSlbAttachmentRead(d *schema.ResourceData, meta interface{}) error {
    61  
    62  	slbconn := meta.(*AliyunClient).slbconn
    63  	loadBalancer, err := slbconn.DescribeLoadBalancerAttribute(d.Id())
    64  	if err != nil {
    65  		if notFoundError(err) {
    66  			d.SetId("")
    67  			return nil
    68  		}
    69  		return fmt.Errorf("Read special SLB Id not found: %#v", err)
    70  	}
    71  
    72  	if loadBalancer == nil {
    73  		d.SetId("")
    74  		return nil
    75  	}
    76  
    77  	backendServerType := loadBalancer.BackendServers
    78  	servers := backendServerType.BackendServer
    79  	instanceIds := make([]string, 0, len(servers))
    80  	if len(servers) > 0 {
    81  		for _, e := range servers {
    82  			instanceIds = append(instanceIds, e.ServerId)
    83  		}
    84  		if err != nil {
    85  			return err
    86  		}
    87  	}
    88  
    89  	d.Set("slb_id", d.Id())
    90  	d.Set("instances", instanceIds)
    91  	d.Set("backend_servers", strings.Join(instanceIds, ","))
    92  
    93  	return nil
    94  }
    95  
    96  func resourceAliyunSlbAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
    97  
    98  	slbconn := meta.(*AliyunClient).slbconn
    99  	if d.HasChange("instances") {
   100  		o, n := d.GetChange("instances")
   101  		os := o.(*schema.Set)
   102  		ns := n.(*schema.Set)
   103  		remove := expandBackendServers(os.Difference(ns).List())
   104  		add := expandBackendServers(ns.Difference(os).List())
   105  
   106  		if len(add) > 0 {
   107  			_, err := slbconn.AddBackendServers(d.Id(), add)
   108  			if err != nil {
   109  				return err
   110  			}
   111  		}
   112  		if len(remove) > 0 {
   113  			removeBackendServers := make([]string, 0, len(remove))
   114  			for _, e := range remove {
   115  				removeBackendServers = append(removeBackendServers, e.ServerId)
   116  			}
   117  			_, err := slbconn.RemoveBackendServers(d.Id(), removeBackendServers)
   118  			if err != nil {
   119  				return err
   120  			}
   121  		}
   122  
   123  	}
   124  
   125  	return resourceAliyunSlbAttachmentRead(d, meta)
   126  
   127  }
   128  
   129  func resourceAliyunSlbAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
   130  
   131  	slbconn := meta.(*AliyunClient).slbconn
   132  	o := d.Get("instances")
   133  	os := o.(*schema.Set)
   134  	remove := expandBackendServers(os.List())
   135  
   136  	if len(remove) > 0 {
   137  		removeBackendServers := make([]string, 0, len(remove))
   138  		for _, e := range remove {
   139  			removeBackendServers = append(removeBackendServers, e.ServerId)
   140  		}
   141  		_, err := slbconn.RemoveBackendServers(d.Id(), removeBackendServers)
   142  		if err != nil {
   143  			return err
   144  		}
   145  	}
   146  
   147  	return nil
   148  }