/
snapmirrors_data_source.go
278 lines (257 loc) · 9.39 KB
/
snapmirrors_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
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 = &SnapmirrorsDataSource{}
// NewSnapmirrorsDataSource is a helper function to simplify the provider implementation.
func NewSnapmirrorsDataSource() datasource.DataSource {
return &SnapmirrorsDataSource{
config: resourceOrDataSourceConfig{
name: "snapmirrors_data_source",
},
}
}
// SnapmirrorsDataSource defines the data source implementation.
type SnapmirrorsDataSource struct {
config resourceOrDataSourceConfig
}
// SnapmirrorDataSourceFilterModel describes the data source model.
type SnapmirrorDataSourceFilterModel struct {
DestinantionPath types.String `tfsdk:"destination_path"`
}
// SnapmirrorsDataSourceModel describes the data source data model.
type SnapmirrorsDataSourceModel struct {
CxProfileName types.String `tfsdk:"cx_profile_name"`
Snapmirrors []SnapmirrorDataSourceModel `tfsdk:"snapmirrors"`
Filter *SnapmirrorDataSourceFilterModel `tfsdk:"filter"`
}
// Metadata returns the data source type name.
func (d *SnapmirrorsDataSource) 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 *SnapmirrorsDataSource) 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: "Snapmirrors data source",
Attributes: map[string]schema.Attribute{
"cx_profile_name": schema.StringAttribute{
MarkdownDescription: "Connection profile name",
Required: true,
},
"filter": schema.SingleNestedAttribute{
Attributes: map[string]schema.Attribute{
"destination_path": schema.StringAttribute{
MarkdownDescription: "Destination path",
Optional: true,
},
},
Optional: true,
},
"snapmirrors": schema.ListNestedAttribute{
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"cx_profile_name": schema.StringAttribute{
MarkdownDescription: "Connection profile name",
Computed: true,
},
"source": schema.SingleNestedAttribute{
MarkdownDescription: "Snapmirror source endpoint",
Computed: true,
Attributes: map[string]schema.Attribute{
"cluster": schema.SingleNestedAttribute{
MarkdownDescription: "Cluster details",
Computed: true,
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "cluster name",
Computed: true,
},
"uuid": schema.StringAttribute{
MarkdownDescription: "cluster UUID",
Computed: true,
},
},
},
"path": schema.StringAttribute{
MarkdownDescription: "Path to the source endpoint of the SnapMirror relationship",
Computed: true,
},
"svm": schema.SingleNestedAttribute{
MarkdownDescription: "Cluster details",
Computed: true,
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "svm name",
Computed: true,
},
"uuid": schema.StringAttribute{
MarkdownDescription: "svm UUID",
Computed: true,
},
},
},
},
},
"destination": schema.SingleNestedAttribute{
MarkdownDescription: "Snapmirror destination endpoint",
Computed: true,
Attributes: map[string]schema.Attribute{
"path": schema.StringAttribute{
MarkdownDescription: "Path to the destination endpoint of the SnapMirror relationship",
Computed: true,
},
"svm": schema.SingleNestedAttribute{
MarkdownDescription: "Cluster details",
Computed: true,
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "cluster name",
Computed: true,
},
"uuid": schema.StringAttribute{
MarkdownDescription: "cluster UUID",
Computed: true,
},
},
},
},
},
"healthy": schema.BoolAttribute{
MarkdownDescription: "healthy of the relationship",
Computed: true,
},
"restore": schema.BoolAttribute{
MarkdownDescription: "restore of the relationship",
Computed: true,
},
"id": schema.StringAttribute{
MarkdownDescription: "uuid of the relationship",
Computed: true,
},
"state": schema.StringAttribute{
MarkdownDescription: "state of the relationship",
Computed: true,
},
"policy": schema.SingleNestedAttribute{
MarkdownDescription: "policy of the relationship",
Computed: true,
Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
MarkdownDescription: "Policy UUID",
Computed: true,
},
},
},
"group_type": schema.StringAttribute{
MarkdownDescription: "group_type of the relationship",
Computed: true,
},
"throttle": schema.Int64Attribute{
MarkdownDescription: "throttle of the relationship",
Computed: true,
},
},
},
Computed: true,
MarkdownDescription: "",
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *SnapmirrorsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.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 Data Source Configure Type",
fmt.Sprintf("Expected Config, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
}
d.config.providerConfig = config
}
// Read refreshes the Terraform state with the latest data.
func (d *SnapmirrorsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data SnapmirrorsDataSourceModel
// 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
}
cluster, err := interfaces.GetCluster(errorHandler, *client)
if err != nil {
// error reporting done inside GetCluster
return
}
if cluster == nil {
errorHandler.MakeAndReportError("No cluster found", fmt.Sprintf("cluster not found"))
return
}
var filter *interfaces.SnapmirrorFilterModel = nil
if data.Filter != nil {
filter = &interfaces.SnapmirrorFilterModel{
DestinationPath: data.Filter.DestinantionPath.ValueString(),
}
}
restInfo, err := interfaces.GetSnapmirrors(errorHandler, *client, filter, cluster.Version)
if err != nil {
// error reporting done inside GetSnapmirrors
return
}
data.Snapmirrors = make([]SnapmirrorDataSourceModel, len(restInfo))
for index, record := range restInfo {
data.Snapmirrors[index] = SnapmirrorDataSourceModel{
CxProfileName: types.String(data.CxProfileName),
Source: &Source{
Cluster: &SnapmirrorCluster{
Name: types.StringValue(record.Source.Cluster.Name),
UUID: types.StringValue(record.Source.Cluster.UUID),
},
Path: types.StringValue(record.Source.Path),
Svm: &Svm{
Name: types.StringValue(record.Source.Svm.Name),
UUID: types.StringValue(record.Source.Svm.UUID),
},
},
Destination: &Destination{
Path: types.StringValue(record.Destination.Path),
Svm: &Svm{
Name: types.StringValue(record.Destination.Svm.Name),
UUID: types.StringValue(record.Destination.Svm.UUID),
},
},
Healthy: types.BoolValue(record.Healthy),
Restore: types.BoolValue(record.Restore),
ID: types.StringValue(record.UUID),
State: types.StringValue(record.State),
}
if cluster.Version.Generation == 9 && cluster.Version.Major > 10 {
data.Snapmirrors[index].Throttle = types.Int64Value(int64(record.Throttle))
data.Snapmirrors[index].GroupType = types.StringValue(record.GroupType)
}
}
// 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)...)
}