Skip to content

Commit

Permalink
Add timeouts to snowflake_execute
Browse files Browse the repository at this point in the history
References: #3334
  • Loading branch information
sfc-gh-asawicki committed Jan 14, 2025
1 parent f70f4d9 commit 620ac8c
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 9 deletions.
5 changes: 5 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Resource monitor in not currently listed as option in `GRANT OWNERSHIP` document

References: [#3318](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/3318)

### Timeouts in `snowflake_execute`
By default, resource operation timeouts after 20 minutes ([reference](https://developer.hashicorp.com/terraform/plugin/sdkv2/resources/retries-and-customizable-timeouts#default-timeouts-and-deadline-exceeded-errors)). Because of generic nature of `snowflake_execute`, we decided to bump its default timeouts to 60 minutes; We also allowed setting them on the resource config level (following [official documentation](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts)).

References: [#3334](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/3334)

## v1.0.0 ➞ v1.0.1

### Fixes in account parameters
Expand Down
11 changes: 11 additions & 0 deletions docs/resources/execute.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,23 @@ resource "snowflake_execute" "test" {
### Optional

- `query` (String) Optional SQL statement to do a read. Invoked on every resource refresh and every time it is changed.
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `id` (String) The ID of this resource.
- `query_results` (List of Map of String) List of key-value maps (text to text) retrieved after executing read query. Will be empty if the query results in an error.

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

Optional:

- `create` (String)
- `delete` (String)
- `read` (String)
- `update` (String)

## Import

Import is supported using the following syntax:
Expand Down
17 changes: 10 additions & 7 deletions pkg/resources/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import (
"context"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -58,6 +55,12 @@ func Execute() *schema.Resource {
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Read: schema.DefaultTimeout(60 * time.Minute),
Update: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},

Description: "Resource allowing execution of ANY SQL statement.",

Expand Down
74 changes: 72 additions & 2 deletions pkg/resources/execute_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"strings"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
Expand Down Expand Up @@ -865,3 +864,74 @@ func testAccCheckDatabaseExistence(t *testing.T, id sdk.AccountObjectIdentifier,
return nil
}
}

// Result of https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/3334.
func TestAcc_Execute_gh3334_allTimeouts(t *testing.T) {
resourceName := "snowflake_execute.test"
createConfigVariables := func() map[string]config.Variable {
return map[string]config.Variable{
"execute": config.StringVariable("CALL SYSTEM$WAIT(5, 'SECONDS');"),
"revert": config.StringVariable("select 2"),
"query": config.StringVariable("select 3"),
"create_timeout": config.StringVariable("1m"),
"read_timeout": config.StringVariable("31m"),
"update_timeout": config.StringVariable("32m"),
"delete_timeout": config.StringVariable("33m"),
}
}

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: nil,
Steps: []resource.TestStep{
{
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Execute_withTimeouts"),
ConfigVariables: createConfigVariables(),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{plancheck.ExpectNonEmptyPlan()},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "timeouts.create", "1m"),
resource.TestCheckResourceAttr(resourceName, "timeouts.read", "31m"),
resource.TestCheckResourceAttr(resourceName, "timeouts.update", "32m"),
resource.TestCheckResourceAttr(resourceName, "timeouts.delete", "33m"),
),
},
},
})
}

// Result of https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/3334.
func TestAcc_Execute_gh3334_longRunningCreate(t *testing.T) {
createConfigVariables := func() map[string]config.Variable {
return map[string]config.Variable{
"execute": config.StringVariable("CALL SYSTEM$WAIT(15, 'SECONDS');"),
"revert": config.StringVariable("select 2"),
"query": config.StringVariable("select 3"),
"create_timeout": config.StringVariable("5s"),
}
}

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: nil,
Steps: []resource.TestStep{
{
ConfigDirectory: acc.ConfigurationDirectory("TestAcc_Execute_withTimeouts"),
ConfigVariables: createConfigVariables(),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{plancheck.ExpectNonEmptyPlan()},
},
ExpectError: regexp.MustCompile("Error: context deadline exceeded"),
},
},
})
}
12 changes: 12 additions & 0 deletions pkg/resources/testdata/TestAcc_Execute_withTimeouts/test.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "snowflake_execute" "test" {
execute = var.execute
revert = var.revert
query = var.query

timeouts {
create = var.create_timeout
read = var.read_timeout
update = var.update_timeout
delete = var.delete_timeout
}
}
31 changes: 31 additions & 0 deletions pkg/resources/testdata/TestAcc_Execute_withTimeouts/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "execute" {
type = string
}

variable "revert" {
type = string
}

variable "query" {
type = string
}

variable "create_timeout" {
type = string
default = "21m"
}

variable "read_timeout" {
type = string
default = "22m"
}

variable "update_timeout" {
type = string
default = "23m"
}

variable "delete_timeout" {
type = string
default = "24m"
}

0 comments on commit 620ac8c

Please sign in to comment.