diff --git a/observe/observe_sweeper_test.go b/observe/observe_sweeper_test.go index ae6cd737..199f595a 100644 --- a/observe/observe_sweeper_test.go +++ b/observe/observe_sweeper_test.go @@ -36,6 +36,7 @@ func init() { "observe_preferred_path", "observe_datastream", "observe_app", + "observe_dataset_outbound_share", }, }) resource.AddTestSweepers("observe_monitor", &resource.Sweeper{ @@ -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 { @@ -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) }