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: Migrate Warehouse resource + datasource to new SDK #1792

Merged
merged 8 commits into from
May 12, 2023
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
14 changes: 0 additions & 14 deletions docs/resources/warehouse.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ resource "snowflake_warehouse" "warehouse" {
- `scaling_policy` (String) Specifies the policy for automatically starting and shutting down clusters in a multi-cluster warehouse running in Auto-scale mode.
- `statement_queued_timeout_in_seconds` (Number) Object parameter that specifies the time, in seconds, a SQL statement (query, DDL, DML, etc.) can be queued on a warehouse before it is canceled by the system.
- `statement_timeout_in_seconds` (Number) Specifies the time, in seconds, after which a running SQL statement (query, DDL, DML, etc.) is canceled by the system
- `tag` (Block List, Deprecated) Definitions of a tag to associate with the resource. (see [below for nested schema](#nestedblock--tag))
- `wait_for_provisioning` (Boolean) Specifies whether the warehouse, after being resized, waits for all the servers to provision before executing any queued or new queries.
- `warehouse_size` (String) Specifies the size of the virtual warehouse. Larger warehouse sizes 5X-Large and 6X-Large are currently in preview and only available on Amazon Web Services (AWS).
- `warehouse_type` (String) Specifies a STANDARD or SNOWPARK-OPTIMIZED warehouse
Expand All @@ -51,19 +50,6 @@ resource "snowflake_warehouse" "warehouse" {

- `id` (String) The ID of this resource.

<a id="nestedblock--tag"></a>
### Nested Schema for `tag`

Required:

- `name` (String) Tag name, e.g. department.
- `value` (String) Tag value, e.g. marketing_info.

Optional:

- `database` (String) Name of the database that the tag was created in.
- `schema` (String) Name of the schema that the tag was created in.

## Import

Import is supported using the following syntax:
Expand Down
23 changes: 8 additions & 15 deletions pkg/datasources/warehouses.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package datasources

import (
"context"
"database/sql"
"errors"
"fmt"
"log"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -59,31 +59,24 @@ func Warehouses() *schema.Resource {

func ReadWarehouses(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
client := sdk.NewClientFromDB(db)
ctx := context.Background()

account, err := snowflake.ReadCurrentAccount(db)
if err != nil {
log.Print("[DEBUG] unable to retrieve current account")
d.SetId("")
return nil
}

d.SetId(fmt.Sprintf("%s.%s", account.Account, account.Region))

currentWarehouses, err := snowflake.ListWarehouses(db)
if errors.Is(err, sql.ErrNoRows) {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] no warehouses found in account (%s)", d.Id())
d.SetId("")
return nil
} else if err != nil {
log.Printf("[DEBUG] unable to parse warehouses in account (%s)", d.Id())
d.SetId("")
return nil
result, err := client.Warehouses.Show(ctx, nil)
if err != nil {
return err
}

warehouses := []map[string]interface{}{}

for _, warehouse := range currentWarehouses {
for _, warehouse := range result {
warehouseMap := map[string]interface{}{}

warehouseMap["name"] = warehouse.Name
Expand Down
4 changes: 2 additions & 2 deletions pkg/resources/resource_monitor_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resourceMonitorConfig(accName string) string {
resource "snowflake_warehouse" "warehouse" {
name = "test"
comment = "foo"
warehouse_size = "small"
warehouse_size = "SMALL"
}

resource "snowflake_resource_monitor" "test" {
Expand All @@ -77,7 +77,7 @@ func resourceMonitorConfig2(accName string) string {
resource "snowflake_warehouse" "warehouse" {
name = "test"
comment = "foo"
warehouse_size = "small"
warehouse_size = "SMALL"
}

resource "snowflake_resource_monitor" "test" {
Expand Down
31 changes: 0 additions & 31 deletions pkg/resources/snowflake_sweeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,6 @@ func TestMain(m *testing.M) {
resource.TestMain(m)
}

func getWarehousesSweeper(name string) *resource.Sweeper {
return &resource.Sweeper{
Name: name,
F: func(ununsed string) error {
db, err := provider.GetDatabaseHandleFromEnv()
if err != nil {
return fmt.Errorf("Error getting db handle: %w", err)
}

warehouses, err := snowflake.ListWarehouses(db)
if err != nil {
return fmt.Errorf("Error listing warehouses: %w", err)
}

for _, wh := range warehouses {
log.Printf("[DEBUG] Testing if warehouse %s starts with tst-terraform", wh.Name)
if strings.HasPrefix(wh.Name, "tst-terraform") {
log.Printf("[DEBUG] deleting warehouse %s", wh.Name)
whBuilder := snowflake.NewWarehouseBuilder(name).Builder
stmt := whBuilder.Drop()
if err := snowflake.Exec(db, stmt); err != nil {
return fmt.Errorf("Error deleting warehouse %q %w", wh.Name, err)
}
}
}
return nil
},
}
}

func getDatabaseSweepers(name string) *resource.Sweeper {
return &resource.Sweeper{
Name: name,
Expand Down Expand Up @@ -161,7 +131,6 @@ func getIntegrationsSweeper(name string) *resource.Sweeper {
// Sweepers usually go along with the tests. In TF[CE]'s case everything depends on the organization,
// which means that if we delete it then all the other entities will be deleted automatically.
func init() {
resource.AddTestSweepers("wh_sweeper", getWarehousesSweeper("wh_sweeper"))
resource.AddTestSweepers("db_sweeper", getDatabaseSweepers("db_sweeper"))
resource.AddTestSweepers("role_sweeper", getRolesSweeper("role_sweeper"))
resource.AddTestSweepers("user_sweeper", getUsersSweeper("user_sweeper"))
Expand Down
Loading