66 "fmt"
77 "log"
88 "slices"
9+ "strings"
910 "testing"
1011 "time"
1112
@@ -39,6 +40,44 @@ func TestSweepAll(t *testing.T) {
3940 })
4041}
4142
43+ func SweepAfterIntegrationTests (client * Client , suffix string ) error {
44+ return sweep (client , suffix )
45+ }
46+
47+ func SweepAfterAcceptanceTests (client * Client , suffix string ) error {
48+ return sweep (client , suffix )
49+ }
50+
51+ // TODO [SNOW-867247]: use if exists/use method from helper for dropping
52+ // TODO [SNOW-867247]: sweep all missing account-level objects (like users, integrations, replication groups, network policies, ...)
53+ // TODO [SNOW-867247]: extract sweepers to a separate dir
54+ // TODO [SNOW-867247]: rework the sweepers (funcs -> objects)
55+ // TODO [SNOW-867247]: consider generalization (almost all the sweepers follow the same pattern: show, drop if matches)
56+ // TODO [SNOW-867247]: consider failing after all sweepers and not with the first error
57+ // TODO [SNOW-867247]: consider showing only objects with the given suffix (in almost every sweeper)
58+ func sweep (client * Client , suffix string ) error {
59+ if suffix == "" {
60+ return fmt .Errorf ("suffix is required to run sweepers" )
61+ }
62+ sweepers := []func () error {
63+ getAccountPolicyAttachmentsSweeper (client ),
64+ getResourceMonitorSweeper (client , suffix ),
65+ getNetworkPolicySweeper (client , suffix ),
66+ nukeUsers (client , suffix ),
67+ getFailoverGroupSweeper (client , suffix ),
68+ getShareSweeper (client , suffix ),
69+ getDatabaseSweeper (client , suffix ),
70+ getWarehouseSweeper (client , suffix ),
71+ getRoleSweeper (client , suffix ),
72+ }
73+ for _ , sweeper := range sweepers {
74+ if err := sweeper (); err != nil {
75+ return err
76+ }
77+ }
78+ return nil
79+ }
80+
4281func Test_Sweeper_NukeStaleObjects (t * testing.T ) {
4382 _ = testenvs .GetOrSkipTest (t , testenvs .EnableSweep )
4483
@@ -81,7 +120,7 @@ func Test_Sweeper_NukeStaleObjects(t *testing.T) {
81120
82121 t .Run ("sweep users" , func (t * testing.T ) {
83122 for _ , c := range allClients {
84- err := nukeUsers (c )()
123+ err := nukeUsers (c , "" )()
85124 assert .NoError (t , err )
86125 }
87126 })
@@ -223,7 +262,7 @@ func nukeDatabases(client *Client, prefix string) func() error {
223262 }
224263}
225264
226- func nukeUsers (client * Client ) func () error {
265+ func nukeUsers (client * Client , suffix string ) func () error {
227266 protectedUsers := []string {
228267 "SNOWFLAKE" ,
229268 "ARTUR_SAWICKI" ,
@@ -239,27 +278,42 @@ func nukeUsers(client *Client) func() error {
239278 }
240279
241280 return func () error {
242- log .Println ("[DEBUG] Nuking users" )
243281 ctx := context .Background ()
244282
245- users , err := client .Users .Show (ctx , & ShowUserOptions {})
283+ var userDropCondition func (u User ) bool
284+ if suffix != "" {
285+ log .Printf ("[DEBUG] Sweeping users with suffix %s" , suffix )
286+ userDropCondition = func (u User ) bool {
287+ return strings .HasSuffix (u .Name , suffix )
288+ }
289+ } else {
290+ log .Println ("[DEBUG] Sweeping stale users" )
291+ userDropCondition = func (u User ) bool {
292+ return u .CreatedOn .Before (time .Now ().Add (- 15 * time .Minute ))
293+ }
294+ }
295+
296+ urs , err := client .Users .Show (ctx , new (ShowUserOptions ))
246297 if err != nil {
247- return fmt .Errorf ("sweeping users ended with error, err = %w" , err )
298+ return fmt .Errorf ("SHOW USERS ended with error, err = %w" , err )
248299 }
300+
301+ log .Printf ("[DEBUG] Found %d users" , len (urs ))
302+
249303 var errs []error
250- log . Printf ( "[DEBUG] Found %d users" , len ( users ))
251- for idx , user := range users {
252- log . Printf ( "[DEBUG] Processing user [%d/%d]: %s..." , idx + 1 , len ( users ), user . ID (). FullyQualifiedName ())
253- if ! slices .Contains (protectedUsers , user .Name ) && user . CreatedOn . Before ( time . Now (). Add ( - 15 * time . Minute ) ) {
304+ for idx , user := range urs {
305+ log . Printf ( "[DEBUG] Processing user [%d/%d]: %s..." , idx + 1 , len ( urs ), user . ID (). FullyQualifiedName ())
306+
307+ if ! slices .Contains (protectedUsers , user .Name ) && userDropCondition ( user ) {
254308 log .Printf ("[DEBUG] Dropping user %s" , user .ID ().FullyQualifiedName ())
255309 if err := client .Users .Drop (ctx , user .ID (), & DropUserOptions {IfExists : Bool (true )}); err != nil {
256- log .Printf ("[DEBUG] Dropping user %s, resulted in error %v" , user .ID ().FullyQualifiedName (), err )
257310 errs = append (errs , fmt .Errorf ("sweeping user %s ended with error, err = %w" , user .ID ().FullyQualifiedName (), err ))
258311 }
259312 } else {
260313 log .Printf ("[DEBUG] Skipping user %s" , user .ID ().FullyQualifiedName ())
261314 }
262315 }
316+
263317 return errors .Join (errs ... )
264318 }
265319}
0 commit comments