github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_ess_schedule.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 "time" 10 ) 11 12 func resourceAlicloudEssSchedule() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceAliyunEssScheduleCreate, 15 Read: resourceAliyunEssScheduleRead, 16 Update: resourceAliyunEssScheduleUpdate, 17 Delete: resourceAliyunEssScheduleDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "scheduled_action": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 }, 24 "launch_time": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 }, 28 "scheduled_task_name": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 }, 32 "description": &schema.Schema{ 33 Type: schema.TypeString, 34 Computed: true, 35 Optional: true, 36 }, 37 "launch_expiration_time": &schema.Schema{ 38 Type: schema.TypeInt, 39 Default: 600, 40 Optional: true, 41 ValidateFunc: validateIntegerInRange(0, 21600), 42 }, 43 "recurrence_type": &schema.Schema{ 44 Type: schema.TypeString, 45 Computed: true, 46 Optional: true, 47 ValidateFunc: validateAllowedStringValue([]string{string(ess.Daily), 48 string(ess.Weekly), string(ess.Monthly)}), 49 }, 50 "recurrence_value": &schema.Schema{ 51 Type: schema.TypeString, 52 Computed: true, 53 Optional: true, 54 }, 55 "recurrence_end_time": &schema.Schema{ 56 Type: schema.TypeString, 57 Computed: true, 58 Optional: true, 59 }, 60 "task_enabled": &schema.Schema{ 61 Type: schema.TypeBool, 62 Default: true, 63 Optional: true, 64 }, 65 }, 66 } 67 } 68 69 func resourceAliyunEssScheduleCreate(d *schema.ResourceData, meta interface{}) error { 70 71 args, err := buildAlicloudEssScheduleArgs(d, meta) 72 if err != nil { 73 return err 74 } 75 76 essconn := meta.(*AliyunClient).essconn 77 78 rule, err := essconn.CreateScheduledTask(args) 79 if err != nil { 80 return err 81 } 82 83 d.SetId(rule.ScheduledTaskId) 84 85 return resourceAliyunEssScheduleUpdate(d, meta) 86 } 87 88 func resourceAliyunEssScheduleRead(d *schema.ResourceData, meta interface{}) error { 89 90 client := meta.(*AliyunClient) 91 92 rule, err := client.DescribeScheduleById(d.Id()) 93 if err != nil { 94 if e, ok := err.(*common.Error); ok && e.Code == InstanceNotfound { 95 d.SetId("") 96 return nil 97 } 98 return fmt.Errorf("Error Describe ESS schedule Attribute: %#v", err) 99 } 100 101 d.Set("scheduled_action", rule.ScheduledAction) 102 d.Set("launch_time", rule.LaunchTime) 103 d.Set("scheduled_task_name", rule.ScheduledTaskName) 104 d.Set("description", rule.Description) 105 d.Set("launch_expiration_time", rule.LaunchExpirationTime) 106 d.Set("recurrence_type", rule.RecurrenceType) 107 d.Set("recurrence_value", rule.RecurrenceValue) 108 d.Set("recurrence_end_time", rule.RecurrenceEndTime) 109 d.Set("task_enabled", rule.TaskEnabled) 110 111 return nil 112 } 113 114 func resourceAliyunEssScheduleUpdate(d *schema.ResourceData, meta interface{}) error { 115 116 conn := meta.(*AliyunClient).essconn 117 118 args := &ess.ModifyScheduledTaskArgs{ 119 ScheduledTaskId: d.Id(), 120 } 121 122 if d.HasChange("scheduled_task_name") { 123 args.ScheduledTaskName = d.Get("scheduled_task_name").(string) 124 } 125 126 if d.HasChange("description") { 127 args.Description = d.Get("description").(string) 128 } 129 130 if d.HasChange("scheduled_action") { 131 args.ScheduledAction = d.Get("scheduled_action").(string) 132 } 133 134 if d.HasChange("launch_time") { 135 args.LaunchTime = d.Get("launch_time").(string) 136 } 137 138 if d.HasChange("launch_expiration_time") { 139 args.LaunchExpirationTime = d.Get("launch_expiration_time").(int) 140 } 141 142 if d.HasChange("recurrence_type") { 143 args.RecurrenceType = ess.RecurrenceType(d.Get("recurrence_type").(string)) 144 } 145 146 if d.HasChange("recurrence_value") { 147 args.RecurrenceValue = d.Get("recurrence_value").(string) 148 } 149 150 if d.HasChange("recurrence_end_time") { 151 args.RecurrenceEndTime = d.Get("recurrence_end_time").(string) 152 } 153 154 if d.HasChange("task_enabled") { 155 args.TaskEnabled = d.Get("task_enabled").(bool) 156 } 157 158 if _, err := conn.ModifyScheduledTask(args); err != nil { 159 return err 160 } 161 162 return resourceAliyunEssScheduleRead(d, meta) 163 } 164 165 func resourceAliyunEssScheduleDelete(d *schema.ResourceData, meta interface{}) error { 166 client := meta.(*AliyunClient) 167 168 return resource.Retry(2*time.Minute, func() *resource.RetryError { 169 err := client.DeleteScheduleById(d.Id()) 170 171 if err != nil { 172 return resource.RetryableError(fmt.Errorf("Scaling schedule in use - trying again while it is deleted.")) 173 } 174 175 _, err = client.DescribeScheduleById(d.Id()) 176 if err != nil { 177 if notFoundError(err) { 178 return nil 179 } 180 return resource.NonRetryableError(err) 181 } 182 183 return resource.RetryableError(fmt.Errorf("Scaling schedule in use - trying again while it is deleted.")) 184 }) 185 } 186 187 func buildAlicloudEssScheduleArgs(d *schema.ResourceData, meta interface{}) (*ess.CreateScheduledTaskArgs, error) { 188 args := &ess.CreateScheduledTaskArgs{ 189 RegionId: getRegion(d, meta), 190 ScheduledAction: d.Get("scheduled_action").(string), 191 LaunchTime: d.Get("launch_time").(string), 192 TaskEnabled: d.Get("task_enabled").(bool), 193 } 194 195 if v := d.Get("scheduled_task_name").(string); v != "" { 196 args.ScheduledTaskName = v 197 } 198 199 if v := d.Get("description").(string); v != "" { 200 args.Description = v 201 } 202 203 if v := d.Get("recurrence_type").(string); v != "" { 204 args.RecurrenceType = ess.RecurrenceType(v) 205 } 206 207 if v := d.Get("recurrence_value").(string); v != "" { 208 args.RecurrenceValue = v 209 } 210 211 if v := d.Get("recurrence_end_time").(string); v != "" { 212 args.RecurrenceEndTime = v 213 } 214 215 if v := d.Get("launch_expiration_time").(int); v != 0 { 216 args.LaunchExpirationTime = v 217 } 218 219 return args, nil 220 }