github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_ess_scalinggroup.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "github.com/denverdino/aliyungo/common" 6 "github.com/denverdino/aliyungo/ess" 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/helper/schema" 9 "strings" 10 "time" 11 ) 12 13 func resourceAlicloudEssScalingGroup() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAliyunEssScalingGroupCreate, 16 Read: resourceAliyunEssScalingGroupRead, 17 Update: resourceAliyunEssScalingGroupUpdate, 18 Delete: resourceAliyunEssScalingGroupDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "min_size": &schema.Schema{ 22 Type: schema.TypeInt, 23 Required: true, 24 ValidateFunc: validateIntegerInRange(0, 100), 25 }, 26 "max_size": &schema.Schema{ 27 Type: schema.TypeInt, 28 Required: true, 29 ValidateFunc: validateIntegerInRange(0, 100), 30 }, 31 "scaling_group_name": &schema.Schema{ 32 Type: schema.TypeString, 33 Optional: true, 34 }, 35 "default_cooldown": &schema.Schema{ 36 Type: schema.TypeInt, 37 Default: 300, 38 Optional: true, 39 ValidateFunc: validateIntegerInRange(0, 86400), 40 }, 41 "vswitch_id": &schema.Schema{ 42 Type: schema.TypeString, 43 Optional: true, 44 }, 45 "removal_policies": &schema.Schema{ 46 Type: schema.TypeList, 47 Elem: &schema.Schema{Type: schema.TypeString}, 48 Optional: true, 49 MaxItems: 2, 50 }, 51 "db_instance_ids": &schema.Schema{ 52 Type: schema.TypeList, 53 Elem: &schema.Schema{Type: schema.TypeString}, 54 Optional: true, 55 MaxItems: 3, 56 }, 57 "loadbalancer_ids": &schema.Schema{ 58 Type: schema.TypeList, 59 Elem: &schema.Schema{Type: schema.TypeString}, 60 Optional: true, 61 }, 62 }, 63 } 64 } 65 66 func resourceAliyunEssScalingGroupCreate(d *schema.ResourceData, meta interface{}) error { 67 68 args, err := buildAlicloudEssScalingGroupArgs(d, meta) 69 if err != nil { 70 return err 71 } 72 73 essconn := meta.(*AliyunClient).essconn 74 75 scaling, err := essconn.CreateScalingGroup(args) 76 if err != nil { 77 return err 78 } 79 80 d.SetId(scaling.ScalingGroupId) 81 82 return resourceAliyunEssScalingGroupUpdate(d, meta) 83 } 84 85 func resourceAliyunEssScalingGroupRead(d *schema.ResourceData, meta interface{}) error { 86 87 client := meta.(*AliyunClient) 88 89 scaling, err := client.DescribeScalingGroupById(d.Id()) 90 if err != nil { 91 if e, ok := err.(*common.Error); ok && e.Code == InstanceNotfound { 92 d.SetId("") 93 return nil 94 } 95 return fmt.Errorf("Error Describe ESS scaling group Attribute: %#v", err) 96 } 97 98 d.Set("min_size", scaling.MinSize) 99 d.Set("max_size", scaling.MaxSize) 100 d.Set("scaling_group_name", scaling.ScalingGroupName) 101 d.Set("default_cooldown", scaling.DefaultCooldown) 102 d.Set("removal_policies", scaling.RemovalPolicies) 103 d.Set("db_instance_ids", scaling.DBInstanceIds) 104 d.Set("loadbalancer_ids", scaling.LoadBalancerId) 105 106 return nil 107 } 108 109 func resourceAliyunEssScalingGroupUpdate(d *schema.ResourceData, meta interface{}) error { 110 111 conn := meta.(*AliyunClient).essconn 112 args := &ess.ModifyScalingGroupArgs{ 113 ScalingGroupId: d.Id(), 114 } 115 116 if d.HasChange("scaling_group_name") { 117 args.ScalingGroupName = d.Get("scaling_group_name").(string) 118 } 119 120 if d.HasChange("min_size") { 121 args.MinSize = d.Get("min_size").(int) 122 } 123 124 if d.HasChange("max_size") { 125 args.MaxSize = d.Get("max_size").(int) 126 } 127 128 if d.HasChange("default_cooldown") { 129 args.DefaultCooldown = d.Get("default_cooldown").(int) 130 } 131 132 if d.HasChange("removal_policies") { 133 policyStrings := d.Get("removal_policies").([]interface{}) 134 args.RemovalPolicy = expandStringList(policyStrings) 135 } 136 137 if _, err := conn.ModifyScalingGroup(args); err != nil { 138 return err 139 } 140 141 return resourceAliyunEssScalingGroupRead(d, meta) 142 } 143 144 func resourceAliyunEssScalingGroupDelete(d *schema.ResourceData, meta interface{}) error { 145 client := meta.(*AliyunClient) 146 147 return resource.Retry(2*time.Minute, func() *resource.RetryError { 148 err := client.DeleteScalingGroupById(d.Id()) 149 150 if err != nil { 151 e, _ := err.(*common.Error) 152 if e.ErrorResponse.Code != InvalidScalingGroupIdNotFound { 153 return resource.RetryableError(fmt.Errorf("Scaling group in use - trying again while it is deleted.")) 154 } 155 } 156 157 _, err = client.DescribeScalingGroupById(d.Id()) 158 if err != nil { 159 if notFoundError(err) { 160 return nil 161 } 162 return resource.NonRetryableError(err) 163 } 164 165 return resource.RetryableError(fmt.Errorf("Scaling group in use - trying again while it is deleted.")) 166 }) 167 } 168 169 func buildAlicloudEssScalingGroupArgs(d *schema.ResourceData, meta interface{}) (*ess.CreateScalingGroupArgs, error) { 170 client := meta.(*AliyunClient) 171 args := &ess.CreateScalingGroupArgs{ 172 RegionId: getRegion(d, meta), 173 MinSize: d.Get("min_size").(int), 174 MaxSize: d.Get("max_size").(int), 175 DefaultCooldown: d.Get("default_cooldown").(int), 176 } 177 178 if v := d.Get("scaling_group_name").(string); v != "" { 179 args.ScalingGroupName = v 180 } 181 182 if v := d.Get("vswitch_id").(string); v != "" { 183 args.VSwitchId = v 184 185 // get vpcId 186 vpcId, err := client.GetVpcIdByVSwitchId(v) 187 188 if err != nil { 189 return nil, fmt.Errorf("VswitchId %s is not valid of current region", v) 190 } 191 // fill vpcId by vswitchId 192 args.VpcId = vpcId 193 194 } 195 196 dbs, ok := d.GetOk("db_instance_ids") 197 if ok { 198 dbsStrings := dbs.([]interface{}) 199 args.DBInstanceId = expandStringList(dbsStrings) 200 } 201 202 lbs, ok := d.GetOk("loadbalancer_ids") 203 if ok { 204 lbsStrings := lbs.([]interface{}) 205 args.LoadBalancerId = strings.Join(expandStringList(lbsStrings), COMMA_SEPARATED) 206 } 207 208 return args, nil 209 }