-
Notifications
You must be signed in to change notification settings - Fork 55
/
resource_spotinst_elastigroup_azure.go
317 lines (271 loc) · 10.8 KB
/
resource_spotinst_elastigroup_azure.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
package spotinst
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/elastigroup/providers/azure"
"github.com/spotinst/spotinst-sdk-go/spotinst"
"github.com/spotinst/spotinst-sdk-go/spotinst/client"
"github.com/spotinst/terraform-provider-spotinst/spotinst/commons"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_health_check"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_image"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_integrations"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_launch_configuration"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_load_balancer"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_login"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_network"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_scaling_policies"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_scheduled_task"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_strategy"
"github.com/spotinst/terraform-provider-spotinst/spotinst/elastigroup_azure_vm_sizes"
)
func resourceSpotinstElastigroupAzure() *schema.Resource {
setupElastigroupAzureResource()
return &schema.Resource{
Create: resourceSpotinstElastigroupAzureCreate,
Read: resourceSpotinstElastigroupAzureRead,
Update: resourceSpotinstElastigroupAzureUpdate,
Delete: resourceSpotinstElastigroupAzureDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: commons.ElastigroupAzureResource.GetSchemaMap(),
}
}
func setupElastigroupAzureResource() {
fieldsMap := make(map[commons.FieldName]*commons.GenericField)
elastigroup_azure.Setup(fieldsMap)
elastigroup_azure_health_check.Setup(fieldsMap)
elastigroup_azure_image.Setup(fieldsMap)
elastigroup_azure_integrations.Setup(fieldsMap)
elastigroup_azure_launch_configuration.Setup(fieldsMap)
elastigroup_azure_load_balancer.Setup(fieldsMap)
elastigroup_azure_login.Setup(fieldsMap)
elastigroup_azure_network.Setup(fieldsMap)
elastigroup_azure_strategy.Setup(fieldsMap)
elastigroup_azure_vm_sizes.Setup(fieldsMap)
elastigroup_azure_scheduled_task.Setup(fieldsMap)
elastigroup_azure_scaling_policies.Setup(fieldsMap)
commons.ElastigroupAzureResource = commons.NewElastigroupAzureResource(fieldsMap)
}
func resourceSpotinstElastigroupAzureCreate(resourceData *schema.ResourceData, meta interface{}) error {
log.Printf(string(commons.ResourceOnCreate),
commons.ElastigroupAzureResource.GetName())
elastigroup, err := commons.ElastigroupAzureResource.OnCreate(resourceData, meta)
if err != nil {
return err
}
groupId, err := createAzureGroup(elastigroup, meta.(*Client))
if err != nil {
return err
}
resourceData.SetId(spotinst.StringValue(groupId))
log.Printf("===> Elastigroup created successfully: %s <===", resourceData.Id())
return resourceSpotinstElastigroupAzureRead(resourceData, meta)
}
func createAzureGroup(group *azure.Group, spotinstClient *Client) (*string, error) {
if json, err := commons.ToJson(group); err != nil {
return nil, err
} else {
log.Printf("===> Group create configuration: %s", json)
}
var resp *azure.CreateGroupOutput = nil
err := resource.Retry(time.Minute, func() *resource.RetryError {
input := &azure.CreateGroupInput{Group: group}
r, err := spotinstClient.elastigroup.CloudProviderAzure().Create(context.Background(), input)
if err != nil {
log.Printf("error: %v", err)
// Some other error, report it.
return resource.NonRetryableError(err)
}
resp = r
return nil
})
if err != nil {
return nil, fmt.Errorf("[ERROR] failed to create group: %s", err)
}
return resp.Group.ID, nil
}
func resourceSpotinstElastigroupAzureRead(resourceData *schema.ResourceData, meta interface{}) error {
id := resourceData.Id()
log.Printf(string(commons.ResourceFieldOnRead),
commons.ElastigroupAzureResource.GetName(), id)
input := &azure.ReadGroupInput{GroupID: spotinst.String(id)}
resp, err := meta.(*Client).elastigroup.CloudProviderAzure().Read(context.Background(), input)
if err != nil {
// If the group was not found, return nil so that we can show
// that the group does not exist
if errs, ok := err.(client.Errors); ok && len(errs) > 0 {
for _, err := range errs {
if err.Code == ErrCodeGroupNotFound {
resourceData.SetId("")
return nil
}
}
}
// Some other error, report it.
return fmt.Errorf("failed to read group: %s", err)
}
// If nothing was found, then return no state.
groupResponse := resp.Group
if groupResponse == nil {
resourceData.SetId("")
return nil
}
if err := commons.ElastigroupAzureResource.OnRead(groupResponse, resourceData, meta); err != nil {
return err
}
log.Printf("===> Elastigroup read successfully: %s <===", id)
return nil
}
func resourceSpotinstElastigroupAzureUpdate(resourceData *schema.ResourceData, meta interface{}) error {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnUpdate),
commons.ElastigroupAzureResource.GetName(), id)
shouldUpdate, elastigroup, err := commons.ElastigroupAzureResource.OnUpdate(resourceData, meta)
if err != nil {
return err
}
if shouldUpdate {
elastigroup.SetId(spotinst.String(id))
if err := updateAzureGroup(elastigroup, resourceData, meta); err != nil {
return err
}
}
log.Printf("===> Elastigroup updated successfully: %s <===", id)
return resourceSpotinstElastigroupAzureRead(resourceData, meta)
}
func updateAzureGroup(elastigroup *azure.Group, resourceData *schema.ResourceData, meta interface{}) error {
var input = &azure.UpdateGroupInput{
Group: elastigroup,
}
var shouldRoll = false
groupId := resourceData.Id()
if updatePolicy, exists := resourceData.GetOkExists(string(elastigroup_azure.UpdatePolicy)); exists {
list := updatePolicy.([]interface{})
if len(list) > 0 && list[0] != nil {
m := list[0].(map[string]interface{})
if roll, ok := m[string(elastigroup_azure.ShouldRoll)].(bool); ok && roll {
shouldRoll = roll
}
}
}
if json, err := commons.ToJson(elastigroup); err != nil {
return err
} else {
log.Printf("===> Group update configuration: %s", json)
}
if _, err := meta.(*Client).elastigroup.CloudProviderAzure().Update(context.Background(), input); err != nil {
return fmt.Errorf("[ERROR] Failed to update group [%v]: %v", groupId, err)
} else if shouldRoll {
if err := rollAzureGroup(resourceData, meta); err != nil {
log.Printf("[ERROR] Group [%v] roll failed, error: %v", groupId, err)
return err
}
} else {
log.Printf("onRoll() -> Field [%v] is false, skipping group roll", string(elastigroup_azure.ShouldRoll))
}
return nil
}
func rollAzureGroup(resourceData *schema.ResourceData, meta interface{}) error {
var errResult error = nil
groupId := resourceData.Id()
if updatePolicy, exists := resourceData.GetOkExists(string(elastigroup_azure.UpdatePolicy)); exists {
list := updatePolicy.([]interface{})
if len(list) > 0 && list[0] != nil {
updateGroupSchema := list[0].(map[string]interface{})
if rollConfig, ok := updateGroupSchema[string(elastigroup_azure.RollConfig)]; !ok || rollConfig == nil {
errResult = fmt.Errorf("[ERROR] onRoll() -> Field [%v] is missing, skipping roll for group [%v]", string(elastigroup_azure.RollConfig), groupId)
} else {
if rollGroupInput, err := expandElastigroupAzureRollConfig(rollConfig, spotinst.String(groupId)); err != nil {
errResult = fmt.Errorf("[ERROR] onRoll() -> Failed expanding roll configuration for group [%v], error: %v", groupId, err)
} else {
if json, err := commons.ToJson(rollConfig); err != nil {
return err
} else {
log.Printf("onRoll() -> Rolling group [%v] with configuration %s", groupId, json)
errResult = resource.Retry(time.Minute*5, func() *resource.RetryError {
rollGroupInput.GroupID = spotinst.String(groupId)
_, err := meta.(*Client).elastigroup.CloudProviderAzure().Roll(context.Background(), rollGroupInput)
if err != nil {
// checks whether to retry role
if errs, ok := err.(client.Errors); ok && len(errs) > 0 {
for _, err := range errs {
if strings.Contains(err.Code, "CANT_ROLL_CAPACITY_BELOW_MINIMUM") {
time.Sleep(time.Minute)
return resource.RetryableError(err)
}
}
}
// Some other error, report it.
return resource.NonRetryableError(err)
}
log.Printf("onRoll() -> Successfully rolled group [%v]", groupId)
return nil
})
}
}
}
}
} else {
errResult = fmt.Errorf("[ERROR] onRoll() -> Missing update policy for group [%v]", groupId)
}
return errResult
}
func resourceSpotinstElastigroupAzureDelete(resourceData *schema.ResourceData, meta interface{}) error {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnDelete),
commons.ElastigroupAzureResource.GetName(), id)
if err := deleteAzureGroup(resourceData, meta); err != nil {
return err
}
log.Printf("===> Elastigroup deleted successfully: %s <===", resourceData.Id())
resourceData.SetId("")
return nil
}
func deleteAzureGroup(resourceData *schema.ResourceData, meta interface{}) error {
groupId := resourceData.Id()
input := &azure.DeleteGroupInput{
GroupID: spotinst.String(groupId),
}
if json, err := commons.ToJson(input); err != nil {
return err
} else {
log.Printf("===> Group delete configuration: %s", json)
}
if _, err := meta.(*Client).elastigroup.CloudProviderAzure().Delete(context.Background(), input); err != nil {
return fmt.Errorf("[ERROR] onDelete() -> Failed to delete group: %s", err)
}
return nil
}
func expandElastigroupAzureRollConfig(data interface{}, groupID *string) (*azure.RollGroupInput, error) {
i := &azure.RollGroupInput{GroupID: groupID}
list := data.([]interface{})
if list != nil && list[0] != nil {
m := list[0].(map[string]interface{})
if v, ok := m[string(elastigroup_azure.BatchSizePercentage)].(int); ok {
i.BatchSizePercentage = spotinst.Int(v)
}
if v, ok := m[string(elastigroup_azure.GracePeriod)].(int); ok && v != -1 {
i.GracePeriod = spotinst.Int(v)
}
if v, ok := m[string(elastigroup_azure_health_check.HealthCheckType)].(string); ok && v != "" {
i.HealthCheckType = spotinst.String(v)
}
}
return i, nil
}
func getAzureRollStatus(rollOut *azure.RollGroupOutput) *string {
for item := range rollOut.Items {
rs := strings.ToUpper(spotinst.StringValue(rollOut.Items[item].Status))
if rs == "IN_PROGRESS" || rs == "STARTING" {
return rollOut.Items[item].RollID
}
}
return nil
}