Updating the resource to use a set instead of a list
By using a set for the availability zones, you can use things like `availability_zones = ["${aws_instance.web.*.availability_zone}"]` where is very likely multiple of the same zones will be added to the set. If you use a list here, the list will say it’s changed (even if you add the same zone) which will force a new resource.
This commit is contained in:
parent
7e091dd7bd
commit
3aeba87e38
|
@ -37,10 +37,13 @@ func resourceAwsElb() *schema.Resource {
|
||||||
},
|
},
|
||||||
|
|
||||||
"availability_zones": &schema.Schema{
|
"availability_zones": &schema.Schema{
|
||||||
Type: schema.TypeList,
|
Type: schema.TypeSet,
|
||||||
Elem: &schema.Schema{Type: schema.TypeString},
|
Elem: &schema.Schema{Type: schema.TypeString},
|
||||||
Optional: true,
|
Optional: true,
|
||||||
ForceNew: true,
|
ForceNew: true,
|
||||||
|
Set: func(v interface{}) int {
|
||||||
|
return hashcode.String(v.(string))
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
"instances": &schema.Schema{
|
"instances": &schema.Schema{
|
||||||
|
@ -172,7 +175,7 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("availability_zones"); ok {
|
if v, ok := d.GetOk("availability_zones"); ok {
|
||||||
elbOpts.AvailZone = expandStringList(v.([]interface{}))
|
elbOpts.AvailZone = expandStringList(v.(*schema.Set).List())
|
||||||
}
|
}
|
||||||
|
|
||||||
if v, ok := d.GetOk("security_groups"); ok {
|
if v, ok := d.GetOk("security_groups"); ok {
|
||||||
|
@ -223,8 +226,7 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return resourceAwsElbUpdate(d, meta)
|
return resourceAwsElbUpdate(d, meta)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,6 +257,7 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
d.Set("name", lb.LoadBalancerName)
|
d.Set("name", lb.LoadBalancerName)
|
||||||
d.Set("dns_name", lb.DNSName)
|
d.Set("dns_name", lb.DNSName)
|
||||||
d.Set("internal", lb.Scheme == "internal")
|
d.Set("internal", lb.Scheme == "internal")
|
||||||
|
d.Set("availability_zones", flattenAvailabilityZones(lb.AvailabilityZones))
|
||||||
d.Set("instances", flattenInstances(lb.Instances))
|
d.Set("instances", flattenInstances(lb.Instances))
|
||||||
d.Set("listener", flattenListeners(lb.Listeners))
|
d.Set("listener", flattenListeners(lb.Listeners))
|
||||||
d.Set("security_groups", lb.SecurityGroups)
|
d.Set("security_groups", lb.SecurityGroups)
|
||||||
|
@ -306,25 +309,25 @@ func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
return fmt.Errorf("Failure deregistering instances: %s", err)
|
return fmt.Errorf("Failure deregistering instances: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
d.SetPartial("instances")
|
d.SetPartial("instances")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Println("[INFO] outside modify attributes")
|
log.Println("[INFO] outside modify attributes")
|
||||||
if d.HasChange("cross_zone_load_balancing") {
|
if d.HasChange("cross_zone_load_balancing") {
|
||||||
log.Println("[INFO] inside modify attributes")
|
log.Println("[INFO] inside modify attributes")
|
||||||
attrs := elb.ModifyLoadBalancerAttributes{
|
attrs := elb.ModifyLoadBalancerAttributes{
|
||||||
LoadBalancerName: d.Get("name").(string),
|
LoadBalancerName: d.Get("name").(string),
|
||||||
LoadBalancerAttributes: elb.LoadBalancerAttributes{
|
LoadBalancerAttributes: elb.LoadBalancerAttributes{
|
||||||
CrossZoneLoadBalancingEnabled: d.Get("cross_zone_load_balancing").(bool),
|
CrossZoneLoadBalancingEnabled: d.Get("cross_zone_load_balancing").(bool),
|
||||||
},
|
},
|
||||||
}
|
|
||||||
_, err := elbconn.ModifyLoadBalancerAttributes(&attrs)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failure configuring health check: %s", err)
|
|
||||||
}
|
|
||||||
d.SetPartial("cross_zone_load_balancing")
|
|
||||||
}
|
}
|
||||||
|
_, err := elbconn.ModifyLoadBalancerAttributes(&attrs)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("Failure configuring health check: %s", err)
|
||||||
|
}
|
||||||
|
d.SetPartial("cross_zone_load_balancing")
|
||||||
|
}
|
||||||
|
|
||||||
d.Partial(false)
|
d.Partial(false)
|
||||||
return resourceAwsElbRead(d, meta)
|
return resourceAwsElbRead(d, meta)
|
||||||
|
|
|
@ -172,6 +172,15 @@ func flattenInstances(list []elb.Instance) []string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flattens an array of AvailabilityZones into a []string
|
||||||
|
func flattenAvailabilityZones(list []elb.AvailabilityZone) []string {
|
||||||
|
result := make([]string, 0, len(list))
|
||||||
|
for _, z := range list {
|
||||||
|
result = append(result, z.AvailabilityZone)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
// Flattens an array of Listeners into a []map[string]interface{}
|
// Flattens an array of Listeners into a []map[string]interface{}
|
||||||
func flattenListeners(list []elb.Listener) []map[string]interface{} {
|
func flattenListeners(list []elb.Listener) []map[string]interface{} {
|
||||||
result := make([]map[string]interface{}, 0, len(list))
|
result := make([]map[string]interface{}, 0, len(list))
|
||||||
|
|
Loading…
Reference in New Issue