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