@@ -11,11 +11,13 @@ import (
11
11
"testing"
12
12
"time"
13
13
14
+ "go.mongodb.org/atlas-sdk/v20250312002/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
@@ -127,15 +131,16 @@ func TestCleanProjectAndClusters(t *testing.T) {
127
131
})
128
132
}
129
133
t .Cleanup (func () {
130
- projectsAfter := readAllProjects (t .Context (), t , client )
134
+ //nolint:usetesting // reason: using context.Background() here intentionally because t.Context() is canceled at cleanup
135
+ projectsAfter := readAllProjects (context .Background (), t , client )
131
136
t .Logf ("SUMMARY\n Projects changed from %d to %d\n delete_errors=%d\n DRY_RUN=%t" , projectsBefore , len (projectsAfter ), deleteErrors , dryRun )
132
137
})
133
138
}
134
139
135
140
func readAllProjects (ctx context.Context , t * testing.T , client * admin.APIClient ) []admin.Group {
136
141
t .Helper ()
137
142
projects , err := dsschema .AllPages (ctx , func (ctx context.Context , pageNum int ) (dsschema.PaginateResponse [admin.Group ], * http.Response , error ) {
138
- return client .ProjectsApi .ListProjects (t . Context () ).ItemsPerPage (itemsPerPage ).PageNum (pageNum ).Execute ()
143
+ return client .ProjectsApi .ListProjects (ctx ).ItemsPerPage (itemsPerPage ).PageNum (pageNum ).Execute ()
139
144
})
140
145
require .NoError (t , err )
141
146
return projects
@@ -187,6 +192,14 @@ func removeProjectResources(ctx context.Context, t *testing.T, dryRun bool, clie
187
192
if federatedDatabasesRemoved > 0 {
188
193
changes = append (changes , fmt .Sprintf ("removed %d federated databases" , federatedDatabasesRemoved ))
189
194
}
195
+ streamInstancesRemoved := removeStreamInstances (ctx , t , dryRun , client , projectID )
196
+ if streamInstancesRemoved > 0 {
197
+ changes = append (changes , fmt .Sprintf ("removed %d stream instances" , streamInstancesRemoved ))
198
+ }
199
+ privateEndpointServicesRemoved := removePrivateEndpointServices (ctx , t , dryRun , client , projectID )
200
+ if privateEndpointServicesRemoved > 0 {
201
+ changes = append (changes , fmt .Sprintf ("removed %d private endpoint services" , privateEndpointServicesRemoved ))
202
+ }
190
203
return strings .Join (changes , ", " )
191
204
}
192
205
@@ -303,6 +316,65 @@ func removeDataLakePipelines(ctx context.Context, t *testing.T, dryRun bool, cli
303
316
return len (datalakeResults )
304
317
}
305
318
319
+ func removeStreamInstances (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
320
+ t .Helper ()
321
+ streamInstances , _ , err := client .StreamsApi .ListStreamInstances (ctx , projectID ).Execute ()
322
+ require .NoError (t , err )
323
+
324
+ for _ , instance := range * streamInstances .Results {
325
+ instanceName := * instance .Name
326
+ id := instance .GetId ()
327
+ t .Logf ("delete stream instance %s" , id )
328
+
329
+ if ! dryRun {
330
+ _ , err = client .StreamsApi .DeleteStreamInstance (ctx , projectID , instanceName ).Execute ()
331
+ if err != nil && admin .IsErrorCode (err , "STREAM_TENANT_HAS_STREAM_PROCESSORS" ) {
332
+ t .Logf ("stream instance %s has stream processors, attempting to delete" , id )
333
+ streamProcessors , _ , spErr := client .StreamsApi .ListStreamProcessors (ctx , projectID , instanceName ).Execute ()
334
+ require .NoError (t , spErr )
335
+
336
+ for _ , processor := range * streamProcessors .Results {
337
+ t .Logf ("delete stream processor %s" , processor .Id )
338
+ _ , err = client .StreamsApi .DeleteStreamProcessor (ctx , projectID , instanceName , processor .Name ).Execute ()
339
+ require .NoError (t , err )
340
+ }
341
+ t .Logf ("retry delete stream instance %s after removing stream processors" , id )
342
+ _ , err = client .StreamsApi .DeleteStreamInstance (ctx , projectID , instanceName ).Execute ()
343
+ require .NoError (t , err )
344
+ } else {
345
+ require .NoError (t , err )
346
+ }
347
+ }
348
+ }
349
+ return len (* streamInstances .Results )
350
+ }
351
+
352
+ func removePrivateEndpointServices (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
353
+ t .Helper ()
354
+ totalCount := 0
355
+ cloudProviders := []string {"AWS" , "AZURE" , "GCP" }
356
+
357
+ for _ , provider := range cloudProviders {
358
+ endpointServices , _ , err := client .PrivateEndpointServicesApi .ListPrivateEndpointServices (ctx , projectID , provider ).Execute ()
359
+ if err != nil {
360
+ t .Errorf ("failed to list private endpoint services for %s: %v" , provider , err )
361
+ continue
362
+ }
363
+
364
+ for _ , service := range endpointServices {
365
+ id := service .GetId ()
366
+ t .Logf ("delete private endpoint service %s for provider %s" , id , provider )
367
+ if ! dryRun {
368
+ _ , err := client .PrivateEndpointServicesApi .DeletePrivateEndpointService (ctx , projectID , provider , id ).Execute ()
369
+ require .NoError (t , err )
370
+ }
371
+ }
372
+ totalCount += len (endpointServices )
373
+ }
374
+
375
+ return totalCount
376
+ }
377
+
306
378
func removeFederatedDatabasePrivateEndpoints (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
307
379
t .Helper ()
308
380
paginatedResults , _ , err := client .DataFederationApi .ListDataFederationPrivateEndpoints (ctx , projectID ).Execute ()
0 commit comments