github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/aws/resource_aws_autoscaling_schedule.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/autoscaling" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 const awsAutoscalingScheduleTimeLayout = "2006-01-02T15:04:05Z" 14 15 func resourceAwsAutoscalingSchedule() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsAutoscalingScheduleCreate, 18 Read: resourceAwsAutoscalingScheduleRead, 19 Update: resourceAwsAutoscalingScheduleCreate, 20 Delete: resourceAwsAutoscalingScheduleDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "arn": &schema.Schema{ 24 Type: schema.TypeString, 25 Computed: true, 26 }, 27 "scheduled_action_name": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 "autoscaling_group_name": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 "start_time": &schema.Schema{ 38 Type: schema.TypeString, 39 Optional: true, 40 Computed: true, 41 ValidateFunc: validateASGScheduleTimestamp, 42 }, 43 "end_time": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 Computed: true, 47 ValidateFunc: validateASGScheduleTimestamp, 48 }, 49 "recurrence": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 Computed: true, 53 }, 54 "min_size": &schema.Schema{ 55 Type: schema.TypeInt, 56 Optional: true, 57 Computed: true, 58 }, 59 "max_size": &schema.Schema{ 60 Type: schema.TypeInt, 61 Optional: true, 62 Computed: true, 63 }, 64 "desired_capacity": &schema.Schema{ 65 Type: schema.TypeInt, 66 Optional: true, 67 Computed: true, 68 }, 69 }, 70 } 71 } 72 73 func resourceAwsAutoscalingScheduleCreate(d *schema.ResourceData, meta interface{}) error { 74 autoscalingconn := meta.(*AWSClient).autoscalingconn 75 params := &autoscaling.PutScheduledUpdateGroupActionInput{ 76 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 77 ScheduledActionName: aws.String(d.Get("scheduled_action_name").(string)), 78 } 79 80 if attr, ok := d.GetOk("start_time"); ok { 81 t, err := time.Parse(awsAutoscalingScheduleTimeLayout, attr.(string)) 82 if err != nil { 83 return fmt.Errorf("Error Parsing AWS Autoscaling Group Schedule Start Time: %s", err.Error()) 84 } 85 params.StartTime = aws.Time(t) 86 } 87 88 if attr, ok := d.GetOk("end_time"); ok { 89 t, err := time.Parse(awsAutoscalingScheduleTimeLayout, attr.(string)) 90 if err != nil { 91 return fmt.Errorf("Error Parsing AWS Autoscaling Group Schedule End Time: %s", err.Error()) 92 } 93 params.EndTime = aws.Time(t) 94 } 95 96 if attr, ok := d.GetOk("recurrence"); ok { 97 params.Recurrence = aws.String(attr.(string)) 98 } 99 100 if attr, ok := d.GetOk("min_size"); ok { 101 params.MinSize = aws.Int64(int64(attr.(int))) 102 } 103 104 if attr, ok := d.GetOk("max_size"); ok { 105 params.MaxSize = aws.Int64(int64(attr.(int))) 106 } 107 108 if attr, ok := d.GetOk("desired_capacity"); ok { 109 params.DesiredCapacity = aws.Int64(int64(attr.(int))) 110 } 111 112 log.Printf("[INFO] Creating Autoscaling Scheduled Action: %s", d.Get("scheduled_action_name").(string)) 113 _, err := autoscalingconn.PutScheduledUpdateGroupAction(params) 114 if err != nil { 115 return fmt.Errorf("Error Creating Autoscaling Scheduled Action: %s", err.Error()) 116 } 117 118 d.SetId(d.Get("scheduled_action_name").(string)) 119 120 return resourceAwsAutoscalingScheduleRead(d, meta) 121 } 122 123 func resourceAwsAutoscalingScheduleRead(d *schema.ResourceData, meta interface{}) error { 124 sa, err := resourceAwsASGScheduledActionRetrieve(d, meta) 125 if err != nil { 126 return err 127 } 128 129 d.Set("autoscaling_group_name", sa.AutoScalingGroupName) 130 d.Set("arn", sa.ScheduledActionARN) 131 d.Set("desired_capacity", sa.DesiredCapacity) 132 d.Set("min_size", sa.MinSize) 133 d.Set("max_size", sa.MaxSize) 134 d.Set("recurrence", sa.Recurrence) 135 136 if sa.StartTime != nil { 137 d.Set("start_time", sa.StartTime.Format(awsAutoscalingScheduleTimeLayout)) 138 } 139 140 if sa.EndTime != nil { 141 d.Set("end_time", sa.EndTime.Format(awsAutoscalingScheduleTimeLayout)) 142 } 143 144 return nil 145 } 146 147 func resourceAwsAutoscalingScheduleDelete(d *schema.ResourceData, meta interface{}) error { 148 autoscalingconn := meta.(*AWSClient).autoscalingconn 149 150 params := &autoscaling.DeleteScheduledActionInput{ 151 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 152 ScheduledActionName: aws.String(d.Id()), 153 } 154 155 log.Printf("[INFO] Deleting Autoscaling Scheduled Action: %s", d.Id()) 156 _, err := autoscalingconn.DeleteScheduledAction(params) 157 if err != nil { 158 return fmt.Errorf("Error deleting Autoscaling Scheduled Action: %s", err.Error()) 159 } 160 161 return nil 162 } 163 164 func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error) { 165 autoscalingconn := meta.(*AWSClient).autoscalingconn 166 167 params := &autoscaling.DescribeScheduledActionsInput{ 168 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 169 ScheduledActionNames: []*string{aws.String(d.Id())}, 170 } 171 172 log.Printf("[INFO] Describing Autoscaling Scheduled Action: %+v", params) 173 actions, err := autoscalingconn.DescribeScheduledActions(params) 174 if err != nil { 175 return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err) 176 } 177 178 if len(actions.ScheduledUpdateGroupActions) != 1 || 179 *actions.ScheduledUpdateGroupActions[0].ScheduledActionName != d.Id() { 180 return nil, fmt.Errorf("Unable to find Autoscaling Scheduled Action: %#v", actions.ScheduledUpdateGroupActions) 181 } 182 183 return actions.ScheduledUpdateGroupActions[0], nil 184 }