Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Nuke stale objects #2869

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ sweep: ## destroy the whole architecture; USE ONLY FOR DEVELOPMENT ACCOUNTS
@echo "Are you sure? [y/n]" >&2
@read -r REPLY; \
if echo "$$REPLY" | grep -qG "^[yY]$$"; then \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout 300s -run ^TestSweepAll ./pkg/sdk -v; \
TEST_SF_TF_ENABLE_SWEEP=1 go test -timeout 300s -run "^(TestSweepAll|Test_Sweeper_NukeStaleObjects)" ./pkg/sdk -v; \
else echo "Aborting..."; \
fi;

Expand Down
95 changes: 95 additions & 0 deletions pkg/sdk/sweepers_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package sdk

import (
"context"
"fmt"
"log"
"testing"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
Expand Down Expand Up @@ -29,3 +33,94 @@ func TestSweepAll(t *testing.T) {
assert.NoError(t, err)
})
}

func Test_Sweeper_NukeStaleObjects(t *testing.T) {
_ = testenvs.GetOrSkipTest(t, testenvs.EnableSweep)

t.Run("sweep integration test precreated objects", func(t *testing.T) {
client := testClient(t)
secondaryClient := testSecondaryClient(t)

err := nukeWarehouses(client, "int_test_wh_%")()
assert.NoError(t, err)

err = nukeWarehouses(secondaryClient, "int_test_wh_%")()
assert.NoError(t, err)

err = nukeDatabases(client, "int_test_db_%")()
assert.NoError(t, err)

err = nukeDatabases(secondaryClient, "int_test_db_%")()
assert.NoError(t, err)
})

t.Run("sweep acceptance tests precreated objects", func(t *testing.T) {
client := testClient(t)
secondaryClient := testSecondaryClient(t)

err := nukeWarehouses(client, "acc_test_wh_%")()
assert.NoError(t, err)

err = nukeWarehouses(secondaryClient, "acc_test_wh_%")()
assert.NoError(t, err)

err = nukeDatabases(client, "acc_test_db_%")()
assert.NoError(t, err)

err = nukeDatabases(secondaryClient, "acc_test_db_%")()
assert.NoError(t, err)
})

// TODO [SNOW-955520]: nuke stale objects (e.g. created more than 2 weeks ago)
}

// TODO [SNOW-955520]: generalize nuke methods (sweepers too)
func nukeWarehouses(client *Client, prefix string) func() error {
return func() error {
log.Printf("[DEBUG] Nuking warehouses with prefix %s\n", prefix)
ctx := context.Background()

whs, err := client.Warehouses.Show(ctx, &ShowWarehouseOptions{Like: &Like{Pattern: String(prefix)}})
if err != nil {
return fmt.Errorf("sweeping warehouses ended with error, err = %w", err)
}
log.Printf("[DEBUG] Found %d warehouses matching search criteria\n", len(whs))
for idx, wh := range whs {
log.Printf("[DEBUG] Processing warehouse [%d/%d]: %s...\n", idx+1, len(whs), wh.ID().FullyQualifiedName())
if wh.Name != "SNOWFLAKE" && wh.CreatedOn.Before(time.Now().Add(-4*time.Hour)) {
log.Printf("[DEBUG] Dropping warehouse %s, created at: %s\n", wh.ID().FullyQualifiedName(), wh.CreatedOn.String())
if err := client.Warehouses.Drop(ctx, wh.ID(), &DropWarehouseOptions{IfExists: Bool(true)}); err != nil {
return fmt.Errorf("sweeping warehouse %s ended with error, err = %w", wh.ID().FullyQualifiedName(), err)
}
} else {
log.Printf("[DEBUG] Skipping warehouse %s, created at: %s\n", wh.ID().FullyQualifiedName(), wh.CreatedOn.String())
}
}
return nil
}
}

func nukeDatabases(client *Client, prefix string) func() error {
return func() error {
log.Printf("[DEBUG] Nuking databases with prefix %s\n", prefix)
ctx := context.Background()

dbs, err := client.Databases.Show(ctx, &ShowDatabasesOptions{Like: &Like{Pattern: String(prefix)}})
if err != nil {
return fmt.Errorf("sweeping databases ended with error, err = %w", err)
}
log.Printf("[DEBUG] Found %d databases matching search criteria\n", len(dbs))
for idx, db := range dbs {
log.Printf("[DEBUG] Processing database [%d/%d]: %s...\n", idx+1, len(dbs), db.ID().FullyQualifiedName())
if db.Name != "SNOWFLAKE" && db.CreatedOn.Before(time.Now().Add(-4*time.Hour)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: max age of objects can be configurable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add to SNOW-955520.

log.Printf("[DEBUG] Dropping database %s, created at: %s\n", db.ID().FullyQualifiedName(), db.CreatedOn.String())
if err := client.Databases.Drop(ctx, db.ID(), &DropDatabaseOptions{IfExists: Bool(true)}); err != nil {
return fmt.Errorf("sweeping database %s ended with error, err = %w", db.ID().FullyQualifiedName(), err)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a question: did you consider just logging an error instead of early return? I think we could at least try removing other objects.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, getting an error here means that something is f***ed up (e.g. with connection) because removal with if exists should succeed no matter what. That's why early return here is IMO more accurate.

}
} else {
log.Printf("[DEBUG] Skipping database %s, created at: %s\n", db.ID().FullyQualifiedName(), db.CreatedOn.String())
}
}
return nil
}
}
Loading