-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
223 lines (178 loc) · 7.1 KB
/
resource.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
package env
import (
"context"
"fmt"
"time"
"github.com/hashicorp/terraform-plugin-framework/types"
common "github.com/altinity/terraform-provider-altinitycloud/internal/provider/env/common"
"github.com/altinity/terraform-provider-altinitycloud/internal/sdk"
"github.com/altinity/terraform-provider-altinitycloud/internal/sdk/auth"
"github.com/altinity/terraform-provider-altinitycloud/internal/sdk/client"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var _ resource.Resource = &AWSEnvResource{}
var _ resource.ResourceWithImportState = &AWSEnvResource{}
func NewAWSEnvResource() resource.Resource {
return &AWSEnvResource{}
}
type AWSEnvResource struct {
client *client.Client
auth *auth.Auth
}
func (r *AWSEnvResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_env_aws"
}
func (r *AWSEnvResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
sdk, ok := req.ProviderData.(*sdk.AltinityCloudSDK)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = sdk.Client
r.auth = sdk.Auth
}
func (r *AWSEnvResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *AWSEnvResourceModel
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
envName := data.Name.ValueString()
tflog.Trace(ctx, "creating resource", map[string]interface{}{"name": envName})
sdkEnv, _ := data.toSDK()
apiResp, err := r.client.CreateAWSEnv(ctx, sdkEnv)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create env %s, got error: %s", envName, err))
return
}
data.Id = data.Name
data.Zones = common.ListToModel(apiResp.CreateAWSEnv.Spec.Zones)
data.NodeGroups = nodeGroupsToModel(apiResp.CreateAWSEnv.Spec.NodeGroups)
data.SpecRevision = types.Int64Value(apiResp.CreateAWSEnv.SpecRevision)
tflog.Trace(ctx, "created resource", map[string]interface{}{"name": envName})
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *AWSEnvResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *AWSEnvResourceModel
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
envName := data.Name.ValueString()
tflog.Trace(ctx, "getting environment", map[string]interface{}{"name": envName})
apiResp, err := r.client.GetAWSEnv(ctx, envName)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read env %s, got error: %s", envName, err))
return
}
if apiResp.AwsEnv == nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Environment %s was not found", envName))
return
}
data.toModel(*apiResp.AwsEnv)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *AWSEnvResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *AWSEnvResourceModel
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
envName := data.Name.ValueString()
tflog.Trace(ctx, "updating resource", map[string]interface{}{"name": envName})
_, sdkEnv := data.toSDK()
apiResp, err := r.client.UpdateAWSEnv(ctx, sdkEnv)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update env %s, got error: %s", envName, err))
return
}
tflog.Trace(ctx, "updated resource", map[string]interface{}{"name": envName})
data.Zones = common.ListToModel(apiResp.UpdateAWSEnv.Spec.Zones)
data.NodeGroups = nodeGroupsToModel(apiResp.UpdateAWSEnv.Spec.NodeGroups)
data.SpecRevision = types.Int64Value(apiResp.UpdateAWSEnv.SpecRevision)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *AWSEnvResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *AWSEnvResourceModel
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
envName := data.Name.ValueString()
if !data.ForceDestroy.ValueBool() {
resp.Diagnostics.AddError("Env Locked", fmt.Sprintf("env %s is protected for deletion, set `force_destroy` property to `true` and run `terraform apply` to unlock it", envName))
return
}
_, err := r.client.DeleteAWSEnv(ctx, client.DeleteAWSEnvInput{
Name: envName,
Force: data.SkipDeprovisionOnDestroy.ValueBoolPointer(),
ForceDestroyClusters: data.ForceDestroyClusters.ValueBoolPointer(),
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete env %s, got error: %s", envName, err))
return
}
_, err = r.client.GetAWSEnv(ctx, envName)
if err != nil {
notFound, err := client.IsNotFoundError(err)
if notFound {
tflog.Trace(ctx, "deleted resource", map[string]interface{}{"name": envName})
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read env %s, got error: %s", envName, err))
}
return
}
// Polling to wait for deletion to complete
timeout := time.After(time.Duration(30) * time.Minute)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
resp.Diagnostics.AddError("Context Cancelled", "The context was cancelled, stopping env deletion.")
return
case <-timeout:
resp.Diagnostics.AddError("Timeout", "Timeout reached while waiting for env to be deleted.")
return
case <-ticker.C:
tflog.Trace(ctx, "checking if env was deleted", map[string]interface{}{"name": envName})
_, err := r.client.GetAWSEnv(ctx, envName)
if err != nil {
notFound, err := client.IsNotFoundError(err)
if notFound {
tflog.Trace(ctx, "deleted resource", map[string]interface{}{"name": envName})
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read env %s, got error: %s", envName, err))
}
return
}
}
}
}
func (r *AWSEnvResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func (r AWSEnvResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
if req.Plan.Raw.IsNull() {
var skipDeprovision types.Bool
req.State.GetAttribute(ctx, path.Root("skip_deprovision_on_destroy"), &skipDeprovision)
if skipDeprovision.ValueBool() {
resp.Diagnostics.AddAttributeWarning(path.Root("skip_deprovision_on_destroy"), "Skip Deprovision on Destroy", "This resource is using the 'skip_deprovision_on_destroy'.\nUse this with precaution as it will delete the environment without deleting any of your cloud resources.")
}
}
}