github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_ess_scalingrule.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 resourceAlicloudEssScalingRule() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAliyunEssScalingRuleCreate, 16 Read: resourceAliyunEssScalingRuleRead, 17 Update: resourceAliyunEssScalingRuleUpdate, 18 Delete: resourceAliyunEssScalingRuleDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "scaling_group_id": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 "adjustment_type": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ValidateFunc: validateAllowedStringValue([]string{string(ess.QuantityChangeInCapacity), 29 string(ess.PercentChangeInCapacity), string(ess.TotalCapacity)}), 30 }, 31 "adjustment_value": &schema.Schema{ 32 Type: schema.TypeInt, 33 Required: true, 34 }, 35 "scaling_rule_name": &schema.Schema{ 36 Type: schema.TypeString, 37 Computed: true, 38 Optional: true, 39 }, 40 "ari": &schema.Schema{ 41 Type: schema.TypeString, 42 Computed: true, 43 }, 44 "cooldown": &schema.Schema{ 45 Type: schema.TypeInt, 46 Optional: true, 47 ValidateFunc: validateIntegerInRange(0, 86400), 48 }, 49 }, 50 } 51 } 52 53 func resourceAliyunEssScalingRuleCreate(d *schema.ResourceData, meta interface{}) error { 54 55 args, err := buildAlicloudEssScalingRuleArgs(d, meta) 56 if err != nil { 57 return err 58 } 59 60 essconn := meta.(*AliyunClient).essconn 61 62 rule, err := essconn.CreateScalingRule(args) 63 if err != nil { 64 return err 65 } 66 67 d.SetId(d.Get("scaling_group_id").(string) + COLON_SEPARATED + rule.ScalingRuleId) 68 69 return resourceAliyunEssScalingRuleUpdate(d, meta) 70 } 71 72 func resourceAliyunEssScalingRuleRead(d *schema.ResourceData, meta interface{}) error { 73 74 client := meta.(*AliyunClient) 75 ids := strings.Split(d.Id(), COLON_SEPARATED) 76 77 rule, err := client.DescribeScalingRuleById(ids[0], ids[1]) 78 if err != nil { 79 if e, ok := err.(*common.Error); ok && e.Code == InstanceNotfound { 80 d.SetId("") 81 return nil 82 } 83 return fmt.Errorf("Error Describe ESS scaling rule Attribute: %#v", err) 84 } 85 86 d.Set("scaling_group_id", rule.ScalingGroupId) 87 d.Set("ari", rule.ScalingRuleAri) 88 d.Set("adjustment_type", rule.AdjustmentType) 89 d.Set("adjustment_value", rule.AdjustmentValue) 90 d.Set("scaling_rule_name", rule.ScalingRuleName) 91 d.Set("cooldown", rule.Cooldown) 92 93 return nil 94 } 95 96 func resourceAliyunEssScalingRuleDelete(d *schema.ResourceData, meta interface{}) error { 97 client := meta.(*AliyunClient) 98 ids := strings.Split(d.Id(), COLON_SEPARATED) 99 100 return resource.Retry(2*time.Minute, func() *resource.RetryError { 101 err := client.DeleteScalingRuleById(ids[1]) 102 103 if err != nil { 104 return resource.RetryableError(fmt.Errorf("Scaling rule in use - trying again while it is deleted.")) 105 } 106 107 _, err = client.DescribeScalingRuleById(ids[0], ids[1]) 108 if err != nil { 109 if notFoundError(err) { 110 return nil 111 } 112 return resource.NonRetryableError(err) 113 } 114 115 return resource.RetryableError(fmt.Errorf("Scaling rule in use - trying again while it is deleted.")) 116 }) 117 } 118 119 func resourceAliyunEssScalingRuleUpdate(d *schema.ResourceData, meta interface{}) error { 120 121 conn := meta.(*AliyunClient).essconn 122 ids := strings.Split(d.Id(), COLON_SEPARATED) 123 124 args := &ess.ModifyScalingRuleArgs{ 125 ScalingRuleId: ids[1], 126 } 127 128 if d.HasChange("adjustment_type") { 129 args.AdjustmentType = ess.AdjustmentType(d.Get("adjustment_type").(string)) 130 } 131 132 if d.HasChange("adjustment_value") { 133 args.AdjustmentValue = d.Get("adjustment_value").(int) 134 } 135 136 if d.HasChange("scaling_rule_name") { 137 args.ScalingRuleName = d.Get("scaling_rule_name").(string) 138 } 139 140 if d.HasChange("cooldown") { 141 args.Cooldown = d.Get("cooldown").(int) 142 } 143 144 if _, err := conn.ModifyScalingRule(args); err != nil { 145 return err 146 } 147 148 return resourceAliyunEssScalingRuleRead(d, meta) 149 } 150 151 func buildAlicloudEssScalingRuleArgs(d *schema.ResourceData, meta interface{}) (*ess.CreateScalingRuleArgs, error) { 152 args := &ess.CreateScalingRuleArgs{ 153 RegionId: getRegion(d, meta), 154 ScalingGroupId: d.Get("scaling_group_id").(string), 155 AdjustmentType: ess.AdjustmentType(d.Get("adjustment_type").(string)), 156 AdjustmentValue: d.Get("adjustment_value").(int), 157 } 158 159 if v := d.Get("scaling_rule_name").(string); v != "" { 160 args.ScalingRuleName = v 161 } 162 163 if v := d.Get("cooldown").(int); v != 0 { 164 args.Cooldown = v 165 } 166 167 return args, nil 168 }