Skip to content

Commit

Permalink
sweep: Snowflake outbound shares and dataset outbound shares (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendrucker authored Oct 19, 2023
1 parent 0106d37 commit d90d87f
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions observe/observe_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func init() {
"observe_preferred_path",
"observe_datastream",
"observe_app",
"observe_dataset_outbound_share",
},
})
resource.AddTestSweepers("observe_monitor", &resource.Sweeper{
Expand Down Expand Up @@ -100,6 +101,17 @@ func init() {
Name: "observe_filedrop",
F: filedropSweeperFunc,
})
resource.AddTestSweepers("observe_snowflake_share_outbound", &resource.Sweeper{
Name: "observe_snowflake_share_outbound",
F: snowflakeShareOutboundSweeper,
Dependencies: []string{
"observe_dataset_outbound_share",
},
})
resource.AddTestSweepers("observe_dataset_outbound_share", &resource.Sweeper{
Name: "observe_dataset_outbound_share",
F: datasetOutboundShareSweeper,
})
}

type client struct {
Expand Down Expand Up @@ -718,6 +730,108 @@ func filedropSweeperFunc(pattern string) error {
return nil
}

func snowflakeShareOutboundSweeper(pattern string) error {
client, err := sharedClient(pattern)
if err != nil {
return err
}

ctx := context.Background()

workspaces, err := client.ListWorkspaces(ctx)
if err != nil {
return err
}

for _, workspace := range workspaces {
result, err := client.Meta.Run(ctx, `
query snowflakeSharesOutbound($workspaceId: ObjectId!) {
searchSnowflakeShareOutbound(workspaceId: $workspaceId) {
results {
id
name
}
}
}`, map[string]interface{}{
"workspaceId": workspace.Id,
})

if err != nil {
return fmt.Errorf("failed to lookup snowflake outbound shares: %w", err)
}

result = result["searchSnowflakeShareOutbound"].(map[string]interface{})

for _, i := range result["results"].([]interface{}) {
var (
item = i.(map[string]interface{})
id = item["id"].(string)
name = item["name"].(string)
)

if client.MatchName(name) {
log.Printf("[WARN] Deleting snowflake outbound share %s [id=%s]\n", name, id)
if err := client.DeleteSnowflakeShareOutbound(ctx, id); err != nil {
return err
}
}
}
}

return nil
}

func datasetOutboundShareSweeper(pattern string) error {
client, err := sharedClient(pattern)
if err != nil {
return err
}

ctx := context.Background()

workspaces, err := client.ListWorkspaces(ctx)
if err != nil {
return err
}

for _, workspace := range workspaces {
result, err := client.Meta.Run(ctx, `
query datasetOutboundShares($workspaceId: ObjectId!) {
searchDatasetOutboundShare(workspaceId: $workspaceId) {
results {
id
name
}
}
}`, map[string]interface{}{
"workspaceId": workspace.Id,
})

if err != nil {
return fmt.Errorf("failed to lookup dataset outbound shares: %w", err)
}

result = result["searchDatasetOutboundShare"].(map[string]interface{})

for _, i := range result["results"].([]interface{}) {
var (
item = i.(map[string]interface{})
id = item["id"].(string)
name = item["name"].(string)
)

if client.MatchName(name) {
log.Printf("[WARN] Deleting dataset outbound share %s [id=%s]\n", name, id)
if err := client.DeleteDatasetOutboundShare(ctx, id); err != nil {
return err
}
}
}
}

return nil
}

func TestMain(m *testing.M) {
resource.TestMain(m)
}

0 comments on commit d90d87f

Please sign in to comment.