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:
Sander van Harmelen 2014-12-16 13:13:59 +01:00
parent 7e091dd7bd
commit 3aeba87e38
2 changed files with 30 additions and 18 deletions

View File

@ -37,10 +37,13 @@ func resourceAwsElb() *schema.Resource {
},
"availability_zones": &schema.Schema{
Type: schema.TypeList,
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
ForceNew: true,
Set: func(v interface{}) int {
return hashcode.String(v.(string))
},
},
"instances": &schema.Schema{
@ -172,7 +175,7 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
}
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 {
@ -223,8 +226,7 @@ func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
}
}
}
return resourceAwsElbUpdate(d, meta)
}
@ -255,6 +257,7 @@ func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
d.Set("name", lb.LoadBalancerName)
d.Set("dns_name", lb.DNSName)
d.Set("internal", lb.Scheme == "internal")
d.Set("availability_zones", flattenAvailabilityZones(lb.AvailabilityZones))
d.Set("instances", flattenInstances(lb.Instances))
d.Set("listener", flattenListeners(lb.Listeners))
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)
}
}
d.SetPartial("instances")
}
log.Println("[INFO] outside modify attributes")
if d.HasChange("cross_zone_load_balancing") {
log.Println("[INFO] inside modify attributes")
attrs := elb.ModifyLoadBalancerAttributes{
LoadBalancerName: d.Get("name").(string),
LoadBalancerAttributes: elb.LoadBalancerAttributes{
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")
if d.HasChange("cross_zone_load_balancing") {
log.Println("[INFO] inside modify attributes")
attrs := elb.ModifyLoadBalancerAttributes{
LoadBalancerName: d.Get("name").(string),
LoadBalancerAttributes: elb.LoadBalancerAttributes{
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")
}
d.Partial(false)
return resourceAwsElbRead(d, meta)

View File

@ -172,6 +172,15 @@ func flattenInstances(list []elb.Instance) []string {
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{}
func flattenListeners(list []elb.Listener) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))