-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsnapmirror_policy.go
288 lines (257 loc) · 14.8 KB
/
snapmirror_policy.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
package interfaces
import (
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/mitchellh/mapstructure"
"github.com/netapp/terraform-provider-netapp-ontap/internal/restclient"
"github.com/netapp/terraform-provider-netapp-ontap/internal/utils"
)
// SnapmirrorPolicyGetDataModelONTAP defines the resource get data model
type SnapmirrorPolicyGetDataModelONTAP struct {
Name string `mapstructure:"name"`
SVM svm `mapstructure:"svm"`
Type string `mapstructure:"type,omitempty"`
SyncType string `mapstructure:"sync_type,omitempty"`
Comment string `mapstructure:"comment"`
TransferSchedule TransferScheduleType `mapstructure:"transfer_schedule"`
NetworkCompressionEnabled bool `mapstructure:"network_compression_enabled"`
Retention []RetentionGetDataModel `mapstructure:"retention,omitempty"`
IdentityPreservation string `mapstructure:"identity_preservation,omitempty"`
CopyAllSourceSnapshots bool `mapstructure:"copy_all_source_snapshots,omitempty"`
CopyLatestSourceSnapshot bool `mapstructure:"copy_latest_source_snapshot,omitempty"`
UUID string `mapstructure:"uuid"`
}
// SnapmirrorPolicyGetRawDataModelONTAP defines the resource get data model
type SnapmirrorPolicyGetRawDataModelONTAP struct {
Name string `mapstructure:"name"`
SVM svm `mapstructure:"svm"`
Type string `mapstructure:"type,omitempty"`
SyncType string `mapstructure:"sync_type,omitempty"`
Comment string `mapstructure:"comment"`
TransferSchedule TransferScheduleType `mapstructure:"transfer_schedule"`
NetworkCompressionEnabled bool `mapstructure:"network_compression_enabled"`
Retention []RetentionGetRawDataModel `mapstructure:"retention"`
IdentityPreservation string `mapstructure:"identity_preservation,omitempty"`
CopyAllSourceSnapshots bool `mapstructure:"copy_all_source_snapshots,omitempty"`
CopyLatestSourceSnapshot bool `mapstructure:"copy_latest_source_snapshot,omitempty"`
CreateSnapshotOnSource bool `mapstructure:"create_snapshot_on_source,omitempty"`
UUID string `mapstructure:"uuid"`
}
// RetentionGetRawDataModel defines the resource get retention model
type RetentionGetRawDataModel struct {
CreationSchedule CreationScheduleModel `mapstructure:"creation_schedule,omitempty"`
Count string `mapstructure:"count"`
Label string `mapstructure:"label"`
Prefix string `mapstructure:"prefix,omitempty"`
}
// RetentionGetDataModel defines the resource get retention model
type RetentionGetDataModel struct {
CreationSchedule CreationScheduleModel `mapstructure:"creation_schedule,omitempty"`
Count int64 `mapstructure:"count"`
Label string `mapstructure:"label"`
Prefix string `mapstructure:"prefix,omitempty"`
}
// CreationScheduleModel defines the resource creationschedule model
type CreationScheduleModel struct {
Name string `mapstructure:"name"`
}
// SnapmirrorPolicyResourceBodyDataModelONTAP defines the resource data model
type SnapmirrorPolicyResourceBodyDataModelONTAP struct {
Name string `mapstructure:"name"`
SVM svm `mapstructure:"svm"`
Type string `mapstructure:"type,omitempty"`
SyncType string `mapstructure:"sync_type,omitempty"`
Comment string `mapstructure:"comment"`
TransferSchedule TransferScheduleType `mapstructure:"transfer_schedule,omitempty"`
NetworkCompressionEnabled bool `mapstructure:"network_compression_enabled,omitempty"`
Retention []map[string]interface{} `mapstructure:"retention,omitempty"`
IdentityPreservation string `mapstructure:"identity_preservation,omitempty"`
CopyAllSourceSnapshots bool `mapstructure:"copy_all_source_snapshots,omitempty"`
CopyLatestSourceSnapshot bool `mapstructure:"copy_latest_source_snapshot,omitempty"`
CreateSnapshotOnSource bool `mapstructure:"create_snapshot_on_source,omitempty"`
}
// TransferScheduleType describes the transfer_schedule
type TransferScheduleType struct {
Name string `mapstructure:"name,omitempty"`
}
// UpdateSnapmirrorPolicyResourceBodyDataModelONTAP defines the resource update request body
type UpdateSnapmirrorPolicyResourceBodyDataModelONTAP struct {
Comment string `mapstructure:"comment"`
TransferSchedule map[string]interface{} `mapstructure:"transfer_schedule"`
NetworkCompressionEnabled bool `mapstructure:"network_compression_enabled"`
Retention []map[string]interface{} `mapstructure:"retention,omitempty"`
IdentityPreservation string `mapstructure:"identity_preservation,omitempty"`
}
// UpdateSyncSnapmirrorPolicyResourceBodyDataModelONTAP defins the sync type snapmirror policy update request body
type UpdateSyncSnapmirrorPolicyResourceBodyDataModelONTAP struct {
Comment string `mapstructure:"comment"`
NetworkCompressionEnabled bool `mapstructure:"network_compression_enabled"`
Retention []map[string]interface{} `mapstructure:"retention,omitempty"`
}
// UpdateTransferScheduleType describes the transfer_schedule data type in update request
type UpdateTransferScheduleType struct {
Name string `mapstructure:"name"`
UUID string `mapstructure:"uuid,omitempty"`
}
// SnapmirrorPolicyFilterModel describes filter model
type SnapmirrorPolicyFilterModel struct {
Name string `mapstructure:"name"`
}
// GetSnapmirrorPolicy by ID
func GetSnapmirrorPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, id string) (*SnapmirrorPolicyGetRawDataModelONTAP, error) {
api := "snapmirror/policies/" + id
statusCode, response, err := r.GetNilOrOneRecord(api, nil, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
}
if err != nil {
return nil, errorHandler.MakeAndReportError("error reading snapmirror policy info", fmt.Sprintf("error on GET %s: %s", api, err))
}
var rawDataONTAP SnapmirrorPolicyGetRawDataModelONTAP
if err := mapstructure.Decode(response, &rawDataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding snapmirror policy info", fmt.Sprintf("error on decode %s: %s, statusCode %d, response %#v", api, err, statusCode, response))
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("\n777Read snapmirror policy source - udata: %#v", rawDataONTAP))
return &rawDataONTAP, nil
}
// GetSnapmirrorPolicyByName to get snapmirror policy info
func GetSnapmirrorPolicyByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, name string, svmName string) (*SnapmirrorPolicyGetRawDataModelONTAP, error) {
api := "snapmirror/policies"
query := r.NewQuery()
query.Set("name", name)
if svmName == "" {
query.Set("scope", "cluster")
} else {
query.Set("svm.name", svmName)
query.Set("scope", "svm")
}
// TODO: copy_all_source_snapshots is 9.10 and up
query.Fields(([]string{"name", "svm.name", "type", "sync_type", "comment", "transfer_schedule", "network_compression_enabled", "retention", "identity_preservation", "copy_all_source_snapshots", "uuid"}))
statusCode, response, err := r.GetNilOrOneRecord(api, query, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
}
if err != nil {
return nil, errorHandler.MakeAndReportError("error reading snapmirror/policies info", fmt.Sprintf("error on GET %s: %s, statusCode %d", api, err, statusCode))
}
var dataONTAP SnapmirrorPolicyGetRawDataModelONTAP
if err := mapstructure.Decode(response, &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError(fmt.Sprintf("failed to decode response from GET %s", api),
fmt.Sprintf("error: %s, statusCode %d, response %#v", err, statusCode, response))
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read snapmirror/policies data source: %#v", dataONTAP))
// If retention is [] we need to convert it to nil for Terrafrom to work correct
return &dataONTAP, nil
}
// GetSnapmirrorPolicyDataSourceByName to get snapmirror policy data source info by name
func GetSnapmirrorPolicyDataSourceByName(errorHandler *utils.ErrorHandler, r restclient.RestClient, name string, version versionModelONTAP) (*SnapmirrorPolicyGetRawDataModelONTAP, error) {
api := "snapmirror/policies"
query := r.NewQuery()
query.Set("name", name)
fields := []string{"name", "svm.name", "type", "comment", "transfer_schedule", "network_compression_enabled",
"retention", "identity_preservation", "uuid", "create_snapshot_on_source", "transfer_schedule.name", "sync_type"}
if version.Generation == 9 && version.Major > 9 {
fields = append(fields, "copy_all_source_snapshots")
}
if version.Generation == 9 && version.Major > 10 {
fields = append(fields, "create_snapshot_on_source", "copy_latest_source_snapshot")
}
query.Fields(fields)
statusCode, response, err := r.GetNilOrOneRecord(api, query, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
}
if err != nil {
return nil, errorHandler.MakeAndReportError("error reading snapmirror/policies info", fmt.Sprintf("error on GET %s: %s, statusCode %d", api, err, statusCode))
}
var dataONTAP SnapmirrorPolicyGetRawDataModelONTAP
if err := mapstructure.Decode(response, &dataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError(fmt.Sprintf("failed to decode response from GET %s", api),
fmt.Sprintf("error: %s, statusCode %d, response %#v", err, statusCode, response))
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read snapmirror/policies data source: %#v", dataONTAP))
return &dataONTAP, nil
}
// GetSnapmirrorPolicies to get list of policies
func GetSnapmirrorPolicies(errorHandler *utils.ErrorHandler, r restclient.RestClient, filter *SnapmirrorPolicyFilterModel, version versionModelONTAP) ([]SnapmirrorPolicyGetRawDataModelONTAP, error) {
api := "snapmirror/policies"
query := r.NewQuery()
fields := []string{"name", "svm.name", "type", "comment", "transfer_schedule", "network_compression_enabled",
"retention", "identity_preservation", "uuid", "create_snapshot_on_source", "transfer_schedule.name", "sync_type"}
if version.Generation == 9 && version.Major > 9 {
fields = append(fields, "copy_all_source_snapshots")
}
if version.Generation == 9 && version.Major > 10 {
fields = append(fields, "create_snapshot_on_source", "copy_latest_source_snapshot")
}
query.Fields(fields)
if filter != nil {
var filterMap map[string]interface{}
if err := mapstructure.Decode(filter, &filterMap); err != nil {
return nil, errorHandler.MakeAndReportError("error encoding snapmirror/policies filter info", fmt.Sprintf("error on filter %#v: %s", filter, err))
}
query.SetValues(filterMap)
}
statusCode, response, err := r.GetZeroOrMoreRecords(api, query, nil)
if err == nil && response == nil {
err = fmt.Errorf("no response for GET %s", api)
}
if err != nil {
return nil, errorHandler.MakeAndReportError("error reading snapmirror/policies info", fmt.Sprintf("error on GET %s: %s, statusCode %d", api, err, statusCode))
}
var dataONTAP []SnapmirrorPolicyGetRawDataModelONTAP
for _, info := range response {
var record SnapmirrorPolicyGetRawDataModelONTAP
if err := mapstructure.Decode(info, &record); err != nil {
return nil, errorHandler.MakeAndReportError(fmt.Sprintf("failed to decode response from GET %s", api),
fmt.Sprintf("error: %s, statusCode %d, info %#v", err, statusCode, info))
}
dataONTAP = append(dataONTAP, record)
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Read protcols_nfs_service data source: %#v", dataONTAP))
return dataONTAP, nil
}
// CreateSnapmirrorPolicy to create snapmirror policy
func CreateSnapmirrorPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, body SnapmirrorPolicyResourceBodyDataModelONTAP) (*SnapmirrorPolicyGetRawDataModelONTAP, error) {
api := "snapmirror/policies"
var bodyMap map[string]interface{}
if err := mapstructure.Decode(body, &bodyMap); err != nil {
return nil, errorHandler.MakeAndReportError("error encoding snapmirror/policies body", fmt.Sprintf("error on encoding %s body: %s, body: %#v", api, err, body))
}
query := r.NewQuery()
query.Add("return_records", "true")
statusCode, response, err := r.CallCreateMethod(api, query, bodyMap)
if err != nil {
return nil, errorHandler.MakeAndReportError("error creating snapmirror/policies", fmt.Sprintf("error on POST %s: %s, statusCode %d", api, err, statusCode))
}
var rawDataONTAP SnapmirrorPolicyGetRawDataModelONTAP
if err := mapstructure.Decode(response.Records[0], &rawDataONTAP); err != nil {
return nil, errorHandler.MakeAndReportError("error decoding snapmirror/policies info", fmt.Sprintf("error on decode snapmirror/policies info: %s, statusCode %d, response %#v", err, statusCode, response))
}
tflog.Debug(errorHandler.Ctx, fmt.Sprintf("Create snapmirror/policies source - udata: %#v", rawDataONTAP))
return &rawDataONTAP, nil
}
// UpdateSnapmirrorPolicy to update snapmirror policy
func UpdateSnapmirrorPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, data any, id string) error {
api := "snapmirror/policies/" + id
var body map[string]interface{}
if err := mapstructure.Decode(data, &body); err != nil {
return errorHandler.MakeAndReportError("error encoding update snapmirror/policies body", fmt.Sprintf("error on encoding %s body: %s, body: %#v", api, err, body))
}
query := r.NewQuery()
query.Add("return_records", "true")
statusCode, response, err := r.CallUpdateMethod(api, query, body)
if err != nil {
return errorHandler.MakeAndReportError("error updating export policy", fmt.Sprintf("error on PATCH %s: %s, statusCode %d, response %#v", api, err, statusCode, response))
}
return nil
}
// DeleteSnapmirrorPolicy to delete ip_interface
func DeleteSnapmirrorPolicy(errorHandler *utils.ErrorHandler, r restclient.RestClient, uuid string) error {
api := "snapmirror/policies/"
statusCode, _, err := r.CallDeleteMethod(api+uuid, nil, nil)
if err != nil {
return errorHandler.MakeAndReportError("error deleting snapmirror/policies", fmt.Sprintf("error on DELETE %s: %s, statusCode %d", api, err, statusCode))
}
return nil
}