-
Notifications
You must be signed in to change notification settings - Fork 134
/
resource_tc_as_schedule.go
219 lines (192 loc) · 7.53 KB
/
resource_tc_as_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package as
import (
"context"
"fmt"
"log"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func ResourceTencentCloudAsSchedule() *schema.Resource {
return &schema.Resource{
Create: resourceTencentCloudAsScheduleCreate,
Read: resourceTencentCloudAsScheduleRead,
Update: resourceTencentCloudAsScheduleUpdate,
Delete: resourceTencentCloudAsScheduleDelete,
Schema: map[string]*schema.Schema{
"scaling_group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "ID of a scaling group.",
},
"schedule_action_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: tccommon.ValidateStringLengthInRange(1, 60),
Description: "The name of this scaling action.",
},
"max_size": {
Type: schema.TypeInt,
Required: true,
Description: "The maximum size for the Auto Scaling group.",
},
"min_size": {
Type: schema.TypeInt,
Required: true,
Description: "The minimum size for the Auto Scaling group.",
},
"desired_capacity": {
Type: schema.TypeInt,
Required: true,
Description: "The desired number of CVM instances that should be running in the group.",
},
"start_time": {
Type: schema.TypeString,
Required: true,
ValidateFunc: tccommon.ValidateAsScheduleTimestamp,
Description: "The time for this action to start, in \"YYYY-MM-DDThh:mm:ss+08:00\" format (UTC+8).",
},
"end_time": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: tccommon.ValidateAsScheduleTimestamp,
Description: "The time for this action to end, in \"YYYY-MM-DDThh:mm:ss+08:00\" format (UTC+8).",
},
"recurrence": {
Type: schema.TypeString,
Optional: true,
Description: "The time when recurring future actions will start. Start time is specified by the user following the Unix cron syntax format. And this argument should be set with end_time together.",
},
},
}
}
func resourceTencentCloudAsScheduleCreate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_as_schedule.create")()
logId := tccommon.GetLogId(tccommon.ContextNil)
request := as.NewCreateScheduledActionRequest()
request.AutoScalingGroupId = helper.String(d.Get("scaling_group_id").(string))
request.ScheduledActionName = helper.String(d.Get("schedule_action_name").(string))
request.MaxSize = helper.IntUint64(d.Get("max_size").(int))
request.MinSize = helper.IntUint64(d.Get("min_size").(int))
request.DesiredCapacity = helper.IntUint64(d.Get("desired_capacity").(int))
request.StartTime = helper.String(d.Get("start_time").(string))
// end_time and recurrence must be specified at the same time
if v, ok := d.GetOk("end_time"); ok {
request.EndTime = helper.String(v.(string))
if vv, ok := d.GetOk("recurrence"); ok {
request.Recurrence = helper.String(vv.(string))
} else {
return fmt.Errorf("end_time and recurrence must be specified at the same time.")
}
} else {
if _, ok := d.GetOk("recurrence"); ok {
return fmt.Errorf("end_time and recurrence must be specified at the same time.")
}
}
response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().CreateScheduledAction(request)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), err.Error())
return err
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
if response.Response.ScheduledActionId == nil {
return fmt.Errorf("schedule action id is nil")
}
d.SetId(*response.Response.ScheduledActionId)
return resourceTencentCloudAsScheduleRead(d, meta)
}
func resourceTencentCloudAsScheduleRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_as_schedule.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
scheduledActionId := d.Id()
asService := AsService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
scheduledAction, has, e := asService.DescribeScheduledActionById(ctx, scheduledActionId)
if e != nil {
return tccommon.RetryError(e)
}
if has == 0 {
d.SetId("")
return nil
}
_ = d.Set("scaling_group_id", *scheduledAction.AutoScalingGroupId)
_ = d.Set("schedule_action_name", *scheduledAction.ScheduledActionName)
_ = d.Set("max_size", *scheduledAction.MaxSize)
_ = d.Set("min_size", *scheduledAction.MinSize)
_ = d.Set("desired_capacity", *scheduledAction.DesiredCapacity)
_ = d.Set("start_time", *scheduledAction.StartTime)
if scheduledAction.EndTime != nil {
_ = d.Set("end_time", *scheduledAction.EndTime)
}
if scheduledAction.Recurrence != nil {
_ = d.Set("recurrence", *scheduledAction.Recurrence)
}
return nil
})
if err != nil {
return err
}
return nil
}
func resourceTencentCloudAsScheduleUpdate(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_as_schedule.update")()
logId := tccommon.GetLogId(tccommon.ContextNil)
request := as.NewModifyScheduledActionRequest()
scheduledActionId := d.Id()
request.ScheduledActionId = &scheduledActionId
if d.HasChange("schedule_action_name") {
request.ScheduledActionName = helper.String(d.Get("schedule_action_name").(string))
}
if d.HasChange("max_size") {
request.MaxSize = helper.IntUint64(d.Get("max_size").(int))
}
if d.HasChange("min_size") {
request.MinSize = helper.IntUint64(d.Get("min_size").(int))
}
if d.HasChange("desired_capacity") {
request.DesiredCapacity = helper.IntUint64(d.Get("desired_capacity").(int))
}
if d.HasChange("start_time") {
request.StartTime = helper.String(d.Get("start_time").(string))
}
if d.HasChange("end_time") {
request.EndTime = helper.String(d.Get("end_time").(string))
request.Recurrence = helper.String(d.Get("recurrence").(string))
}
if d.HasChange("recurrence") {
request.Recurrence = helper.String(d.Get("recurrence").(string))
request.EndTime = helper.String(d.Get("end_time").(string))
}
response, err := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseAsClient().ModifyScheduledAction(request)
if err != nil {
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
logId, request.GetAction(), request.ToJsonString(), err.Error())
return err
}
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
return nil
}
func resourceTencentCloudAsScheduleDelete(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("resource.tencentcloud_as_schedule.delete")()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
scheduledActionId := d.Id()
asService := AsService{
client: meta.(tccommon.ProviderMeta).GetAPIV3Conn(),
}
err := asService.DeleteScheduledAction(ctx, scheduledActionId)
if err != nil {
return err
}
return nil
}