-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sweep): connection pool support (#1510)
- Loading branch information
1 parent
448b564
commit 02e59e9
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |