Skip to content

Commit

Permalink
feat(sweep): connection pool support (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-savciuc authored Jan 9, 2024
1 parent 448b564 commit 02e59e9
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions internal/sdkprovider/service/connectionpool/sweep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//go:build sweep

package connectionpool

import (
"context"
"fmt"
"os"

"github.com/aiven/aiven-go-client/v2"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"

"github.com/aiven/terraform-provider-aiven/internal/schemautil"
"github.com/aiven/terraform-provider-aiven/internal/sweep"
)

func init() {
ctx := context.Background()

client, err := sweep.SharedClient()
if err != nil {
panic(fmt.Sprintf("error getting client: %s", err))
}

resource.AddTestSweepers("aiven_connection_pool", &resource.Sweeper{
Name: "aiven_connection_pool",
F: sweepConnectionPoll(ctx, client),
})

}

func sweepConnectionPoll(ctx context.Context, client *aiven.Client) func(string) error {
return func(id string) error {
projectName := os.Getenv("AIVEN_PROJECT_NAME")

services, err := client.Services.List(ctx, projectName)
if err != nil && !aiven.IsNotFound(err) {
return fmt.Errorf("error retrieving a list of services for a project `%s`: %w", projectName, err)
}

for _, s := range services {
if s.Type != schemautil.ServiceTypePG {
continue
}

l, err := client.ConnectionPools.List(ctx, projectName, s.Name)
if err != nil && !aiven.IsNotFound(err) {
return err
}

for _, pool := range l {
err = client.ConnectionPools.Delete(ctx, projectName, s.Name, pool.PoolName)
if err != nil && !aiven.IsNotFound(err) {
return err
}
}
}
return nil
}
}

0 comments on commit 02e59e9

Please sign in to comment.