From 9947b44b38e7014496ae5b399feee999efee5b52 Mon Sep 17 00:00:00 2001 From: Scott Winkler Date: Thu, 27 Jul 2023 09:44:00 -0700 Subject: [PATCH] fix sweepers (#1980) --- pkg/sdk/sweepers.go | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/pkg/sdk/sweepers.go b/pkg/sdk/sweepers.go index 2e874c64f4..083b0f63c3 100644 --- a/pkg/sdk/sweepers.go +++ b/pkg/sdk/sweepers.go @@ -10,11 +10,15 @@ import ( func Sweep(client *Client, prefix string) error { sweepers := []func() error{ + getAccountPolicyAttachementsSweeper(client), + getResourceMonitorSweeper(client, prefix), getFailoverGroupSweeper(client, prefix), getShareSweeper(client, prefix), getDatabaseSweeper(client, prefix), getWarehouseSweeper(client, prefix), getRoleSweeper(client, prefix), + // todo: users, integrations, replication groups, network policies + // getUserSweeper(client, prefix), } for _, sweeper := range sweepers { if err := sweeper(); err != nil { @@ -28,6 +32,32 @@ func SweepAll(client *Client) error { return Sweep(client, "") } +func getResourceMonitorSweeper(client *Client, prefix string) func() error { + return func() error { + if prefix == "" { + log.Printf("[DEBUG] Sweeping all resource monitors") + } else { + log.Printf("[DEBUG] Sweeping all resource monitors with prefix %s", prefix) + } + ctx := context.Background() + rms, err := client.ResourceMonitors.Show(ctx, nil) + if err != nil { + return err + } + for _, rm := range rms { + if prefix == "" || strings.HasPrefix(rm.Name, prefix) { + log.Printf("[DEBUG] Dropping resource monitor %s", rm.Name) + if err := client.ResourceMonitors.Drop(ctx, rm.ID()); err != nil { + return err + } + } else { + log.Printf("[DEBUG] Skipping resource monitor %s", rm.Name) + } + } + return nil + } +} + func getFailoverGroupSweeper(client *Client, prefix string) func() error { return func() error { if prefix == "" { @@ -61,6 +91,20 @@ func getFailoverGroupSweeper(client *Client, prefix string) func() error { } } +func getUserSweeper(client *Client, prefix string) func() error { + return func() error { + /*ctx := context.Background() + users, client.Users.Show(ctx,nil) // LIKE '%TEST_%' + for _, user := range users { + err := client.Users.Drop(ctx, user.ID(), nil) + if err != nil { + return err + } + }*/ + return nil + } +} + func getRoleSweeper(client *Client, prefix string) func() error { return func() error { if prefix == "" { @@ -164,3 +208,23 @@ func getWarehouseSweeper(client *Client, prefix string) func() error { return nil } } + +func getAccountPolicyAttachementsSweeper(client *Client) func() error { + return func() error { + log.Printf("[DEBUG] Unsetting password and session policies set on the account level") + ctx := context.Background() + opts := &AlterAccountOptions{ + Unset: &AccountUnset{ + PasswordPolicy: Bool(true), + }, + } + _ = client.Accounts.Alter(ctx, opts) + opts = &AlterAccountOptions{ + Unset: &AccountUnset{ + SessionPolicy: Bool(true), + }, + } + _ = client.Accounts.Alter(ctx, opts) + return nil + } +}