github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/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 params.MinSize = aws.Int64(int64(d.Get("min_size").(int))) 101 params.MaxSize = aws.Int64(int64(d.Get("max_size").(int))) 102 params.DesiredCapacity = aws.Int64(int64(d.Get("desired_capacity").(int))) 103 104 log.Printf("[INFO] Creating Autoscaling Scheduled Action: %s", d.Get("scheduled_action_name").(string)) 105 _, err := autoscalingconn.PutScheduledUpdateGroupAction(params) 106 if err != nil { 107 return fmt.Errorf("Error Creating Autoscaling Scheduled Action: %s", err.Error()) 108 } 109 110 d.SetId(d.Get("scheduled_action_name").(string)) 111 112 return resourceAwsAutoscalingScheduleRead(d, meta) 113 } 114 115 func resourceAwsAutoscalingScheduleRead(d *schema.ResourceData, meta interface{}) error { 116 sa, err := resourceAwsASGScheduledActionRetrieve(d, meta) 117 if err != nil { 118 return err 119 } 120 121 d.Set("autoscaling_group_name", sa.AutoScalingGroupName) 122 d.Set("arn", sa.ScheduledActionARN) 123 d.Set("desired_capacity", sa.DesiredCapacity) 124 d.Set("min_size", sa.MinSize) 125 d.Set("max_size", sa.MaxSize) 126 d.Set("recurrence", sa.Recurrence) 127 128 if sa.StartTime != nil { 129 d.Set("start_time", sa.StartTime.Format(awsAutoscalingScheduleTimeLayout)) 130 } 131 132 if sa.EndTime != nil { 133 d.Set("end_time", sa.EndTime.Format(awsAutoscalingScheduleTimeLayout)) 134 } 135 136 return nil 137 } 138 139 func resourceAwsAutoscalingScheduleDelete(d *schema.ResourceData, meta interface{}) error { 140 autoscalingconn := meta.(*AWSClient).autoscalingconn 141 142 params := &autoscaling.DeleteScheduledActionInput{ 143 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 144 ScheduledActionName: aws.String(d.Id()), 145 } 146 147 log.Printf("[INFO] Deleting Autoscaling Scheduled Action: %s", d.Id()) 148 _, err := autoscalingconn.DeleteScheduledAction(params) 149 if err != nil { 150 return fmt.Errorf("Error deleting Autoscaling Scheduled Action: %s", err.Error()) 151 } 152 153 return nil 154 } 155 156 func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error) { 157 autoscalingconn := meta.(*AWSClient).autoscalingconn 158 159 params := &autoscaling.DescribeScheduledActionsInput{ 160 AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)), 161 ScheduledActionNames: []*string{aws.String(d.Id())}, 162 } 163 164 log.Printf("[INFO] Describing Autoscaling Scheduled Action: %+v", params) 165 actions, err := autoscalingconn.DescribeScheduledActions(params) 166 if err != nil { 167 return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err) 168 } 169 170 if len(actions.ScheduledUpdateGroupActions) != 1 || 171 *actions.ScheduledUpdateGroupActions[0].ScheduledActionName != d.Id() { 172 return nil, fmt.Errorf("Unable to find Autoscaling Scheduled Action: %#v", actions.ScheduledUpdateGroupActions) 173 } 174 175 return actions.ScheduledUpdateGroupActions[0], nil 176 }