/
svm_resource.go
377 lines (332 loc) · 12 KB
/
svm_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
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
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/mapstructure"
"github.com/netapp/terraform-provider-netapp-ontap/internal/interfaces"
"github.com/netapp/terraform-provider-netapp-ontap/internal/utils"
)
var _ resource.Resource = &SvmResource{}
var _ resource.ResourceWithImportState = &SvmResource{}
// NewSvmResource is a helper function to simplify the provider implementation.
func NewSvmResource() resource.Resource {
return &SvmResource{
config: resourceOrDataSourceConfig{
name: "svm_resource",
},
}
}
// SvmResource defines the resource implementation.
type SvmResource struct {
config resourceOrDataSourceConfig
}
// SvmResourceModel describes the resource data model.
type SvmResourceModel struct {
CxProfileName types.String `tfsdk:"cx_profile_name"`
Name types.String `tfsdk:"name"`
Ipspace types.String `tfsdk:"ipspace"`
SnapshotPolicy types.String `tfsdk:"snapshot_policy"`
SubType types.String `tfsdk:"subtype"`
Comment types.String `tfsdk:"comment"`
Language types.String `tfsdk:"language"`
Aggregates []types.String `tfsdk:"aggregates"`
MaxVolumes types.String `tfsdk:"max_volumes"`
ID types.String `tfsdk:"id"`
}
// Metadata returns the resource type name.
func (r *SvmResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + r.config.name
}
// Schema defines the schema for the resource.
func (r *SvmResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Svm resource",
Attributes: map[string]schema.Attribute{
"cx_profile_name": schema.StringAttribute{
MarkdownDescription: "Connection profile name",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the svm to manage",
Required: true,
},
"ipspace": schema.StringAttribute{
MarkdownDescription: "The name of the ipspace to manage",
Optional: true,
},
"snapshot_policy": schema.StringAttribute{
MarkdownDescription: "The name of the snapshot policy to manage",
Optional: true,
},
"subtype": schema.StringAttribute{
MarkdownDescription: "The subtype for svm to be created",
Optional: true,
},
"comment": schema.StringAttribute{
MarkdownDescription: "Comment for svm to be created",
Optional: true,
},
"language": schema.StringAttribute{
MarkdownDescription: "Language to use for svm",
Optional: true,
},
"aggregates": schema.SetAttribute{
ElementType: types.StringType,
MarkdownDescription: "Aggregates to be assigned use for svm",
Optional: true,
},
"max_volumes": schema.StringAttribute{
MarkdownDescription: "Maximum number of volumes that can be created on the svm. Expects an integer or unlimited",
Optional: true,
},
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "SVM identifier",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
}
}
// Configure adds the provider configured client to the resource.
func (r *SvmResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
config, ok := req.ProviderData.(Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
}
r.config.providerConfig = config
}
// Create the resource and sets the initial Terraform state.
func (r *SvmResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *SvmResourceModel
errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var request interfaces.SvmResourceModel
request.Name = data.Name.ValueString()
if !data.Ipspace.IsNull() {
request.Ipspace.Name = data.Ipspace.ValueString()
}
if !data.SnapshotPolicy.IsNull() {
request.SnapshotPolicy.Name = data.SnapshotPolicy.ValueString()
}
if !data.SubType.IsNull() {
request.SubType = data.SubType.ValueString()
}
setCommentEmpty := false
if !data.Comment.IsNull() {
request.Comment = data.Comment.ValueString()
} else {
setCommentEmpty = true
}
if !data.Language.IsNull() {
request.Language = data.Language.ValueString()
}
if !data.MaxVolumes.IsNull() {
err := interfaces.ValidateIntORString(errorHandler, data.MaxVolumes.ValueString(), "unlimited")
if err != nil {
return
}
request.MaxVolumes = data.MaxVolumes.ValueString()
}
setAggrEmpty := false
if len(data.Aggregates) != 0 {
aggregates := []interfaces.Aggregate{}
for _, v := range data.Aggregates {
aggr := interfaces.Aggregate{}
aggr.Name = v.ValueString()
aggregates = append(aggregates, aggr)
}
err := mapstructure.Decode(aggregates, &request.Aggregates)
if err != nil {
errorHandler.MakeAndReportError("error creating svm", fmt.Sprintf("error on encoding aggregates info: %s, aggregates %#v", err, aggregates))
return
}
} else {
setAggrEmpty = true
}
errorHandler = utils.NewErrorHandler(ctx, &resp.Diagnostics)
client, err := getRestClient(errorHandler, r.config, data.CxProfileName)
if err != nil {
// error reporting done inside NewClient
return
}
svm, err := interfaces.CreateSvm(errorHandler, *client, request, setAggrEmpty, setCommentEmpty)
if err != nil {
return
}
// data.UUID = types.StringValue(svm.UUID)
data.ID = types.StringValue(svm.UUID)
tflog.Trace(ctx, "created a resource")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
// Read refreshes the Terraform state with the latest data.
func (r *SvmResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *SvmResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)
client, err := getRestClient(errorHandler, r.config, data.CxProfileName)
if err != nil {
// error reporting done inside NewClient
return
}
tflog.Debug(ctx, fmt.Sprintf("read a svm resource: %#v", data))
svm, err := interfaces.GetSvm(errorHandler, *client, data.ID.ValueString())
if err != nil {
return
}
if svm == nil {
errorHandler.MakeAndReportError("No Svm found", "No SVM found")
}
data.Name = types.StringValue(svm.Name)
data.ID = types.StringValue(svm.UUID)
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
// Update updates the resource and sets the updated Terraform state on success.
func (r *SvmResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data *SvmResourceModel
var state *SvmResourceModel
setCommentEmpty := false
setAggrEmpty := false
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
// Read state file data
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
if resp.Diagnostics.HasError() {
return
}
errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)
client, err := getRestClient(errorHandler, r.config, data.CxProfileName)
if err != nil {
return
}
var request interfaces.SvmResourceModel
if !data.Name.Equal(state.Name) {
if data.Name.ValueString() == "" {
errorHandler.MakeAndReportError("update name", "name cannot be updated with empty string")
return
}
request.Name = data.Name.ValueString()
}
// TODO: Ipspace can not be modify on SVM/patch. We can't fail or maybe a warning should be sent?
//if !data.Ipspace.IsNull() {
// request.Ipspace.Name = data.Ipspace.ValueString()
//}
if !data.SnapshotPolicy.Equal(state.SnapshotPolicy) {
if data.SnapshotPolicy.ValueString() == "" {
// snapshot policy cannot be modifoed as empty name but API does not fail with empty snapshot policy
errorHandler.MakeAndReportError("update snapshot_policy", "snapshot_policy cannot be updated with empty string")
return
}
request.SnapshotPolicy.Name = data.SnapshotPolicy.ValueString()
}
if !data.SubType.Equal(state.SubType) {
errorHandler.MakeAndReportError("update subtype", "subtype cannot be modified")
return
}
// comment can be modified as empty string
if !data.Comment.Equal(state.Comment) {
if data.Comment.ValueString() == "" {
setCommentEmpty = true
}
request.Comment = data.Comment.ValueString()
}
if !data.Language.Equal(state.Language) {
if data.Language.ValueString() == "" {
errorHandler.MakeAndReportError("update language", "language cannot be updated with empty string")
return
}
request.Language = data.Language.ValueString()
}
if !data.MaxVolumes.Equal(state.MaxVolumes) {
if data.MaxVolumes.ValueString() == "" {
errorHandler.MakeAndReportError("update max_volumes", "max_volumes cannot be updated with empty string")
return
}
err := interfaces.ValidateIntORString(errorHandler, data.MaxVolumes.ValueString(), "unlimited")
if err != nil {
return
}
request.MaxVolumes = data.MaxVolumes.ValueString()
}
// aggregates can be modified as empty list
aggregates := []interfaces.Aggregate{}
if len(data.Aggregates) != 0 {
for _, v := range data.Aggregates {
aggr := interfaces.Aggregate{}
aggr.Name = v.ValueString()
aggregates = append(aggregates, aggr)
}
} else {
if len(state.Aggregates) != 0 {
setAggrEmpty = true
}
}
err = mapstructure.Decode(aggregates, &request.Aggregates)
if err != nil {
errorHandler.MakeAndReportError("error creating svm", fmt.Sprintf("error on encoding aggregates info: %s, aggregates %#v", err, aggregates))
return
}
tflog.Debug(ctx, fmt.Sprintf("update a svm resource: %#v", data))
err = interfaces.UpdateSvm(errorHandler, *client, request, state.ID.ValueString(), setAggrEmpty, setCommentEmpty)
if err != nil {
return
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
}
// Delete deletes the resource and removes the Terraform state on success.
func (r *SvmResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *SvmResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)
if data.ID.IsNull() {
errorHandler.MakeAndReportError("ID is null", "svm UUID is null")
return
}
client, err := getRestClient(errorHandler, r.config, data.CxProfileName)
if err != nil {
// error reporting done inside NewClient
return
}
err = interfaces.DeleteSvm(errorHandler, *client, data.ID.ValueString())
if err != nil {
return
}
}
// ImportState imports a resource using ID from terraform import command by calling the Read method.
func (r *SvmResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}