generated from davidji99/terraform-provider-scaffolding
/
resource_herokux_formation_autoscaling.go
450 lines (365 loc) · 13.2 KB
/
resource_herokux_formation_autoscaling.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package herokux
import (
"context"
"fmt"
"github.com/davidji99/terraform-provider-herokux/api/metrics"
"github.com/davidji99/tfph"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
"strings"
)
func resourceHerokuxFormationAutoscaling() *schema.Resource {
return &schema.Resource{
CreateContext: resourceHerokuxFormationAutoscalingCreate,
ReadContext: resourceHerokuxFormationAutoscalingRead,
UpdateContext: resourceHerokuxFormationAutoscalingUpdate,
DeleteContext: resourceHerokuxFormationAutoscalingDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceHerokuxFormationAutoscalingImport,
},
Timeouts: resourceTimeouts(),
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},
"process_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"is_active": {
Type: schema.TypeBool,
Required: true,
},
"min_quantity": {
Type: schema.TypeInt,
ValidateFunc: validation.IntAtLeast(1),
Required: true,
},
"max_quantity": {
Type: schema.TypeInt,
ValidateFunc: validation.IntAtLeast(1),
Required: true,
},
"desired_p95_response_time": {
Type: schema.TypeInt,
ValidateFunc: validation.IntAtLeast(1),
Required: true,
},
"dyno_size": {
Type: schema.TypeString,
Required: true,
StateFunc: formatSize,
},
"notification_channels": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Computed: true,
},
"notification_period": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"action_type": {
Type: schema.TypeString,
Computed: true,
},
"operation": {
Type: schema.TypeString,
Computed: true,
},
"quantity": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceHerokuxFormationAutoscalingImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*Config).API
// Parse te import ID for the appID and processType
importID, parseErr := tfph.ParseCompositeID(d.Id(), 2)
if parseErr != nil {
return nil, parseErr
}
appID := importID[0]
processType := importID[1]
monitor, _, findErr := client.Metrics.FindMonitorByName(appID, processType, metrics.FormationMonitorNames.LatencyScale)
if findErr != nil {
return nil, findErr
}
d.SetId(fmt.Sprintf("%s:%s:%s", monitor.GetAppID(), monitor.GetProcessType(), monitor.GetID()))
readErr := resourceHerokuxFormationAutoscalingRead(ctx, d, meta)
if readErr.HasError() {
return nil, fmt.Errorf(readErr[0].Summary)
}
return []*schema.ResourceData{d}, nil
}
func resourceHerokuxFormationAutoscalingCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := meta.(*Config).API
// Get app id and formation name
appID := getAppID(d)
processType := getProcessType(d)
// Check for existing autoscaling.
existingAlertCheckErr := checkForExistingMonitor(client, appID, processType,
metrics.FormationMonitorActionTypes.Scale.ToString(), metrics.FormationMonitorNames.LatencyScale.ToString())
if existingAlertCheckErr != nil {
return existingAlertCheckErr
}
opts := constructAutoscalingOpts(d)
opts.Name = metrics.FormationMonitorNames.LatencyScale
notificationChannels := make([]string, 0)
if v, ok := d.GetOk("notification_channels"); ok {
raw := v.([]interface{})
for _, r := range raw {
notificationChannels = append(notificationChannels, r.(string))
}
}
log.Printf("[DEBUG] notification_channels is : %v", notificationChannels)
opts.NotificationChannels = notificationChannels
log.Printf("[DEBUG] Creating formation autoscaling for app %s", appID)
fm, _, createErr := client.Metrics.CreateFormationAutoscaling(appID, processType, opts)
if createErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Unable to setup autoscaling for app [%s] process type [%s]", appID, processType),
Detail: createErr.Error(),
})
return diags
}
log.Printf("[DEBUG] Created formation autoscaling for app %s", appID)
// Set the ID to be a composite of the APP_ID, FORMATION_NAME, and MONITOR_ID
d.SetId(fmt.Sprintf("%s:%s:%s", appID, processType, fm.GetID()))
return resourceHerokuxFormationAutoscalingRead(ctx, d, meta)
}
func resourceHerokuxFormationAutoscalingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
metricsAPI := meta.(*Config).API
platformAPI := meta.(*Config).PlatformAPI
resourceID, parseErr := tfph.ParseCompositeID(d.Id(), 3)
if parseErr != nil {
return diag.FromErr(parseErr)
}
appID := resourceID[0]
processType := resourceID[1]
monitorID := resourceID[2]
monitor, _, getErr := metricsAPI.Metrics.GetMonitor(appID, processType, monitorID)
if getErr != nil {
return diag.FromErr(getErr)
}
d.Set("app_id", monitor.GetAppID())
d.Set("process_type", monitor.GetProcessType())
d.Set("is_active", monitor.GetIsActive())
d.Set("min_quantity", monitor.GetMinQuantity())
d.Set("max_quantity", monitor.GetMaxQuantity())
d.Set("action_type", monitor.GetActionType())
d.Set("operation", monitor.GetOperation())
d.Set("notification_period", monitor.GetNotificationPeriod())
p95, _ := monitor.GetValue().Int64()
d.Set("desired_p95_response_time", p95)
// Get formation information in order to retrieve the dyno size as it's not returned by the above call.
formation, formationGetErr := platformAPI.FormationInfo(context.TODO(), appID, processType)
if formationGetErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("unable to retrieve formation %s information", processType),
Detail: formationGetErr.Error(),
})
return diags
}
d.Set("dyno_size", formation.Size)
notificationChannels := make([]string, 0)
if monitor.HasNotificationChannels() {
notificationChannels = monitor.NotificationChannels
}
d.Set("notification_channels", notificationChannels)
return nil
}
func resourceHerokuxFormationAutoscalingUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*Config).API
// Get app id and formation name
resourceID, parseErr := tfph.ParseCompositeID(d.Id(), 3)
if parseErr != nil {
return diag.FromErr(parseErr)
}
appID := resourceID[0]
processType := resourceID[1]
monitorID := resourceID[2]
opts := constructAutoscalingOpts(d)
notificationChannels := make([]string, 0)
if ok := d.HasChange("notification_channels"); ok {
_, n := d.GetChange("notification_channels")
if n != nil {
raw := n.([]interface{})
for _, r := range raw {
notificationChannels = append(notificationChannels, r.(string))
}
}
}
log.Printf("[DEBUG] new notification_channels is : %v", notificationChannels)
opts.NotificationChannels = notificationChannels
log.Printf("[DEBUG] Updating formation autoscaling for app %s, formation: %s, monitor %s", appID, processType, monitorID)
isSet, resp, setErr := client.Metrics.UpdateFormationAutoscaling(appID, processType, monitorID, opts)
if setErr != nil {
return diag.FromErr(setErr)
}
// Specific error msg if the response code is 403, which might mean the user is trying to autoscale an unsupported dyno type
if resp.StatusCode == 403 {
return diag.Errorf("unable to autoscale likely due to unsupported dyno type")
}
if !isSet {
return diag.Errorf("Did not successfully set autoscaling. StatusCode: %d", resp.StatusCode)
}
log.Printf("[DEBUG] Updated formation autoscaling for app %s, formation: %s, monitor %s", appID, processType, monitorID)
return resourceHerokuxFormationAutoscalingRead(ctx, d, meta)
}
func resourceHerokuxFormationAutoscalingDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
resourceID, parseErr := tfph.ParseCompositeID(d.Id(), 3)
if parseErr != nil {
return diag.FromErr(parseErr)
}
appID := resourceID[0]
processType := resourceID[1]
monitorID := resourceID[2]
config := meta.(*Config)
metricsAPI := config.API
platformAPI := config.PlatformAPI
// Get current monitor information
monitor, _, getErr := metricsAPI.Metrics.GetMonitor(appID, processType, monitorID)
if getErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("unable to retrieve monitor %s's information prior to resource deletion", monitorID),
Detail: getErr.Error(),
})
return diags
}
// Get formation information in order to retrieve the dyno size as it's not returned by the above call.
formation, formationGetErr := platformAPI.FormationInfo(context.TODO(), appID, processType)
if formationGetErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("unable to retrieve formation %s information prior to resource deletion", processType),
Detail: formationGetErr.Error(),
})
return diags
}
// In order to disable the autoscaling, we'll need to first retrieve the current details of the autoscaling.
// Then, create a new request to update the autoscaling with the current information but is_active set to `false`.
// This is the only way to safely, programmatically disable autoscaling like how the UI does it.
p95Raw, _ := monitor.GetValue().Int64()
opts := &metrics.FormationAutoscalingRequest{
DynoSize: formation.Size,
IsActive: false,
MaxQuantity: monitor.GetMaxQuantity(),
MinQuantity: monitor.GetMinQuantity(),
NotificationChannels: monitor.NotificationChannels,
NotificationPeriod: monitor.GetNotificationPeriod(),
DesiredP95RespTime: int(p95Raw),
Period: monitor.GetPeriod(),
ActionType: metrics.FormationMonitorActionTypes.Scale,
Operation: metrics.DefaultOperationAttrVal,
Name: *monitor.GetName(),
}
log.Printf("[DEBUG] Disabling formation autoscaling for app %s, process_type %s, monitor %s", appID, processType, monitorID)
isSet, resp, setErr := metricsAPI.Metrics.UpdateFormationAutoscaling(appID, processType, monitorID, opts)
if setErr != nil {
return diag.FromErr(setErr)
}
if !isSet {
return diag.Errorf("Did not successfully disable autoscaling. StatusCode: %d", resp.StatusCode)
}
log.Printf("[DEBUG] Disabled formation autoscaling for app %s, process_type %s, monitor %s", appID, processType, monitorID)
d.SetId("")
return nil
}
func constructAutoscalingOpts(d *schema.ResourceData) *metrics.FormationAutoscalingRequest {
opts := &metrics.FormationAutoscalingRequest{}
// Period is mainly used for app alerts so setting the value to be the default of 1.
opts.Period = 1
if v, ok := d.GetOk("is_active"); ok {
vs := v.(bool)
log.Printf("[DEBUG] is_active is : %v", vs)
opts.IsActive = vs
}
if v, ok := d.GetOk("dyno_size"); ok {
vs := v.(string)
log.Printf("[DEBUG] dyno_size is : %s", vs)
opts.DynoSize = vs
}
if v, ok := d.GetOk("min_quantity"); ok {
vs := v.(int)
log.Printf("[DEBUG] min_quantity is : %d", vs)
opts.MinQuantity = vs
}
if v, ok := d.GetOk("max_quantity"); ok {
vs := v.(int)
log.Printf("[DEBUG] max_quantity is : %d", vs)
opts.MaxQuantity = vs
}
if v, ok := d.GetOk("desired_p95_response_time"); ok {
vs := v.(int)
log.Printf("[DEBUG] desired_p95_response_time is : %d", vs)
opts.DesiredP95RespTime = vs
}
if v, ok := d.GetOk("notification_period"); ok {
vs := v.(int)
log.Printf("[DEBUG] notification_period is : %d", vs)
opts.NotificationPeriod = vs
}
// Define default values for certain FormationAutoscalingRequest fields based on the fact that these request fields
// only have a single value.
opts.ActionType = metrics.FormationMonitorActionTypes.Scale
opts.Quantity = 1
opts.Operation = metrics.DefaultOperationAttrVal
return opts
}
// Guarantees a consistent format for the string that describes the
// size of a dyno. A formation's size can be "free" or "standard-1x"
// or "Private-M".
//
// Heroku's PATCH formation endpoint accepts lowercase but
// returns the capitalised version. This ensures consistent
// capitalisation for state.
//
// For all supported dyno types see:
// https://devcenter.heroku.com/articles/dyno-types
// https://devcenter.heroku.com/articles/heroku-enterprise#available-dyno-types
func formatSize(quant interface{}) string {
if quant == nil || quant == (*string)(nil) {
return ""
}
var rawQuant string
switch t := quant.(type) {
case string:
log.Printf("[DEBUG] quant %v", t)
rawQuant = quant.(string)
case *string:
log.Printf("[DEBUG] quant %v", t)
rawQuant = *quant.(*string)
default:
return ""
}
// Capitalise the first descriptor, uppercase the remaining descriptors
var formattedSlice []string
s := strings.Split(rawQuant, "-")
for i := range s {
if i == 0 {
formattedSlice = append(formattedSlice, strings.Title(s[i]))
} else {
formattedSlice = append(formattedSlice, strings.ToUpper(s[i]))
}
}
return strings.Join(formattedSlice, "-")
}