/
storage_volume_data_source.go
383 lines (358 loc) · 14.9 KB
/
storage_volume_data_source.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
378
379
380
381
382
383
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/netapp/terraform-provider-netapp-ontap/internal/interfaces"
"github.com/netapp/terraform-provider-netapp-ontap/internal/utils"
)
// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &StorageVolumeDataSource{}
// NewStorageVolumeDataSource is a helper function to simplify the provider implementation.
func NewStorageVolumeDataSource() datasource.DataSource {
return &StorageVolumeDataSource{
config: resourceOrDataSourceConfig{
name: "storage_volume_data_source",
},
}
}
// StorageVolumeDataSource defines the data source implementation.
type StorageVolumeDataSource struct {
config resourceOrDataSourceConfig
}
// StorageVolumeDataSourceModel describes the data source data model.
type StorageVolumeDataSourceModel struct {
CxProfileName types.String `tfsdk:"cx_profile_name"`
Name types.String `tfsdk:"name"`
SVMName types.String `tfsdk:"svm_name"`
State types.String `tfsdk:"state"`
Type types.String `tfsdk:"type"`
SpaceGuarantee types.String `tfsdk:"space_guarantee"`
Encrypt types.Bool `tfsdk:"encryption"`
SnapshotPolicy types.String `tfsdk:"snapshot_policy"`
Language types.String `tfsdk:"language"`
QOSPolicyGroup types.String `tfsdk:"qos_policy_group"`
Comment types.String `tfsdk:"comment"`
Aggregates []StorageVolumeDataSourceAggregates `tfsdk:"aggregates"`
ID types.String `tfsdk:"id"`
Space *StorageVolumeDataSourceSpace `tfsdk:"space"`
Nas *StorageVolumeDataSourceNas `tfsdk:"nas"`
Tiering *StorageVolumeDataSourceTiering `tfsdk:"tiering"`
Efficiency *StorageVolumeDataSourceEfficiency `tfsdk:"efficiency"`
SnapLock *StorageVolumeDataSourceSnapLock `tfsdk:"snaplock"`
Analytics *StorageVolumeDataSourceAnalytics `tfsdk:"analytics"`
}
// StorageVolumeDataSourceAggregates describes the analytics model.
type StorageVolumeDataSourceAggregates struct {
Name types.String `tfsdk:"name"`
}
// StorageVolumeDataSourceAnalytics describes the analytics model.
type StorageVolumeDataSourceAnalytics struct {
State types.String `tfsdk:"state"`
}
// StorageVolumeDataSourceSnapLock describes the snaplock model.
type StorageVolumeDataSourceSnapLock struct {
SnaplockType types.String `tfsdk:"type"`
}
// StorageVolumeDataSourceEfficiency describes the efficiency model.
type StorageVolumeDataSourceEfficiency struct {
Policy types.String `tfsdk:"policy_name"`
Compression types.String `tfsdk:"compression"`
}
// StorageVolumeDataSourceTiering describes the tiering model.
type StorageVolumeDataSourceTiering struct {
Policy types.String `tfsdk:"policy_name"`
MinimumCoolingDays types.Int64 `tfsdk:"minimum_cooling_days"`
}
// StorageVolumeDataSourceNas describes the Nas model.
type StorageVolumeDataSourceNas struct {
ExportPolicy types.String `tfsdk:"export_policy_name"`
JunctionPath types.String `tfsdk:"junction_path"`
GroupID types.Int64 `tfsdk:"group_id"`
UserID types.Int64 `tfsdk:"user_id"`
SecurityStyle types.String `tfsdk:"security_style"`
UnixPermissions types.Int64 `tfsdk:"unix_permissions"`
}
// StorageVolumeDataSourceSpace describes the space model.
type StorageVolumeDataSourceSpace struct {
Size types.Int64 `tfsdk:"size"`
SizeUnit types.String `tfsdk:"size_unit"`
PercentSnapshotSpace types.Int64 `tfsdk:"percent_snapshot_space"`
LogicalSpace *StorageVolumeDataSourceSpaceLogicalSpace `tfsdk:"logical_space"`
}
// StorageVolumeDataSourceSpaceLogicalSpace describes the logical space model within sapce model.
type StorageVolumeDataSourceSpaceLogicalSpace struct {
Enforcement types.Bool `tfsdk:"enforcement"`
Reporting types.Bool `tfsdk:"reporting"`
}
// Metadata returns the data source type name.
func (d *StorageVolumeDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_" + d.config.name
}
// Schema defines the schema for the data source.
func (d *StorageVolumeDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Storage Volume data source",
Attributes: map[string]schema.Attribute{
"cx_profile_name": schema.StringAttribute{
MarkdownDescription: "Connection profile name",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "The name of the volume to manage",
Required: true,
},
"svm_name": schema.StringAttribute{
MarkdownDescription: "Name of the svm to use",
Required: true,
},
"aggregates": schema.ListNestedAttribute{
Computed: true,
MarkdownDescription: "List of aggregates that the volume is on",
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Name of the aggregate",
Computed: true,
},
},
},
},
"state": schema.StringAttribute{
MarkdownDescription: "Whether the specified volume is online, or not",
Computed: true,
},
"type": schema.StringAttribute{
MarkdownDescription: "The volume type, either read-write (RW) or data-protection (DP)",
Computed: true,
},
"space_guarantee": schema.StringAttribute{
MarkdownDescription: "Space guarantee style for the volume",
Computed: true,
},
"encryption": schema.BoolAttribute{
MarkdownDescription: "Whether or not to enable Volume Encryption",
Computed: true,
},
"snapshot_policy": schema.StringAttribute{
MarkdownDescription: "The name of the snapshot policy",
Computed: true,
},
"language": schema.StringAttribute{
MarkdownDescription: "Language to use for volume",
Computed: true,
},
// with Rest API qos_policy_group and qos_adaptive_policy_group are now the same thing and cannot be set at the same time
"qos_policy_group": schema.StringAttribute{
MarkdownDescription: "Specifies a QoS policy group to be set on volume",
Computed: true,
},
"comment": schema.StringAttribute{
MarkdownDescription: "Sets a comment associated with the volume",
Computed: true,
},
"space": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"size": schema.Int64Attribute{
MarkdownDescription: "The size of the volume",
Computed: true,
},
"size_unit": schema.StringAttribute{
MarkdownDescription: "The unit used to interpret the size parameter",
Computed: true,
},
"percent_snapshot_space": schema.Int64Attribute{
MarkdownDescription: "Amount of space reserved for snapshot copies of the volume",
Computed: true,
},
"logical_space": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"enforcement": schema.BoolAttribute{
MarkdownDescription: "Whether to perform logical space accounting on the volume",
Computed: true,
},
"reporting": schema.BoolAttribute{
MarkdownDescription: "Whether to report space logically",
Computed: true,
},
},
},
},
},
"nas": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"export_policy_name": schema.StringAttribute{
MarkdownDescription: "The name of the export policy",
Computed: true,
},
"junction_path": schema.StringAttribute{
MarkdownDescription: "Junction path of the volume",
Computed: true,
},
"group_id": schema.Int64Attribute{
MarkdownDescription: "The UNIX group ID for the volume",
Computed: true,
},
"user_id": schema.Int64Attribute{
MarkdownDescription: "The UNIX user ID for the volume",
Computed: true,
},
"security_style": schema.StringAttribute{
MarkdownDescription: "The security style associated to the volume",
Computed: true,
},
"unix_permissions": schema.Int64Attribute{
MarkdownDescription: "Unix permission bits in octal or symbolic format. For example, 0 is equivalent to ------------, 777 is equivalent to ---rwxrwxrwx,both formats are accepted",
Computed: true,
},
},
},
"tiering": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"policy_name": schema.StringAttribute{
MarkdownDescription: "The tiering policy that is to be associated with the volume",
Computed: true,
},
"minimum_cooling_days": schema.Int64Attribute{
MarkdownDescription: "Determines how many days must pass before inactive data in a volume using the Auto or Snapshot-Only policy is considered cold and eligible for tiering",
Computed: true,
},
},
},
"efficiency": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"policy_name": schema.StringAttribute{
MarkdownDescription: "Allows a storage efficiency policy to be set on volume creation",
Computed: true,
},
"compression": schema.StringAttribute{
MarkdownDescription: "Whether to enable compression for the volume (HDD and Flash Pool aggregates)",
Computed: true,
},
},
},
"snaplock": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"type": schema.StringAttribute{
MarkdownDescription: "The SnapLock type of the volume",
Computed: true,
},
},
},
"analytics": schema.SingleNestedAttribute{
Computed: true,
Attributes: map[string]schema.Attribute{
"state": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Set file system analytics state of the volume",
},
},
},
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Volume identifier",
},
},
}
}
// Read refreshes the Terraform state with the latest data.
func (d *StorageVolumeDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data StorageVolumeDataSourceModel
// Read Terraform configuration data into the model
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
errorHandler := utils.NewErrorHandler(ctx, &resp.Diagnostics)
// we need to defer setting the client until we can read the connection profile name
client, err := getRestClient(errorHandler, d.config, data.CxProfileName)
if err != nil {
// error reporting done inside NewClient
return
}
volume, err := interfaces.GetStorageVolumeByName(errorHandler, *client, data.Name.ValueString(), data.SVMName.ValueString())
if err != nil {
return
}
if volume == nil {
errorHandler.MakeAndReportError("No volume found", fmt.Sprintf("Volume %s not found.", data.Name))
return
}
data.Name = types.StringValue(volume.Name)
data.SVMName = types.StringValue(volume.SVM.Name)
var aggregates = make([]StorageVolumeDataSourceAggregates, len(volume.Aggregates))
for i, v := range volume.Aggregates {
aggregates[i].Name = types.StringValue(v.Name)
}
data.Aggregates = aggregates
data.State = types.StringValue(volume.State)
data.Type = types.StringValue(volume.Type)
data.SpaceGuarantee = types.StringValue(volume.SpaceGuarantee.Type)
data.Encrypt = types.BoolValue(volume.Encryption.Enabled)
data.SnapshotPolicy = types.StringValue(volume.SnapshotPolicy.Name)
data.Language = types.StringValue(volume.Language)
data.QOSPolicyGroup = types.StringValue(volume.QOS.Policy.Name)
data.Comment = types.StringValue(volume.Comment)
vsize, vunits := interfaces.ByteFormat(int64(volume.Space.Size))
data.Space = &StorageVolumeDataSourceSpace{
Size: types.Int64Value(vsize),
SizeUnit: types.StringValue(vunits),
PercentSnapshotSpace: types.Int64Value(int64(volume.Space.Snapshot.ReservePercent)),
LogicalSpace: &StorageVolumeDataSourceSpaceLogicalSpace{
Enforcement: types.BoolValue(volume.Space.LogicalSpace.Enforcement),
Reporting: types.BoolValue(volume.Space.LogicalSpace.Reporting),
},
}
data.Nas = &StorageVolumeDataSourceNas{
ExportPolicy: types.StringValue(volume.NAS.ExportPolicy.Name),
JunctionPath: types.StringValue(volume.NAS.JunctionPath),
GroupID: types.Int64Value(int64(volume.NAS.GroupID)),
UserID: types.Int64Value(int64(volume.NAS.UserID)),
SecurityStyle: types.StringValue(volume.NAS.SecurityStyle),
UnixPermissions: types.Int64Value(int64(volume.NAS.UnixPermissions)),
}
data.Tiering = &StorageVolumeDataSourceTiering{
Policy: types.StringValue(volume.TieringPolicy.Policy),
MinimumCoolingDays: types.Int64Value(int64(volume.TieringPolicy.MinCoolingDays)),
}
data.Efficiency = &StorageVolumeDataSourceEfficiency{
Policy: types.StringValue(volume.Efficiency.Policy.Name),
Compression: types.StringValue(volume.Efficiency.Compression),
}
data.SnapLock = &StorageVolumeDataSourceSnapLock{
SnaplockType: types.StringValue(volume.Snaplock.Type),
}
data.Analytics = &StorageVolumeDataSourceAnalytics{
State: types.StringValue(volume.Analytics.State),
}
data.ID = types.StringValue(volume.UUID)
// Write logs using the tflog package
// Documentation: https://terraform.io/plugin/log
tflog.Debug(ctx, fmt.Sprintf("read a data source: %#v", data))
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
// Configure adds the provider configured client to the data source.
func (d *StorageVolumeDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
config, ok := req.ProviderData.(Config)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
}
d.config.providerConfig = config
}