github.com/adamar/terraform@v0.2.2-0.20141016210445-2e703afdad0e/builtin/providers/aws/resource_aws_autoscaling_group.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/hashcode" 8 "github.com/hashicorp/terraform/helper/schema" 9 "github.com/mitchellh/goamz/autoscaling" 10 ) 11 12 func resourceAwsAutoscalingGroup() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAwsAutoscalingGroupCreate, 15 Read: resourceAwsAutoscalingGroupRead, 16 Update: resourceAwsAutoscalingGroupUpdate, 17 Delete: resourceAwsAutoscalingGroupDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "launch_configuration": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "desired_capacity": &schema.Schema{ 33 Type: schema.TypeInt, 34 Optional: true, 35 Computed: true, 36 }, 37 38 "min_size": &schema.Schema{ 39 Type: schema.TypeInt, 40 Required: true, 41 }, 42 43 "max_size": &schema.Schema{ 44 Type: schema.TypeInt, 45 Required: true, 46 }, 47 48 "default_cooldown": &schema.Schema{ 49 Type: schema.TypeInt, 50 Optional: true, 51 Computed: true, 52 ForceNew: true, 53 }, 54 55 "force_delete": &schema.Schema{ 56 Type: schema.TypeBool, 57 Optional: true, 58 Computed: true, 59 ForceNew: true, 60 }, 61 62 "health_check_grace_period": &schema.Schema{ 63 Type: schema.TypeInt, 64 Optional: true, 65 Computed: true, 66 ForceNew: true, 67 }, 68 69 "health_check_type": &schema.Schema{ 70 Type: schema.TypeString, 71 Optional: true, 72 Computed: true, 73 ForceNew: true, 74 }, 75 76 "availability_zones": &schema.Schema{ 77 Type: schema.TypeSet, 78 Required: true, 79 ForceNew: true, 80 Elem: &schema.Schema{Type: schema.TypeString}, 81 Set: func(v interface{}) int { 82 return hashcode.String(v.(string)) 83 }, 84 }, 85 86 "load_balancers": &schema.Schema{ 87 Type: schema.TypeSet, 88 Optional: true, 89 ForceNew: true, 90 Elem: &schema.Schema{Type: schema.TypeString}, 91 Set: func(v interface{}) int { 92 return hashcode.String(v.(string)) 93 }, 94 }, 95 96 "vpc_zone_identifier": &schema.Schema{ 97 Type: schema.TypeSet, 98 Optional: true, 99 Computed: true, 100 ForceNew: true, 101 Elem: &schema.Schema{Type: schema.TypeString}, 102 Set: func(v interface{}) int { 103 return hashcode.String(v.(string)) 104 }, 105 }, 106 }, 107 } 108 } 109 110 func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) error { 111 p := meta.(*ResourceProvider) 112 autoscalingconn := p.autoscalingconn 113 114 var autoScalingGroupOpts autoscaling.CreateAutoScalingGroup 115 autoScalingGroupOpts.Name = d.Get("name").(string) 116 autoScalingGroupOpts.HealthCheckType = d.Get("health_check_type").(string) 117 autoScalingGroupOpts.LaunchConfigurationName = d.Get("launch_configuration").(string) 118 autoScalingGroupOpts.MinSize = d.Get("min_size").(int) 119 autoScalingGroupOpts.MaxSize = d.Get("max_size").(int) 120 autoScalingGroupOpts.SetMinSize = true 121 autoScalingGroupOpts.SetMaxSize = true 122 autoScalingGroupOpts.AvailZone = expandStringList( 123 d.Get("availability_zones").(*schema.Set).List()) 124 125 if v, ok := d.GetOk("default_cooldown"); ok { 126 autoScalingGroupOpts.DefaultCooldown = v.(int) 127 autoScalingGroupOpts.SetDefaultCooldown = true 128 } 129 130 if v, ok := d.GetOk("desired_capacity"); ok { 131 autoScalingGroupOpts.DesiredCapacity = v.(int) 132 autoScalingGroupOpts.SetDesiredCapacity = true 133 } 134 135 if v, ok := d.GetOk("health_check_grace_period"); ok { 136 autoScalingGroupOpts.HealthCheckGracePeriod = v.(int) 137 autoScalingGroupOpts.SetHealthCheckGracePeriod = true 138 } 139 140 if v, ok := d.GetOk("load_balancers"); ok { 141 autoScalingGroupOpts.LoadBalancerNames = expandStringList( 142 v.(*schema.Set).List()) 143 } 144 145 if v, ok := d.GetOk("vpc_zone_identifier"); ok { 146 autoScalingGroupOpts.VPCZoneIdentifier = expandStringList( 147 v.(*schema.Set).List()) 148 } 149 150 log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", autoScalingGroupOpts) 151 _, err := autoscalingconn.CreateAutoScalingGroup(&autoScalingGroupOpts) 152 if err != nil { 153 return fmt.Errorf("Error creating Autoscaling Group: %s", err) 154 } 155 156 d.SetId(d.Get("name").(string)) 157 log.Printf("[INFO] AutoScaling Group ID: %s", d.Id()) 158 159 return resourceAwsAutoscalingGroupRead(d, meta) 160 } 161 162 func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{}) error { 163 p := meta.(*ResourceProvider) 164 autoscalingconn := p.autoscalingconn 165 166 opts := autoscaling.UpdateAutoScalingGroup{ 167 Name: d.Id(), 168 } 169 170 if d.HasChange("desired_capacity") { 171 opts.DesiredCapacity = d.Get("desired_capacity").(int) 172 opts.SetDesiredCapacity = true 173 } 174 175 if d.HasChange("min_size") { 176 opts.MinSize = d.Get("min_size").(int) 177 opts.SetMinSize = true 178 } 179 180 if d.HasChange("max_size") { 181 opts.MaxSize = d.Get("max_size").(int) 182 opts.SetMaxSize = true 183 } 184 185 log.Printf("[DEBUG] AutoScaling Group update configuration: %#v", opts) 186 _, err := autoscalingconn.UpdateAutoScalingGroup(&opts) 187 if err != nil { 188 d.Partial(true) 189 return fmt.Errorf("Error updating Autoscaling group: %s", err) 190 } 191 192 return resourceAwsAutoscalingGroupRead(d, meta) 193 } 194 195 func resourceAwsAutoscalingGroupDelete(d *schema.ResourceData, meta interface{}) error { 196 p := meta.(*ResourceProvider) 197 autoscalingconn := p.autoscalingconn 198 199 log.Printf("[DEBUG] AutoScaling Group destroy: %v", d.Id()) 200 deleteopts := autoscaling.DeleteAutoScalingGroup{Name: d.Id()} 201 202 // You can force an autoscaling group to delete 203 // even if it's in the process of scaling a resource. 204 // Normally, you would set the min-size and max-size to 0,0 205 // and then delete the group. This bypasses that and leaves 206 // resources potentially dangling. 207 if d.Get("force_delete").(bool) { 208 deleteopts.ForceDelete = true 209 } 210 211 _, err := autoscalingconn.DeleteAutoScalingGroup(&deleteopts) 212 if err != nil { 213 autoscalingerr, ok := err.(*autoscaling.Error) 214 if ok && autoscalingerr.Code == "InvalidGroup.NotFound" { 215 return nil 216 } 217 218 return err 219 } 220 221 return nil 222 } 223 224 func resourceAwsAutoscalingGroupRead(d *schema.ResourceData, meta interface{}) error { 225 p := meta.(*ResourceProvider) 226 autoscalingconn := p.autoscalingconn 227 228 describeOpts := autoscaling.DescribeAutoScalingGroups{ 229 Names: []string{d.Id()}, 230 } 231 232 log.Printf("[DEBUG] AutoScaling Group describe configuration: %#v", describeOpts) 233 describeGroups, err := autoscalingconn.DescribeAutoScalingGroups(&describeOpts) 234 if err != nil { 235 autoscalingerr, ok := err.(*autoscaling.Error) 236 if ok && autoscalingerr.Code == "InvalidGroup.NotFound" { 237 d.SetId("") 238 return nil 239 } 240 241 return fmt.Errorf("Error retrieving AutoScaling groups: %s", err) 242 } 243 244 // Verify AWS returned our sg 245 if len(describeGroups.AutoScalingGroups) != 1 || 246 describeGroups.AutoScalingGroups[0].Name != d.Id() { 247 if err != nil { 248 return fmt.Errorf("Unable to find AutoScaling group: %#v", describeGroups.AutoScalingGroups) 249 } 250 } 251 252 g := describeGroups.AutoScalingGroups[0] 253 254 d.Set("availability_zones", flattenAvailabilityZones(g.AvailabilityZones)) 255 d.Set("default_cooldown", g.DefaultCooldown) 256 d.Set("desired_capacity", g.DesiredCapacity) 257 d.Set("health_check_grace_period", g.HealthCheckGracePeriod) 258 d.Set("health_check_type", g.HealthCheckType) 259 d.Set("launch_configuration", g.LaunchConfigurationName) 260 d.Set("min_size", g.MinSize) 261 d.Set("max_size", g.MaxSize) 262 d.Set("name", g.Name) 263 d.Set("vpc_zone_identifier", g.VPCZoneIdentifier) 264 265 if len(g.LoadBalancerNames) > 0 && g.LoadBalancerNames[0].LoadBalancerName != "" { 266 d.Set("load_balancers", flattenLoadBalancers(g.LoadBalancerNames)) 267 } 268 269 return nil 270 }