@@ -11,11 +11,13 @@ import (
1111 "testing"
1212 "time"
1313
14+ "go.mongodb.org/atlas-sdk/v20250312002/admin"
15+
16+ "github.com/stretchr/testify/require"
17+
1418 "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
1519 "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema"
1620 "github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
17- "github.com/stretchr/testify/require"
18- "go.mongodb.org/atlas-sdk/v20250312002/admin"
1921)
2022
2123const (
4042 "CANNOT_CLOSE_GROUP_ACTIVE_PEERING_CONNECTIONS" ,
4143 "CANNOT_CLOSE_GROUP_ACTIVE_ATLAS_DATA_LAKES" ,
4244 "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" ,
4347 }
4448)
4549
@@ -127,15 +131,16 @@ func TestCleanProjectAndClusters(t *testing.T) {
127131 })
128132 }
129133 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 )
131136 t .Logf ("SUMMARY\n Projects changed from %d to %d\n delete_errors=%d\n DRY_RUN=%t" , projectsBefore , len (projectsAfter ), deleteErrors , dryRun )
132137 })
133138}
134139
135140func readAllProjects (ctx context.Context , t * testing.T , client * admin.APIClient ) []admin.Group {
136141 t .Helper ()
137142 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 ()
139144 })
140145 require .NoError (t , err )
141146 return projects
@@ -187,6 +192,14 @@ func removeProjectResources(ctx context.Context, t *testing.T, dryRun bool, clie
187192 if federatedDatabasesRemoved > 0 {
188193 changes = append (changes , fmt .Sprintf ("removed %d federated databases" , federatedDatabasesRemoved ))
189194 }
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+ }
190203 return strings .Join (changes , ", " )
191204}
192205
@@ -303,6 +316,65 @@ func removeDataLakePipelines(ctx context.Context, t *testing.T, dryRun bool, cli
303316 return len (datalakeResults )
304317}
305318
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+
306378func removeFederatedDatabasePrivateEndpoints (ctx context.Context , t * testing.T , dryRun bool , client * admin.APIClient , projectID string ) int {
307379 t .Helper ()
308380 paginatedResults , _ , err := client .DataFederationApi .ListDataFederationPrivateEndpoints (ctx , projectID ).Execute ()
0 commit comments