@@ -7,8 +7,10 @@ import (
7
7
"log"
8
8
"net/http"
9
9
"strings"
10
+ "time"
10
11
11
12
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
13
+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12
14
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13
15
14
16
matlas "go.mongodb.org/atlas/mongodbatlas"
@@ -219,7 +221,25 @@ func resourceMongoDBAtlasTeamDelete(ctx context.Context, d *schema.ResourceData,
219
221
orgID := ids ["org_id" ]
220
222
id := ids ["id" ]
221
223
222
- _ , err := conn .Teams .RemoveTeamFromOrganization (ctx , orgID , id )
224
+ err := resource .RetryContext (ctx , 1 * time .Hour , func () * resource.RetryError {
225
+ _ , err := conn .Teams .RemoveTeamFromOrganization (ctx , orgID , id )
226
+ if err != nil {
227
+ var target * matlas.ErrorResponse
228
+ if errors .As (err , & target ) && target .ErrorCode == "CANNOT_DELETE_TEAM_ASSIGNED_TO_PROJECT" {
229
+ projectID , err := getProjectIDByTeamID (ctx , conn , id )
230
+ if err != nil {
231
+ return resource .NonRetryableError (err )
232
+ }
233
+
234
+ _ , err = conn .Teams .RemoveTeamFromProject (ctx , projectID , id )
235
+ if err != nil {
236
+ return resource .NonRetryableError (fmt .Errorf (errorTeamDelete , id , err ))
237
+ }
238
+ return resource .RetryableError (fmt .Errorf ("will retry again" ))
239
+ }
240
+ }
241
+ return nil
242
+ })
223
243
if err != nil {
224
244
return diag .FromErr (fmt .Errorf (errorTeamDelete , id , err ))
225
245
}
@@ -267,3 +287,26 @@ func expandStringListFromSetSchema(list *schema.Set) []string {
267
287
268
288
return res
269
289
}
290
+
291
+ func getProjectIDByTeamID (ctx context.Context , conn * matlas.Client , teamID string ) (string , error ) {
292
+ options := & matlas.ListOptions {}
293
+ projects , _ , err := conn .Projects .GetAllProjects (ctx , options )
294
+ if err != nil {
295
+ return "" , fmt .Errorf ("error getting projects information: %s" , err )
296
+ }
297
+
298
+ for _ , project := range projects .Results {
299
+ teams , _ , err := conn .Projects .GetProjectTeamsAssigned (ctx , project .ID )
300
+ if err != nil {
301
+ return "" , fmt .Errorf ("error getting teams from project information: %s" , err )
302
+ }
303
+
304
+ for _ , team := range teams .Results {
305
+ if team .TeamID == teamID {
306
+ return project .ID , nil
307
+ }
308
+ }
309
+ }
310
+
311
+ return "" , nil
312
+ }
0 commit comments