@@ -6,10 +6,10 @@ import (
66 "fmt"
77 "strings"
88
9- "github.com/spf13/cast"
10-
9+ "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1110 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1211 "github.com/mwielbut/pointy"
12+ "github.com/spf13/cast"
1313 matlas "go.mongodb.org/atlas/mongodbatlas"
1414)
1515
@@ -28,12 +28,12 @@ const (
2828// same as resourceMongoDBAtlasCloudProviderSnapshotBackupPolicy
2929func resourceMongoDBAtlasCloudBackupSchedule () * schema.Resource {
3030 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 ,
3535 Importer : & schema.ResourceImporter {
36- State : resourceMongoDBAtlasCloudBackupScheduleImportState ,
36+ StateContext : resourceMongoDBAtlasCloudBackupScheduleImportState ,
3737 },
3838
3939 Schema : map [string ]* schema.Schema {
@@ -214,25 +214,25 @@ func resourceMongoDBAtlasCloudBackupSchedule() *schema.Resource {
214214 }
215215}
216216
217- func resourceMongoDBAtlasCloudBackupScheduleCreate (d * schema.ResourceData , meta interface {}) error {
217+ func resourceMongoDBAtlasCloudBackupScheduleCreate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
218218 conn := meta .(* MongoDBClient ).Atlas
219219 projectID := d .Get ("project_id" ).(string )
220220 clusterName := d .Get ("cluster_name" ).(string )
221221
222- err := cloudBackupScheduleCreateOrUpdate (conn , d , projectID , clusterName )
222+ err := cloudBackupScheduleCreateOrUpdate (ctx , conn , d , projectID , clusterName )
223223 if err != nil {
224- return fmt .Errorf (errorSnapshotBackupScheduleCreate , err )
224+ return diag .Errorf (errorSnapshotBackupScheduleCreate , err )
225225 }
226226
227227 d .SetId (encodeStateID (map [string ]string {
228228 "project_id" : projectID ,
229229 "cluster_name" : clusterName ,
230230 }))
231231
232- return resourceMongoDBAtlasCloudBackupScheduleRead (d , meta )
232+ return resourceMongoDBAtlasCloudBackupScheduleRead (ctx , d , meta )
233233}
234234
235- func resourceMongoDBAtlasCloudBackupScheduleRead (d * schema.ResourceData , meta interface {}) error {
235+ func resourceMongoDBAtlasCloudBackupScheduleRead (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
236236 // Get client connection.
237237 conn := meta .(* MongoDBClient ).Atlas
238238
@@ -242,57 +242,57 @@ func resourceMongoDBAtlasCloudBackupScheduleRead(d *schema.ResourceData, meta in
242242
243243 backupPolicy , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (context .Background (), projectID , clusterName )
244244 if err != nil {
245- return fmt .Errorf (errorSnapshotBackupScheduleRead , clusterName , err )
245+ return diag .Errorf (errorSnapshotBackupScheduleRead , clusterName , err )
246246 }
247247
248248 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 )
250250 }
251251
252252 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 )
254254 }
255255
256256 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 )
258258 }
259259
260260 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 )
262262 }
263263
264264 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 )
266266 }
267267
268268 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 )
270270 }
271271
272272 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 )
274274 }
275275
276276 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 )
278278 }
279279
280280 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 )
282282 }
283283
284284 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 )
286286 }
287287
288288 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 )
290290 }
291291
292292 return nil
293293}
294294
295- func resourceMongoDBAtlasCloudBackupScheduleUpdate (d * schema.ResourceData , meta interface {}) error {
295+ func resourceMongoDBAtlasCloudBackupScheduleUpdate (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
296296 conn := meta .(* MongoDBClient ).Atlas
297297
298298 ids := decodeStateID (d .Id ())
@@ -301,34 +301,34 @@ func resourceMongoDBAtlasCloudBackupScheduleUpdate(d *schema.ResourceData, meta
301301
302302 if restoreWindowDays , ok := d .GetOk ("restore_window_days" ); ok {
303303 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" )
305305 }
306306 }
307307
308- err := cloudBackupScheduleCreateOrUpdate (conn , d , projectID , clusterName )
308+ err := cloudBackupScheduleCreateOrUpdate (ctx , conn , d , projectID , clusterName )
309309 if err != nil {
310- return fmt .Errorf (errorSnapshotBackupScheduleUpdate , err )
310+ return diag .Errorf (errorSnapshotBackupScheduleUpdate , err )
311311 }
312312
313- return resourceMongoDBAtlasCloudBackupScheduleRead (d , meta )
313+ return resourceMongoDBAtlasCloudBackupScheduleRead (ctx , d , meta )
314314}
315315
316- func resourceMongoDBAtlasCloudBackupScheduleDelete (d * schema.ResourceData , meta interface {}) error {
316+ func resourceMongoDBAtlasCloudBackupScheduleDelete (ctx context. Context , d * schema.ResourceData , meta interface {}) diag. Diagnostics {
317317 // Get client connection.
318318 conn := meta .(* MongoDBClient ).Atlas
319319 ids := decodeStateID (d .Id ())
320320 projectID := ids ["project_id" ]
321321 clusterName := ids ["cluster_name" ]
322322
323- _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (context . Background () , projectID , clusterName )
323+ _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName )
324324 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 )
326326 }
327327
328328 return nil
329329}
330330
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 ) {
332332 conn := meta .(* MongoDBClient ).Atlas
333333
334334 parts := strings .SplitN (d .Id (), "-" , 2 )
@@ -339,7 +339,7 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(d *schema.ResourceData,
339339 projectID := parts [0 ]
340340 clusterName := parts [1 ]
341341
342- _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (context . Background () , projectID , clusterName )
342+ _ , _ , err := conn .CloudProviderSnapshotBackupPolicies .Get (ctx , projectID , clusterName )
343343 if err != nil {
344344 return nil , fmt .Errorf (errorSnapshotBackupScheduleRead , clusterName , err )
345345 }
@@ -360,11 +360,11 @@ func resourceMongoDBAtlasCloudBackupScheduleImportState(d *schema.ResourceData,
360360 return []* schema.ResourceData {d }, nil
361361}
362362
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 {
364364 req := & matlas.CloudProviderSnapshotBackupPolicy {}
365365
366366 // Delete policies items
367- resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (context . Background () , projectID , clusterName )
367+ resp , _ , err := conn .CloudProviderSnapshotBackupPolicies .Delete (ctx , projectID , clusterName )
368368 if err != nil {
369369 return fmt .Errorf ("error deleting MongoDB Cloud Backup Schedule (%s): %s" , clusterName , err )
370370 }
0 commit comments