@@ -24,7 +24,7 @@ import (
24
24
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
25
25
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
26
26
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/project"
27
- matlas "go.mongodb.org/atlas/mongodbatlas "
27
+ "go.mongodb.org/atlas-sdk/v20231115002/admin "
28
28
)
29
29
30
30
const (
@@ -205,7 +205,7 @@ func (r *encryptionAtRestRS) Schema(ctx context.Context, req resource.SchemaRequ
205
205
func (r * encryptionAtRestRS ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
206
206
var encryptionAtRestPlan * tfEncryptionAtRestRSModel
207
207
var encryptionAtRestConfig * tfEncryptionAtRestRSModel
208
- conn := r .Client .Atlas
208
+ connV2 := r .Client .AtlasV2
209
209
210
210
resp .Diagnostics .Append (req .Plan .Get (ctx , & encryptionAtRestPlan )... )
211
211
resp .Diagnostics .Append (req .Config .Get (ctx , & encryptionAtRestConfig )... )
@@ -214,23 +214,21 @@ func (r *encryptionAtRestRS) Create(ctx context.Context, req resource.CreateRequ
214
214
}
215
215
216
216
projectID := encryptionAtRestPlan .ProjectID .ValueString ()
217
- encryptionAtRestReq := & matlas.EncryptionAtRest {
218
- GroupID : projectID ,
219
- }
217
+ encryptionAtRestReq := & admin.EncryptionAtRest {}
220
218
if encryptionAtRestPlan .AwsKmsConfig != nil {
221
- encryptionAtRestReq .AwsKms = * newAtlasAwsKms (encryptionAtRestPlan .AwsKmsConfig )
219
+ encryptionAtRestReq .AwsKms = newAtlasAwsKms (encryptionAtRestPlan .AwsKmsConfig )
222
220
}
223
221
if encryptionAtRestPlan .AzureKeyVaultConfig != nil {
224
- encryptionAtRestReq .AzureKeyVault = * newAtlasAzureKeyVault (encryptionAtRestPlan .AzureKeyVaultConfig )
222
+ encryptionAtRestReq .AzureKeyVault = newAtlasAzureKeyVault (encryptionAtRestPlan .AzureKeyVaultConfig )
225
223
}
226
224
if encryptionAtRestPlan .GoogleCloudKmsConfig != nil {
227
- encryptionAtRestReq .GoogleCloudKms = * newAtlasGcpKms (encryptionAtRestPlan .GoogleCloudKmsConfig )
225
+ encryptionAtRestReq .GoogleCloudKms = newAtlasGcpKms (encryptionAtRestPlan .GoogleCloudKmsConfig )
228
226
}
229
227
230
228
stateConf := & retry.StateChangeConf {
231
229
Pending : []string {retrystrategy .RetryStrategyPendingState },
232
230
Target : []string {retrystrategy .RetryStrategyCompletedState , retrystrategy .RetryStrategyErrorState },
233
- Refresh : resourceMongoDBAtlasEncryptionAtRestCreateRefreshFunc (ctx , projectID , conn , encryptionAtRestReq ),
231
+ Refresh : resourceMongoDBAtlasEncryptionAtRestCreateRefreshFunc (ctx , projectID , connV2 , encryptionAtRestReq ),
234
232
Timeout : 1 * time .Minute ,
235
233
MinTimeout : 1 * time .Second ,
236
234
Delay : 0 ,
@@ -243,7 +241,7 @@ func (r *encryptionAtRestRS) Create(ctx context.Context, req resource.CreateRequ
243
241
return
244
242
}
245
243
246
- encryptionAtRestPlanNew := newTFEncryptionAtRestRSModel (ctx , projectID , encryptionResp .(* matlas .EncryptionAtRest ), encryptionAtRestPlan )
244
+ encryptionAtRestPlanNew := newTFEncryptionAtRestRSModel (ctx , projectID , encryptionResp .(* admin .EncryptionAtRest ), encryptionAtRestPlan )
247
245
resetDefaultsFromConfigOrState (ctx , encryptionAtRestPlan , encryptionAtRestPlanNew , encryptionAtRestConfig )
248
246
249
247
// set state to fully populated data
@@ -254,17 +252,16 @@ func (r *encryptionAtRestRS) Create(ctx context.Context, req resource.CreateRequ
254
252
}
255
253
}
256
254
257
- func resourceMongoDBAtlasEncryptionAtRestCreateRefreshFunc (ctx context.Context , projectID string , conn * matlas. Client , encryptionAtRestReq * matlas .EncryptionAtRest ) retry.StateRefreshFunc {
255
+ func resourceMongoDBAtlasEncryptionAtRestCreateRefreshFunc (ctx context.Context , projectID string , connV2 * admin. APIClient , encryptionAtRestReq * admin .EncryptionAtRest ) retry.StateRefreshFunc {
258
256
return func () (any , string , error ) {
259
- encryptionResp , _ , err := conn . EncryptionsAtRest . Create (ctx , encryptionAtRestReq )
257
+ encryptionResp , _ , err := connV2 . EncryptionAtRestUsingCustomerKeyManagementApi . UpdateEncryptionAtRest (ctx , projectID , encryptionAtRestReq ). Execute ( )
260
258
if err != nil {
261
259
if errors .Is (err , errors .New ("CANNOT_ASSUME_ROLE" )) ||
262
260
errors .Is (err , errors .New ("INVALID_AWS_CREDENTIALS" )) ||
263
261
errors .Is (err , errors .New ("CLOUD_PROVIDER_ACCESS_ROLE_NOT_AUTHORIZED" )) {
264
262
log .Printf ("warning issue performing authorize EncryptionsAtRest not done try again: %s \n " , err .Error ())
265
263
log .Println ("retrying " )
266
264
267
- encryptionAtRestReq .GroupID = projectID
268
265
return encryptionResp , retrystrategy .RetryStrategyPendingState , nil
269
266
}
270
267
return encryptionResp , retrystrategy .RetryStrategyErrorState , err
@@ -290,9 +287,9 @@ func (r *encryptionAtRestRS) Read(ctx context.Context, req resource.ReadRequest,
290
287
isImport = true
291
288
}
292
289
293
- conn := r .Client .Atlas
290
+ connV2 := r .Client .AtlasV2
294
291
295
- encryptionResp , _ , err := conn . EncryptionsAtRest . Get (context .Background (), projectID )
292
+ encryptionResp , _ , err := connV2 . EncryptionAtRestUsingCustomerKeyManagementApi . GetEncryptionAtRest (context .Background (), projectID ). Execute ( )
296
293
if err != nil {
297
294
resp .Diagnostics .AddError ("error when getting encryption at rest resource during read" , fmt .Sprintf (errorReadEncryptionAtRest , err .Error ()))
298
295
return
@@ -314,7 +311,7 @@ func (r *encryptionAtRestRS) Update(ctx context.Context, req resource.UpdateRequ
314
311
var encryptionAtRestState * tfEncryptionAtRestRSModel
315
312
var encryptionAtRestConfig * tfEncryptionAtRestRSModel
316
313
var encryptionAtRestPlan * tfEncryptionAtRestRSModel
317
- conn := r .Client .Atlas
314
+ connV2 := r .Client .AtlasV2
318
315
319
316
// get current config
320
317
resp .Diagnostics .Append (req .Config .Get (ctx , & encryptionAtRestConfig )... )
@@ -332,7 +329,7 @@ func (r *encryptionAtRestRS) Update(ctx context.Context, req resource.UpdateRequ
332
329
return
333
330
}
334
331
projectID := encryptionAtRestState .ProjectID .ValueString ()
335
- atlasEncryptionAtRest , atlasResp , err := conn . EncryptionsAtRest . Get (context .Background (), projectID )
332
+ atlasEncryptionAtRest , atlasResp , err := connV2 . EncryptionAtRestUsingCustomerKeyManagementApi . GetEncryptionAtRest (context .Background (), projectID ). Execute ( )
336
333
if err != nil {
337
334
if resp != nil && atlasResp .StatusCode == http .StatusNotFound {
338
335
resp .State .RemoveResource (ctx )
@@ -343,18 +340,16 @@ func (r *encryptionAtRestRS) Update(ctx context.Context, req resource.UpdateRequ
343
340
}
344
341
345
342
if hasAwsKmsConfigChanged (encryptionAtRestPlan .AwsKmsConfig , encryptionAtRestState .AwsKmsConfig ) {
346
- atlasEncryptionAtRest .AwsKms = * newAtlasAwsKms (encryptionAtRestPlan .AwsKmsConfig )
343
+ atlasEncryptionAtRest .AwsKms = newAtlasAwsKms (encryptionAtRestPlan .AwsKmsConfig )
347
344
}
348
345
if hasAzureKeyVaultConfigChanged (encryptionAtRestPlan .AzureKeyVaultConfig , encryptionAtRestState .AzureKeyVaultConfig ) {
349
- atlasEncryptionAtRest .AzureKeyVault = * newAtlasAzureKeyVault (encryptionAtRestPlan .AzureKeyVaultConfig )
346
+ atlasEncryptionAtRest .AzureKeyVault = newAtlasAzureKeyVault (encryptionAtRestPlan .AzureKeyVaultConfig )
350
347
}
351
348
if hasGcpKmsConfigChanged (encryptionAtRestPlan .GoogleCloudKmsConfig , encryptionAtRestState .GoogleCloudKmsConfig ) {
352
- atlasEncryptionAtRest .GoogleCloudKms = * newAtlasGcpKms (encryptionAtRestPlan .GoogleCloudKmsConfig )
349
+ atlasEncryptionAtRest .GoogleCloudKms = newAtlasGcpKms (encryptionAtRestPlan .GoogleCloudKmsConfig )
353
350
}
354
351
355
- atlasEncryptionAtRest .GroupID = projectID
356
-
357
- encryptionResp , _ , err := conn .EncryptionsAtRest .Create (ctx , atlasEncryptionAtRest )
352
+ encryptionResp , _ , err := connV2 .EncryptionAtRestUsingCustomerKeyManagementApi .UpdateEncryptionAtRest (ctx , projectID , atlasEncryptionAtRest ).Execute ()
358
353
if err != nil {
359
354
resp .Diagnostics .AddError ("error updating encryption at rest" , fmt .Sprintf (errorUpdateEncryptionAtRest , err .Error ()))
360
355
return
@@ -376,9 +371,22 @@ func (r *encryptionAtRestRS) Delete(ctx context.Context, req resource.DeleteRequ
376
371
return
377
372
}
378
373
379
- conn := r .Client .Atlas
374
+ enabled := false
375
+ connV2 := r .Client .AtlasV2
380
376
projectID := encryptionAtRestState .ProjectID .ValueString ()
381
- _ , err := conn .EncryptionsAtRest .Delete (ctx , projectID )
377
+
378
+ _ , _ , err := connV2 .EncryptionAtRestUsingCustomerKeyManagementApi .GetEncryptionAtRest (context .Background (), projectID ).Execute ()
379
+ if err != nil {
380
+ resp .Diagnostics .AddError ("error when destroying resource" , fmt .Sprintf (errorDeleteEncryptionAtRest , projectID , err .Error ()))
381
+ return
382
+ }
383
+
384
+ softDelete := admin.EncryptionAtRest {
385
+ AwsKms : & admin.AWSKMSConfiguration {Enabled : & enabled },
386
+ AzureKeyVault : & admin.AzureKeyVault {Enabled : & enabled },
387
+ GoogleCloudKms : & admin.GoogleCloudKMS {Enabled : & enabled },
388
+ }
389
+ _ , _ , err = connV2 .EncryptionAtRestUsingCustomerKeyManagementApi .UpdateEncryptionAtRest (ctx , projectID , & softDelete ).Execute ()
382
390
383
391
if err != nil {
384
392
resp .Diagnostics .AddError ("error when destroying resource" , fmt .Sprintf (errorDeleteEncryptionAtRest , projectID , err .Error ()))
@@ -468,110 +476,110 @@ func handleAzureKeyVaultConfigDefaults(ctx context.Context, earRSCurrent, earRSN
468
476
}
469
477
}
470
478
471
- func newTFEncryptionAtRestRSModel (ctx context.Context , projectID string , encryptionResp * matlas .EncryptionAtRest , plan * tfEncryptionAtRestRSModel ) * tfEncryptionAtRestRSModel {
479
+ func newTFEncryptionAtRestRSModel (ctx context.Context , projectID string , encryptionResp * admin .EncryptionAtRest , plan * tfEncryptionAtRestRSModel ) * tfEncryptionAtRestRSModel {
472
480
return & tfEncryptionAtRestRSModel {
473
481
ID : types .StringValue (projectID ),
474
482
ProjectID : types .StringValue (projectID ),
475
- AwsKmsConfig : newTFAwsKmsConfig (ctx , & encryptionResp .AwsKms , plan .AwsKmsConfig ),
476
- AzureKeyVaultConfig : newTFAzureKeyVaultConfig (ctx , & encryptionResp .AzureKeyVault , plan .AzureKeyVaultConfig ),
477
- GoogleCloudKmsConfig : newTFGcpKmsConfig (ctx , & encryptionResp .GoogleCloudKms , plan .GoogleCloudKmsConfig ),
483
+ AwsKmsConfig : newTFAwsKmsConfig (ctx , encryptionResp .AwsKms , plan .AwsKmsConfig ),
484
+ AzureKeyVaultConfig : newTFAzureKeyVaultConfig (ctx , encryptionResp .AzureKeyVault , plan .AzureKeyVaultConfig ),
485
+ GoogleCloudKmsConfig : newTFGcpKmsConfig (ctx , encryptionResp .GoogleCloudKms , plan .GoogleCloudKmsConfig ),
478
486
}
479
487
}
480
488
481
- func newTFAwsKmsConfig (ctx context.Context , awsKms * matlas. AwsKms , currStateSlice []tfAwsKmsConfigModel ) []tfAwsKmsConfigModel {
489
+ func newTFAwsKmsConfig (ctx context.Context , awsKms * admin. AWSKMSConfiguration , currStateSlice []tfAwsKmsConfigModel ) []tfAwsKmsConfigModel {
482
490
if awsKms == nil {
483
491
return []tfAwsKmsConfigModel {}
484
492
}
485
493
newState := tfAwsKmsConfigModel {}
486
494
487
495
newState .Enabled = types .BoolPointerValue (awsKms .Enabled )
488
- newState .CustomerMasterKeyID = types .StringValue (awsKms .CustomerMasterKeyID )
489
- newState .Region = types .StringValue (awsKms .Region )
490
- newState .AccessKeyID = conversion .StringNullIfEmpty (awsKms .AccessKeyID )
491
- newState .SecretAccessKey = conversion .StringNullIfEmpty (awsKms .SecretAccessKey )
492
- newState .RoleID = conversion .StringNullIfEmpty (awsKms .RoleID )
496
+ newState .CustomerMasterKeyID = types .StringValue (awsKms .GetCustomerMasterKeyID () )
497
+ newState .Region = types .StringValue (awsKms .GetRegion () )
498
+ newState .AccessKeyID = conversion .StringNullIfEmpty (awsKms .GetAccessKeyID () )
499
+ newState .SecretAccessKey = conversion .StringNullIfEmpty (awsKms .GetSecretAccessKey () )
500
+ newState .RoleID = conversion .StringNullIfEmpty (awsKms .GetRoleId () )
493
501
494
502
return []tfAwsKmsConfigModel {newState }
495
503
}
496
504
497
- func newTFAzureKeyVaultConfig (ctx context.Context , az * matlas .AzureKeyVault , currStateSlice []tfAzureKeyVaultConfigModel ) []tfAzureKeyVaultConfigModel {
505
+ func newTFAzureKeyVaultConfig (ctx context.Context , az * admin .AzureKeyVault , currStateSlice []tfAzureKeyVaultConfigModel ) []tfAzureKeyVaultConfigModel {
498
506
if az == nil {
499
507
return []tfAzureKeyVaultConfigModel {}
500
508
}
501
509
newState := tfAzureKeyVaultConfigModel {}
502
510
503
511
newState .Enabled = types .BoolPointerValue (az .Enabled )
504
- newState .ClientID = types .StringValue (az .ClientID )
505
- newState .AzureEnvironment = types .StringValue (az .AzureEnvironment )
506
- newState .SubscriptionID = types .StringValue (az .SubscriptionID )
507
- newState .ResourceGroupName = types .StringValue (az .ResourceGroupName )
508
- newState .KeyVaultName = types .StringValue (az .KeyVaultName )
509
- newState .KeyIdentifier = types .StringValue (az .KeyIdentifier )
510
- newState .TenantID = types .StringValue (az .TenantID )
511
- newState .Secret = conversion .StringNullIfEmpty (az .Secret )
512
+ newState .ClientID = types .StringValue (az .GetClientID () )
513
+ newState .AzureEnvironment = types .StringValue (az .GetAzureEnvironment () )
514
+ newState .SubscriptionID = types .StringValue (az .GetSubscriptionID () )
515
+ newState .ResourceGroupName = types .StringValue (az .GetResourceGroupName () )
516
+ newState .KeyVaultName = types .StringValue (az .GetKeyVaultName () )
517
+ newState .KeyIdentifier = types .StringValue (az .GetKeyIdentifier () )
518
+ newState .TenantID = types .StringValue (az .GetTenantID () )
519
+ newState .Secret = conversion .StringNullIfEmpty (az .GetSecret () )
512
520
513
521
return []tfAzureKeyVaultConfigModel {newState }
514
522
}
515
523
516
- func newTFGcpKmsConfig (ctx context.Context , gcpKms * matlas. GoogleCloudKms , currStateSlice []tfGcpKmsConfigModel ) []tfGcpKmsConfigModel {
524
+ func newTFGcpKmsConfig (ctx context.Context , gcpKms * admin. GoogleCloudKMS , currStateSlice []tfGcpKmsConfigModel ) []tfGcpKmsConfigModel {
517
525
if gcpKms == nil {
518
526
return []tfGcpKmsConfigModel {}
519
527
}
520
528
newState := tfGcpKmsConfigModel {}
521
529
522
530
newState .Enabled = types .BoolPointerValue (gcpKms .Enabled )
523
- newState .KeyVersionResourceID = types .StringValue (gcpKms .KeyVersionResourceID )
524
- newState .ServiceAccountKey = conversion .StringNullIfEmpty (gcpKms .ServiceAccountKey )
531
+ newState .KeyVersionResourceID = types .StringValue (gcpKms .GetKeyVersionResourceID () )
532
+ newState .ServiceAccountKey = conversion .StringNullIfEmpty (gcpKms .GetServiceAccountKey () )
525
533
526
534
return []tfGcpKmsConfigModel {newState }
527
535
}
528
536
529
- func newAtlasAwsKms (tfAwsKmsConfigSlice []tfAwsKmsConfigModel ) * matlas. AwsKms {
537
+ func newAtlasAwsKms (tfAwsKmsConfigSlice []tfAwsKmsConfigModel ) * admin. AWSKMSConfiguration {
530
538
if tfAwsKmsConfigSlice == nil || len (tfAwsKmsConfigSlice ) < 1 {
531
- return & matlas. AwsKms {}
539
+ return & admin. AWSKMSConfiguration {}
532
540
}
533
541
v := tfAwsKmsConfigSlice [0 ]
534
542
535
543
awsRegion , _ := conversion .ValRegion (v .Region .ValueString ())
536
544
537
- return & matlas. AwsKms {
545
+ return & admin. AWSKMSConfiguration {
538
546
Enabled : v .Enabled .ValueBoolPointer (),
539
- AccessKeyID : v .AccessKeyID .ValueString (),
540
- SecretAccessKey : v .SecretAccessKey .ValueString (),
541
- CustomerMasterKeyID : v .CustomerMasterKeyID .ValueString (),
542
- Region : awsRegion ,
543
- RoleID : v .RoleID .ValueString (),
547
+ AccessKeyID : v .AccessKeyID .ValueStringPointer (),
548
+ SecretAccessKey : v .SecretAccessKey .ValueStringPointer (),
549
+ CustomerMasterKeyID : v .CustomerMasterKeyID .ValueStringPointer (),
550
+ Region : conversion . StringPtr ( awsRegion ) ,
551
+ RoleId : v .RoleID .ValueStringPointer (),
544
552
}
545
553
}
546
554
547
- func newAtlasGcpKms (tfGcpKmsConfigSlice []tfGcpKmsConfigModel ) * matlas. GoogleCloudKms {
555
+ func newAtlasGcpKms (tfGcpKmsConfigSlice []tfGcpKmsConfigModel ) * admin. GoogleCloudKMS {
548
556
if tfGcpKmsConfigSlice == nil || len (tfGcpKmsConfigSlice ) < 1 {
549
- return & matlas. GoogleCloudKms {}
557
+ return & admin. GoogleCloudKMS {}
550
558
}
551
559
v := tfGcpKmsConfigSlice [0 ]
552
560
553
- return & matlas. GoogleCloudKms {
561
+ return & admin. GoogleCloudKMS {
554
562
Enabled : v .Enabled .ValueBoolPointer (),
555
- ServiceAccountKey : v .ServiceAccountKey .ValueString (),
556
- KeyVersionResourceID : v .KeyVersionResourceID .ValueString (),
563
+ ServiceAccountKey : v .ServiceAccountKey .ValueStringPointer (),
564
+ KeyVersionResourceID : v .KeyVersionResourceID .ValueStringPointer (),
557
565
}
558
566
}
559
567
560
- func newAtlasAzureKeyVault (tfAzKeyVaultConfigSlice []tfAzureKeyVaultConfigModel ) * matlas .AzureKeyVault {
568
+ func newAtlasAzureKeyVault (tfAzKeyVaultConfigSlice []tfAzureKeyVaultConfigModel ) * admin .AzureKeyVault {
561
569
if tfAzKeyVaultConfigSlice == nil || len (tfAzKeyVaultConfigSlice ) < 1 {
562
- return & matlas .AzureKeyVault {}
570
+ return & admin .AzureKeyVault {}
563
571
}
564
572
v := tfAzKeyVaultConfigSlice [0 ]
565
573
566
- return & matlas .AzureKeyVault {
574
+ return & admin .AzureKeyVault {
567
575
Enabled : v .Enabled .ValueBoolPointer (),
568
- ClientID : v .ClientID .ValueString (),
569
- AzureEnvironment : v .AzureEnvironment .ValueString (),
570
- SubscriptionID : v .SubscriptionID .ValueString (),
571
- ResourceGroupName : v .ResourceGroupName .ValueString (),
572
- KeyVaultName : v .KeyVaultName .ValueString (),
573
- KeyIdentifier : v .KeyIdentifier .ValueString (),
574
- Secret : v .Secret .ValueString (),
575
- TenantID : v .TenantID .ValueString (),
576
+ ClientID : v .ClientID .ValueStringPointer (),
577
+ AzureEnvironment : v .AzureEnvironment .ValueStringPointer (),
578
+ SubscriptionID : v .SubscriptionID .ValueStringPointer (),
579
+ ResourceGroupName : v .ResourceGroupName .ValueStringPointer (),
580
+ KeyVaultName : v .KeyVaultName .ValueStringPointer (),
581
+ KeyIdentifier : v .KeyIdentifier .ValueStringPointer (),
582
+ Secret : v .Secret .ValueStringPointer (),
583
+ TenantID : v .TenantID .ValueStringPointer (),
576
584
}
577
585
}
0 commit comments