Skip to content

Commit

Permalink
fix sweepers (#1980)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-swinkler authored Jul 27, 2023
1 parent ef4334f commit 9947b44
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions pkg/sdk/sweepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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
}
}

0 comments on commit 9947b44

Please sign in to comment.