@@ -11,11 +11,13 @@ import (
11
11
"testing"
12
12
"time"
13
13
14
+ "go.mongodb.org/atlas-sdk/v20250312001/admin"
15
+
16
+ "github.com/stretchr/testify/require"
17
+
14
18
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
15
19
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema"
16
20
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
17
- "github.com/stretchr/testify/require"
18
- "go.mongodb.org/atlas-sdk/v20250312002/admin"
19
21
)
20
22
21
23
const (
40
42
"CANNOT_CLOSE_GROUP_ACTIVE_PEERING_CONNECTIONS" ,
41
43
"CANNOT_CLOSE_GROUP_ACTIVE_ATLAS_DATA_LAKES" ,
42
44
"CANNOT_CLOSE_GROUP_ACTIVE_ATLAS_DATA_FEDERATION_PRIVATE_ENDPOINTS" ,
45
+ "CANNOT_CLOSE_GROUP_ACTIVE_STREAMS_RESOURCE" ,
46
+ "CANNOT_CLOSE_GROUP_ACTIVE_ATLAS_PRIVATE_ENDPOINT_SERVICES" ,
43
47
}
44
48
)
45
49
@@ -153,7 +157,7 @@ func findRetryErrorCode(err error) string {
153
157
return ""
154
158
}
155
159
func deleteProject (ctx context.Context , client * admin.APIClient , projectID string ) error {
156
- _ , err := client .ProjectsApi .DeleteProject (ctx , projectID ).Execute ()
160
+ _ , _ , err := client .ProjectsApi .DeleteProject (ctx , projectID ).Execute ()
157
161
if err == nil || admin .IsErrorCode (err , "PROJECT_NOT_FOUND" ) {
158
162
return nil
159
163
}
@@ -187,6 +191,14 @@ func removeProjectResources(ctx context.Context, t *testing.T, dryRun bool, clie
187
191
if federatedDatabasesRemoved > 0 {
188
192
changes = append (changes , fmt .Sprintf ("removed %d federated databases" , federatedDatabasesRemoved ))
189
193
}
194
+ streamInstancesRemoved := removeStreamInstances (ctx , t , dryRun , client , projectID )
195
+ if streamInstancesRemoved > 0 {
196
+ changes = append (changes , fmt .Sprintf ("removed %d stream instances" , streamInstancesRemoved ))
197
+ }
198
+ privateEndpointServicesRemoved := removePrivateEndpointServices (ctx , t , dryRun , client , projectID )
199
+ if privateEndpointServicesRemoved > 0 {
200
+ changes = append (changes , fmt .Sprintf ("removed %d private endpoint services" , privateEndpointServicesRemoved ))
201
+ }
190
202
return strings .Join (changes , ", " )
191
203
}
192
204
@@ -296,13 +308,72 @@ func removeDataLakePipelines(ctx context.Context, t *testing.T, dryRun bool, cli
296
308
pipelineID := p .GetId ()
297
309
t .Logf ("delete pipeline %s" , pipelineID )
298
310
if ! dryRun {
299
- _ , err = client .DataLakePipelinesApi .DeletePipeline (ctx , projectID , pipelineID ).Execute ()
311
+ _ , _ , err = client .DataLakePipelinesApi .DeletePipeline (ctx , projectID , pipelineID ).Execute ()
300
312
require .NoError (t , err )
301
313
}
302
314
}
303
315
return len (datalakeResults )
304
316
}
305
317
318
+ func removeStreamInstances (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
319
+ t .Helper ()
320
+ streamInstances , _ , err := client .StreamsApi .ListStreamInstances (ctx , projectID ).Execute ()
321
+ require .NoError (t , err )
322
+
323
+ for _ , instance := range * streamInstances .Results {
324
+ instanceName := * instance .Name
325
+ id := instance .GetId ()
326
+ t .Logf ("delete stream instance %s" , id )
327
+
328
+ if ! dryRun {
329
+ _ , _ , err = client .StreamsApi .DeleteStreamInstance (ctx , projectID , instanceName ).Execute ()
330
+ if err != nil && admin .IsErrorCode (err , "STREAM_TENANT_HAS_STREAM_PROCESSORS" ) {
331
+ t .Logf ("stream instance %s has stream processors, attempting to delete" , id )
332
+ streamProcessors , _ , spErr := client .StreamsApi .ListStreamProcessors (ctx , projectID , instanceName ).Execute ()
333
+ require .NoError (t , spErr )
334
+
335
+ for _ , processor := range * streamProcessors .Results {
336
+ t .Logf ("delete stream processor %s" , processor .Id )
337
+ _ , err = client .StreamsApi .DeleteStreamProcessor (ctx , projectID , instanceName , processor .Name ).Execute ()
338
+ require .NoError (t , err )
339
+ }
340
+ t .Logf ("retry delete stream instance %s after removing stream processors" , id )
341
+ _ , _ , err = client .StreamsApi .DeleteStreamInstance (ctx , projectID , instanceName ).Execute ()
342
+ require .NoError (t , err )
343
+ } else {
344
+ require .NoError (t , err )
345
+ }
346
+ }
347
+ }
348
+ return len (* streamInstances .Results )
349
+ }
350
+
351
+ func removePrivateEndpointServices (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
352
+ t .Helper ()
353
+ totalCount := 0
354
+ cloudProviders := []string {"AWS" , "AZURE" , "GCP" }
355
+
356
+ for _ , provider := range cloudProviders {
357
+ endpointServices , _ , err := client .PrivateEndpointServicesApi .ListPrivateEndpointServices (ctx , projectID , provider ).Execute ()
358
+ if err != nil {
359
+ t .Errorf ("failed to list private endpoint services for %s: %v" , provider , err )
360
+ continue
361
+ }
362
+
363
+ for _ , service := range endpointServices {
364
+ id := service .GetId ()
365
+ t .Logf ("delete private endpoint service %s for provider %s" , id , provider )
366
+ if ! dryRun {
367
+ _ , _ , err := client .PrivateEndpointServicesApi .DeletePrivateEndpointService (ctx , projectID , provider , id ).Execute ()
368
+ require .NoError (t , err )
369
+ }
370
+ }
371
+ totalCount += len (endpointServices )
372
+ }
373
+
374
+ return totalCount
375
+ }
376
+
306
377
func removeFederatedDatabasePrivateEndpoints (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
307
378
t .Helper ()
308
379
paginatedResults , _ , err := client .DataFederationApi .ListDataFederationPrivateEndpoints (ctx , projectID ).Execute ()
@@ -312,7 +383,7 @@ func removeFederatedDatabasePrivateEndpoints(ctx context.Context, t *testing.T,
312
383
endpointID := f .GetEndpointId ()
313
384
t .Logf ("delete federated private endpoint %s" , endpointID )
314
385
if ! dryRun {
315
- _ , err = client .DataFederationApi .DeleteDataFederationPrivateEndpoint (ctx , projectID , endpointID ).Execute ()
386
+ _ , _ , err = client .DataFederationApi .DeleteDataFederationPrivateEndpoint (ctx , projectID , endpointID ).Execute ()
316
387
require .NoError (t , err )
317
388
}
318
389
}
@@ -331,7 +402,7 @@ func removeFederatedDatabases(ctx context.Context, t *testing.T, dryRun bool, cl
331
402
federatedName := f .GetName ()
332
403
t .Logf ("delete federated %s" , federatedName )
333
404
if ! dryRun {
334
- _ , err = client .DataFederationApi .DeleteFederatedDatabase (ctx , projectID , federatedName ).Execute ()
405
+ _ , _ , err = client .DataFederationApi .DeleteFederatedDatabase (ctx , projectID , federatedName ).Execute ()
335
406
require .NoError (t , err )
336
407
}
337
408
}
0 commit comments