Skip to content

Commit

Permalink
chore: Migrate Warehouse resource + datasource to new SDK (#1792)
Browse files Browse the repository at this point in the history
* Allow rendering parameters with aliased string types.

* Allow rendering lists of strings.

* Implement Warehouse sdk.

* Document available clause tags.

* Use SDK for warehouse resource.

* Remove old Warehouse sdk.

* Use SDK with warehouse datasource.

* Update docs
  • Loading branch information
sfc-gh-ngaberel authored May 12, 2023
1 parent 88a58d6 commit a14b994
Show file tree
Hide file tree
Showing 19 changed files with 1,582 additions and 491 deletions.
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

0 comments on commit a14b994

Please sign in to comment.