-
Notifications
You must be signed in to change notification settings - Fork 56
/
resource_spotinst_mrscaler_aws.go
235 lines (191 loc) · 7.35 KB
/
resource_spotinst_mrscaler_aws.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
package spotinst
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/mrscaler"
"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/mrscaler_aws"
"github.com/spotinst/terraform-provider-spotinst/spotinst/mrscaler_aws_cluster"
"github.com/spotinst/terraform-provider-spotinst/spotinst/mrscaler_aws_instance_groups"
"github.com/spotinst/terraform-provider-spotinst/spotinst/mrscaler_aws_scaling_policies"
"github.com/spotinst/terraform-provider-spotinst/spotinst/mrscaler_aws_scheduled_task"
"github.com/spotinst/terraform-provider-spotinst/spotinst/mrscaler_aws_strategy"
"github.com/spotinst/terraform-provider-spotinst/spotinst/mrscaler_aws_terminationPolicies"
)
func resourceSpotinstMRScalerAWS() *schema.Resource {
setupMRScalerAWSResource()
return &schema.Resource{
CreateContext: resourceSpotinstMRScalerAWSCreate,
ReadContext: resourceSpotinstMRScalerAWSRead,
UpdateContext: resourceSpotinstMRScalerAWSUpdate,
DeleteContext: resourceSpotinstMRScalerAWSDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: commons.MRScalerAWSResource.GetSchemaMap(),
}
}
func setupMRScalerAWSResource() {
fieldsMap := make(map[commons.FieldName]*commons.GenericField)
mrscaler_aws.Setup(fieldsMap)
mrscaler_aws_strategy.Setup(fieldsMap)
mrscaler_aws_instance_groups.Setup(fieldsMap)
mrscaler_aws_cluster.Setup(fieldsMap)
mrscaler_aws_scaling_policies.Setup(fieldsMap)
mrscaler_aws_scheduled_task.Setup(fieldsMap)
mrscaler_aws_terminationPolicies.Setup(fieldsMap)
commons.MRScalerAWSResource = commons.NewMRScalerAWSResource(fieldsMap)
}
func resourceSpotinstMRScalerAWSCreate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf(string(commons.ResourceOnCreate),
commons.MRScalerAWSResource.GetName())
scaler, err := commons.MRScalerAWSResource.OnCreate(resourceData, meta)
if err != nil {
return diag.FromErr(err)
}
scalerId, err := createScaler(scaler, meta.(*Client))
if err != nil {
return diag.FromErr(err)
}
resourceData.SetId(spotinst.StringValue(scalerId))
log.Printf("===> MRScaler created successfully: %s <===", resourceData.Id())
return resourceSpotinstMRScalerAWSRead(ctx, resourceData, meta)
}
func createScaler(scaler *mrscaler.Scaler, spotinstClient *Client) (*string, error) {
if json, err := commons.ToJson(scaler); err != nil {
return nil, err
} else {
log.Printf("===> Scaler create configuration: %s", json)
}
var resp *mrscaler.CreateScalerOutput = nil
err := resource.RetryContext(context.Background(), time.Minute, func() *resource.RetryError {
input := &mrscaler.CreateScalerInput{Scaler: scaler}
r, err := spotinstClient.mrscaler.Create(context.Background(), input)
if err != nil {
// Checks whether we should retry the scaler creation.
if errs, ok := err.(client.Errors); ok && len(errs) > 0 {
for _, err := range errs {
if err.Code == "InvalidParameterValue" &&
strings.Contains(err.Message, "Invalid IAM Instance Profile") {
return resource.RetryableError(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 scaler: %s", err)
}
return resp.Scaler.ID, nil
}
func resourceSpotinstMRScalerAWSRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Id()
time.Sleep(10 * time.Second)
log.Printf(string(commons.ResourceOnRead),
commons.MRScalerAWSResource.GetName(), id)
input := &mrscaler.ReadScalerInput{ScalerID: spotinst.String(id)}
resp, err := meta.(*Client).mrscaler.Read(context.Background(), input)
if err != nil {
return diag.Errorf("failed to read mr scaler: %s", err)
}
// If nothing was found, then return no state.
scalerResponse := resp.Scaler
if scalerResponse == nil {
resourceData.SetId("")
return nil
}
if exist := resourceData.Get(string(mrscaler_aws.ExposeClusterID)).(bool); exist {
if err := exposeMrScalerClusterId(resourceData, meta); err != nil {
return diag.FromErr(err)
}
}
if err := commons.MRScalerAWSResource.OnRead(scalerResponse, resourceData, meta); err != nil {
return diag.FromErr(err)
}
log.Printf("===> MRScaler read successfully: %s <===", id)
return nil
}
func resourceSpotinstMRScalerAWSUpdate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnUpdate),
commons.MRScalerAWSResource.GetName(), id)
shouldUpdate, scaler, err := commons.MRScalerAWSResource.OnUpdate(resourceData, meta)
if err != nil {
return diag.FromErr(err)
}
if shouldUpdate {
scaler.SetId(spotinst.String(id))
if err := updateScaler(scaler, resourceData, meta); err != nil {
return diag.FromErr(err)
}
}
log.Printf("===> MRScaler updated successfully: %s <===", id)
return resourceSpotinstMRScalerAWSRead(ctx, resourceData, meta)
}
func updateScaler(scaler *mrscaler.Scaler, resourceData *schema.ResourceData, meta interface{}) error {
var input = &mrscaler.UpdateScalerInput{
Scaler: scaler,
}
scalerId := resourceData.Id()
if json, err := commons.ToJson(scaler); err != nil {
return err
} else {
log.Printf("===> Scaler update configuration: %s", json)
}
if _, err := meta.(*Client).mrscaler.Update(context.Background(), input); err != nil {
return fmt.Errorf("[ERROR] Failed to update group [%v]: %v", scalerId, err)
}
return nil
}
func resourceSpotinstMRScalerAWSDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
id := resourceData.Id()
log.Printf(string(commons.ResourceOnDelete),
commons.MRScalerAWSResource.GetName(), id)
if err := deleteScaler(resourceData, meta); err != nil {
return diag.FromErr(err)
}
log.Printf("===> MRScaler deleted successfully: %s <===", resourceData.Id())
resourceData.SetId("")
return nil
}
func deleteScaler(resourceData *schema.ResourceData, meta interface{}) error {
scalerId := resourceData.Id()
input := &mrscaler.DeleteScalerInput{
ScalerID: spotinst.String(scalerId),
}
if json, err := commons.ToJson(input); err != nil {
return err
} else {
log.Printf("===> Scaler delete configuration: %s", json)
}
if _, err := meta.(*Client).mrscaler.Delete(context.Background(), input); err != nil {
return fmt.Errorf("[ERROR] onDelete() -> Failed to delete scaler: %s", err)
}
return nil
}
func exposeMrScalerClusterId(resourceData *schema.ResourceData, meta interface{}) error {
spotinstClient := meta.(*Client)
input := &mrscaler.ScalerClusterStatusInput{ScalerID: spotinst.String(resourceData.Id())}
resp, err := spotinstClient.mrscaler.ReadScalerCluster(context.Background(), input)
if err != nil {
return fmt.Errorf("failed reading cloned cluster id of mr scaler : %s", err)
}
if resp.ScalerClusterId != nil {
if err = resourceData.Set(string(mrscaler_aws.OutputClusterID), resp.ScalerClusterId); err != nil {
return err
}
}
return nil
}