-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
224 lines (178 loc) · 7.14 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
224
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 = &AzureEnvResource{}
var _ resource.ResourceWithImportState = &AzureEnvResource{}
func NewAzureEnvResource() resource.Resource {
return &AzureEnvResource{}
}
type AzureEnvResource struct {
client *client.Client
auth *auth.Auth
}
func (r *AzureEnvResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_env_azure"
}
func (r *AzureEnvResource) 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 *AzureEnvResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *AzureEnvResourceModel
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
name := data.Name.ValueString()
tflog.Trace(ctx, "creating resource", map[string]interface{}{"name": name})
sdkEnv, _ := data.toSDK()
apiResp, err := r.client.CreateAzureEnv(ctx, sdkEnv)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create env %s, got error: %s", name, err))
return
}
tflog.Trace(ctx, "created resource", map[string]interface{}{"name": name})
data.Id = data.Name
data.Zones = common.ListToModel(apiResp.CreateAzureEnv.Spec.Zones)
data.NodeGroups = nodeGroupsToModel(apiResp.CreateAzureEnv.Spec.NodeGroups)
data.SpecRevision = types.Int64Value(apiResp.CreateAzureEnv.SpecRevision)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *AzureEnvResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *AzureEnvResourceModel
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.GetAzureEnv(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.AzureEnv == nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Environment %s was not found", envName))
return
}
data.toModel(*apiResp.AzureEnv)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *AzureEnvResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *AzureEnvResourceModel
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
name := data.Name.ValueString()
tflog.Trace(ctx, "updating resource", map[string]interface{}{"name": name})
_, sdkEnv := data.toSDK()
apiResp, err := r.client.UpdateAzureEnv(ctx, sdkEnv)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update env %s, got error: %s", name, err))
return
}
tflog.Trace(ctx, "updated resource", map[string]interface{}{"name": name})
data.Zones = common.ListToModel(apiResp.UpdateAzureEnv.Spec.Zones)
data.NodeGroups = nodeGroupsToModel(apiResp.UpdateAzureEnv.Spec.NodeGroups)
data.SpecRevision = types.Int64Value(apiResp.UpdateAzureEnv.SpecRevision)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r *AzureEnvResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *AzureEnvResourceModel
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.DeleteAzureEnv(ctx, client.DeleteAzureEnvInput{
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.GetAzureEnv(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.GetAzureEnv(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 *AzureEnvResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("name"), req, resp)
}
func (r AzureEnvResource) 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.")
}
}
}