@@ -192,12 +192,12 @@ func resourceMongoDBAtlasProjectAPIKeyUpdate(ctx context.Context, d *schema.Reso
192
192
apiKeyID := ids ["api_key_id" ]
193
193
194
194
if d .HasChange ("project_assignment" ) {
195
- // Getting the current api_keys and the new api_keys with changes
196
- newAPIKeys , changedAPIKeys , removedAPIKeys := getStateProjectAssignmentAPIKeys (d )
195
+ // Getting the changes to api key project assignments
196
+ newAssignments , changedAssignments , removedAssignments := getStateProjectAssignmentAPIKeys (d )
197
197
198
- // Adding new api_keys into the project
199
- if len (newAPIKeys ) > 0 {
200
- for _ , apiKey := range newAPIKeys {
198
+ // Adding new projects assignments
199
+ if len (newAssignments ) > 0 {
200
+ for _ , apiKey := range newAssignments {
201
201
projectID := apiKey .(map [string ]any )["project_id" ].(string )
202
202
roles := expandStringList (apiKey .(map [string ]any )["role_names" ].(* schema.Set ).List ())
203
203
_ , err := conn .ProjectAPIKeys .Assign (ctx , projectID , apiKeyID , & matlas.AssignAPIKey {
@@ -209,17 +209,20 @@ func resourceMongoDBAtlasProjectAPIKeyUpdate(ctx context.Context, d *schema.Reso
209
209
}
210
210
}
211
211
212
- // Removing api_keys from the project
213
- for _ , apiKey := range removedAPIKeys {
212
+ // Removing projects assignments
213
+ for _ , apiKey := range removedAssignments {
214
214
projectID := apiKey .(map [string ]any )["project_id" ].(string )
215
215
_ , err := conn .ProjectAPIKeys .Unassign (ctx , projectID , apiKeyID )
216
+ if err != nil && strings .Contains (err .Error (), "GROUP_NOT_FOUND" ) {
217
+ continue // allows removing assignment for a project that has been deleted
218
+ }
216
219
if err != nil {
217
220
return diag .Errorf ("error removing api_key(%s) from the project(%s): %s" , apiKeyID , projectID , err )
218
221
}
219
222
}
220
223
221
- // Updating the role names for the api_key
222
- for _ , apiKey := range changedAPIKeys {
224
+ // Updating the role names for the project assignments
225
+ for _ , apiKey := range changedAssignments {
223
226
projectID := apiKey .(map [string ]any )["project_id" ].(string )
224
227
roles := expandStringList (apiKey .(map [string ]any )["role_names" ].(* schema.Set ).List ())
225
228
_ , err := conn .ProjectAPIKeys .Assign (ctx , projectID , apiKeyID , & matlas.AssignAPIKey {
@@ -396,30 +399,30 @@ func newProjectAssignment(ctx context.Context, conn *matlas.Client, apiKeyID str
396
399
return results , nil
397
400
}
398
401
399
- func getStateProjectAssignmentAPIKeys (d * schema.ResourceData ) (newAPIKeys , changedAPIKeys , removedAPIKeys []any ) {
400
- currentAPIKeys , changes := d .GetChange ("project_assignment" )
402
+ func getStateProjectAssignmentAPIKeys (d * schema.ResourceData ) (newAssignments , changedAssignments , removedAssignments []any ) {
403
+ prevAssignments , currAssignments := d .GetChange ("project_assignment" )
401
404
402
- rAPIKeys := currentAPIKeys .(* schema.Set ).Difference (changes .(* schema.Set ))
403
- nAPIKeys := changes .(* schema.Set ).Difference (currentAPIKeys .(* schema.Set ))
404
- changedAPIKeys = make ([]any , 0 )
405
+ rAssignments := prevAssignments .(* schema.Set ).Difference (currAssignments .(* schema.Set ))
406
+ nAssignments := currAssignments .(* schema.Set ).Difference (prevAssignments .(* schema.Set ))
407
+ changedAssignments = make ([]any , 0 )
405
408
406
- for _ , changed := range nAPIKeys .List () {
407
- for _ , removed := range rAPIKeys .List () {
409
+ for _ , changed := range nAssignments .List () {
410
+ for _ , removed := range rAssignments .List () {
408
411
if changed .(map [string ]any )["project_id" ] == removed .(map [string ]any )["project_id" ] {
409
- rAPIKeys .Remove (removed )
412
+ rAssignments .Remove (removed )
410
413
}
411
414
}
412
415
413
- for _ , current := range currentAPIKeys .(* schema.Set ).List () {
416
+ for _ , current := range prevAssignments .(* schema.Set ).List () {
414
417
if changed .(map [string ]any )["project_id" ] == current .(map [string ]any )["project_id" ] {
415
- changedAPIKeys = append (changedAPIKeys , changed .(map [string ]any ))
416
- nAPIKeys .Remove (changed )
418
+ changedAssignments = append (changedAssignments , changed .(map [string ]any ))
419
+ nAssignments .Remove (changed )
417
420
}
418
421
}
419
422
}
420
423
421
- newAPIKeys = nAPIKeys .List ()
422
- removedAPIKeys = rAPIKeys .List ()
424
+ newAssignments = nAssignments .List ()
425
+ removedAssignments = rAssignments .List ()
423
426
424
427
return
425
428
}
0 commit comments