@@ -6,10 +6,10 @@ import (
6
6
"fmt"
7
7
"strings"
8
8
9
- "github.com/spf13/cast"
10
-
9
+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11
10
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12
11
"github.com/mwielbut/pointy"
12
+ "github.com/spf13/cast"
13
13
matlas "go.mongodb.org/atlas/mongodbatlas"
14
14
)
15
15
@@ -28,12 +28,12 @@ const (
28
28
// same as resourceMongoDBAtlasCloudProviderSnapshotBackupPolicy
29
29
func resourceMongoDBAtlasCloudBackupSchedule () * schema.Resource {
30
30
return & schema.Resource {
31
- Create : resourceMongoDBAtlasCloudBackupScheduleCreate ,
32
- Read : resourceMongoDBAtlasCloudBackupScheduleRead ,
33
- Update : resourceMongoDBAtlasCloudBackupScheduleUpdate ,
34
- Delete : resourceMongoDBAtlasCloudBackupScheduleDelete ,
31
+ CreateContext : resourceMongoDBAtlasCloudBackupScheduleCreate ,
32
+ ReadContext : resourceMongoDBAtlasCloudBackupScheduleRead ,
33
+ UpdateContext : resourceMongoDBAtlasCloudBackupScheduleUpdate ,
34
+ DeleteContext : resourceMongoDBAtlasCloudBackupScheduleDelete ,
35
35
Importer : & schema.ResourceImporter {
36
- State : resourceMongoDBAtlasCloudBackupScheduleImportState ,
36
+ StateContext : resourceMongoDBAtlasCloudBackupScheduleImportState ,
37
37
},
38
38
39
39
Schema : map [string ]* schema.Schema {
@@ -214,25 +214,25 @@ func resourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
214
214
}
215
215
}
216
216
217
- func resourceMongoDBAtlasCloudBackupScheduleCreate (d * schema.ResourceData , meta interface {}) error {
217
+ func resourceMongoDBAtlasCloudBackupScheduleCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
218
218
conn := meta .(* MongoDBClient ).Atlas
219
219
projectID := d .Get ("project_id" ).(string )
220
220
clusterName := d .Get ("cluster_name" ).(string )
221
221
222
- err := cloudBackupScheduleCreateOrUpdate (conn , d , projectID , clusterName )
222
+ err := cloudBackupScheduleCreateOrUpdate (ctx , conn , d , projectID , clusterName )
223
223
if err != nil {
224
- return fmt .Errorf (errorSnapshotBackupScheduleCreate , err )
224
+ return diag .Errorf (errorSnapshotBackupScheduleCreate , err )
225
225
}
226
226
227
227
d .SetId (encodeStateID (map [string ]string {
228
228
"project_id" : projectID ,
229
229
"cluster_name" : clusterName ,
230
230
}))
231
231
232
- return resourceMongoDBAtlasCloudBackupScheduleRead (d , meta )
232
+ return resourceMongoDBAtlasCloudBackupScheduleRead (ctx , d , meta )
233
233
}
234
234
235
- func resourceMongoDBAtlasCloudBackupScheduleRead (d * schema.ResourceData , meta interface {}) error {
235
+ func resourceMongoDBAtlasCloudBackupScheduleRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
236
236
// Get client connection.
237
237
conn := meta .(* MongoDBClient ).Atlas
238
238
@@ -242,57 +242,57 @@ func resourceMongoDBAtlasCloudBackupScheduleRead(d *schema.ResourceData, meta in
242
242
243
243
backupPolicy , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (context .Background (), projectID , clusterName )
244
244
if err != nil {
245
- return fmt .Errorf (errorSnapshotBackupScheduleRead , clusterName , err )
245
+ return diag .Errorf (errorSnapshotBackupScheduleRead , clusterName , err )
246
246
}
247
247
248
248
if err := d .Set ("cluster_id" , backupPolicy .ClusterID ); err != nil {
249
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "cluster_id" , clusterName , err )
249
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "cluster_id" , clusterName , err )
250
250
}
251
251
252
252
if err := d .Set ("reference_hour_of_day" , backupPolicy .ReferenceHourOfDay ); err != nil {
253
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "reference_hour_of_day" , clusterName , err )
253
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "reference_hour_of_day" , clusterName , err )
254
254
}
255
255
256
256
if err := d .Set ("reference_minute_of_hour" , backupPolicy .ReferenceMinuteOfHour ); err != nil {
257
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "reference_minute_of_hour" , clusterName , err )
257
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "reference_minute_of_hour" , clusterName , err )
258
258
}
259
259
260
260
if err := d .Set ("restore_window_days" , backupPolicy .RestoreWindowDays ); err != nil {
261
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "restore_window_days" , clusterName , err )
261
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "restore_window_days" , clusterName , err )
262
262
}
263
263
264
264
if err := d .Set ("update_snapshots" , backupPolicy .UpdateSnapshots ); err != nil {
265
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "update_snapshots" , clusterName , err )
265
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "update_snapshots" , clusterName , err )
266
266
}
267
267
268
268
if err := d .Set ("next_snapshot" , backupPolicy .NextSnapshot ); err != nil {
269
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "next_snapshot" , clusterName , err )
269
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "next_snapshot" , clusterName , err )
270
270
}
271
271
272
272
if err := d .Set ("id_policy" , backupPolicy .Policies [0 ].ID ); err != nil {
273
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "id_policy" , clusterName , err )
273
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "id_policy" , clusterName , err )
274
274
}
275
275
276
276
if err := d .Set ("policy_item_hourly" , flattenPolicyItem (backupPolicy .Policies [0 ].PolicyItems , snapshotScheduleHourly )); err != nil {
277
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_hourly" , clusterName , err )
277
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_hourly" , clusterName , err )
278
278
}
279
279
280
280
if err := d .Set ("policy_item_daily" , flattenPolicyItem (backupPolicy .Policies [0 ].PolicyItems , snapshotScheduleDaily )); err != nil {
281
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_daily" , clusterName , err )
281
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_daily" , clusterName , err )
282
282
}
283
283
284
284
if err := d .Set ("policy_item_weekly" , flattenPolicyItem (backupPolicy .Policies [0 ].PolicyItems , snapshotScheduleWeekly )); err != nil {
285
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_weekly" , clusterName , err )
285
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_weekly" , clusterName , err )
286
286
}
287
287
288
288
if err := d .Set ("policy_item_monthly" , flattenPolicyItem (backupPolicy .Policies [0 ].PolicyItems , snapshotScheduleMonthly )); err != nil {
289
- return fmt .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_monthly" , clusterName , err )
289
+ return diag .Errorf (errorSnapshotBackupScheduleSetting , "policy_item_monthly" , clusterName , err )
290
290
}
291
291
292
292
return nil
293
293
}
294
294
295
- func resourceMongoDBAtlasCloudBackupScheduleUpdate (d * schema.ResourceData , meta interface {}) error {
295
+ func resourceMongoDBAtlasCloudBackupScheduleUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
296
296
conn := meta .(* MongoDBClient ).Atlas
297
297
298
298
ids := decodeStateID (d .Id ())
@@ -301,34 +301,34 @@ func resourceMongoDBAtlasCloudBackupScheduleUpdate(d *schema.ResourceData, meta
301
301
302
302
if restoreWindowDays , ok := d .GetOk ("restore_window_days" ); ok {
303
303
if cast .ToInt64 (restoreWindowDays ) <= 0 {
304
- return fmt .Errorf ("`restore_window_days` cannot be <= 0" )
304
+ return diag .Errorf ("`restore_window_days` cannot be <= 0" )
305
305
}
306
306
}
307
307
308
- err := cloudBackupScheduleCreateOrUpdate (conn , d , projectID , clusterName )
308
+ err := cloudBackupScheduleCreateOrUpdate (ctx , conn , d , projectID , clusterName )
309
309
if err != nil {
310
- return fmt .Errorf (errorSnapshotBackupScheduleUpdate , err )
310
+ return diag .Errorf (errorSnapshotBackupScheduleUpdate , err )
311
311
}
312
312
313
- return resourceMongoDBAtlasCloudBackupScheduleRead (d , meta )
313
+ return resourceMongoDBAtlasCloudBackupScheduleRead (ctx , d , meta )
314
314
}
315
315
316
- func resourceMongoDBAtlasCloudBackupScheduleDelete (d * schema.ResourceData , meta interface {}) error {
316
+ func resourceMongoDBAtlasCloudBackupScheduleDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
317
317
// Get client connection.
318
318
conn := meta .(* MongoDBClient ).Atlas
319
319
ids := decodeStateID (d .Id ())
320
320
projectID := ids ["project_id" ]
321
321
clusterName := ids ["cluster_name" ]
322
322
323
- _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (context . Background () , projectID , clusterName )
323
+ _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName )
324
324
if err != nil {
325
- return fmt .Errorf ("error deleting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
325
+ return diag .Errorf ("error deleting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
326
326
}
327
327
328
328
return nil
329
329
}
330
330
331
- func resourceMongoDBAtlasCloudBackupScheduleImportState (d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
331
+ func resourceMongoDBAtlasCloudBackupScheduleImportState (ctx context. Context , d * schema.ResourceData , meta interface {}) ([]* schema.ResourceData , error ) {
332
332
conn := meta .(* MongoDBClient ).Atlas
333
333
334
334
parts := strings .SplitN (d .Id (), "-" , 2 )
@@ -339,7 +339,7 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(d *schema.ResourceData,
339
339
projectID := parts [0 ]
340
340
clusterName := parts [1 ]
341
341
342
- _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (context . Background () , projectID , clusterName )
342
+ _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (ctx , projectID , clusterName )
343
343
if err != nil {
344
344
return nil , fmt .Errorf (errorSnapshotBackupScheduleRead , clusterName , err )
345
345
}
@@ -360,11 +360,11 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(d *schema.ResourceData,
360
360
return []* schema.ResourceData {d }, nil
361
361
}
362
362
363
- func cloudBackupScheduleCreateOrUpdate (conn * matlas.Client , d * schema.ResourceData , projectID , clusterName string ) error {
363
+ func cloudBackupScheduleCreateOrUpdate (ctx context. Context , conn * matlas.Client , d * schema.ResourceData , projectID , clusterName string ) error {
364
364
req := & matlas.CloudProviderSnapshotBackupPolicy {}
365
365
366
366
// Delete policies items
367
- resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (context . Background () , projectID , clusterName )
367
+ resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName )
368
368
if err != nil {
369
369
return fmt .Errorf ("error deleting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
370
370
}
0 commit comments