Skip to content

Commit

Permalink
Fix external tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Jan 10, 2024
1 parent da83882 commit 35f2551
Show file tree
Hide file tree
Showing 19 changed files with 643 additions and 131 deletions.
1 change: 1 addition & 0 deletions docs/resources/external_table.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ resource "snowflake_external_table" "external_table" {
- `partition_by` (List of String) Specifies any partition columns to evaluate for the external table.
- `pattern` (String) Specifies the file names and/or paths on the external stage to match.
- `refresh_on_create` (Boolean) Specifies weather to refresh when an external table is created.
- `table_format` (String) Identifies the external table table type. For now, only "delta" for Delta Lake table format is supported.
- `tag` (Block List, Deprecated) Definitions of a tag to associate with the resource. (see [below for nested schema](#nestedblock--tag))

### Read-Only
Expand Down
58 changes: 16 additions & 42 deletions pkg/resources/external_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"log"

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

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -30,13 +32,12 @@ var externalTableSchema = map[string]*schema.Schema{
ForceNew: true,
Description: "The database in which to create the external table.",
},
// TODO: Could be a string that we would validate as always "delta" (could be easy to add another type if snowflake introduces one)
"table_format_delta": {
Type: schema.TypeBool,
Required: true,
"table_format": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: `Identifies the external table as referencing a Delta Lake on the cloud storage location. A Delta Lake on Amazon S3, Google Cloud Storage, or Microsoft Azure cloud storage is supported.`,
RequiredWith: []string{"user_specified_partitions"},
Description: `Identifies the external table table type. For now, only "delta" for Delta Lake table format is supported.`,
ValidateFunc: validation.StringInSlice([]string{"delta"}, true),
},
"column": {
Type: schema.TypeList,
Expand Down Expand Up @@ -92,18 +93,11 @@ var externalTableSchema = map[string]*schema.Schema{
ForceNew: true,
Description: "Specifies the aws sns topic for the external table.",
},
"user_specified_partitions": {
Type: schema.TypeBool,
"partition_by": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Description: "Enables to manage partitions manually and perform updates instead of recreating table on partition_by change.",
},
"partition_by": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
//ForceNew: true,
// TODO: Update on user_specified_partitions = true and force new on false
Description: "Specifies any partition columns to evaluate for the external table.",
},
"refresh_on_create": {
Expand Down Expand Up @@ -175,15 +169,11 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {
for key, val := range col.(map[string]any) {
columnDef[key] = val.(string)
}

name := columnDef["name"]
dataTypeString := columnDef["type"]
dataType, err := sdk.ToDataType(dataTypeString)
if err != nil {
return fmt.Errorf(`failed to parse datatype: %s`, dataTypeString)
}
as := columnDef["as"]
columnRequests[i] = sdk.NewExternalTableColumnRequest(name, dataType, as)
columnRequests[i] = sdk.NewExternalTableColumnRequest(
columnDef["name"],
sdk.DataType(columnDef["type"]),
columnDef["as"],
)
}
autoRefresh := sdk.Bool(d.Get("auto_refresh").(bool))
refreshOnCreate := sdk.Bool(d.Get("refresh_on_create").(bool))
Expand Down Expand Up @@ -219,30 +209,15 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {
}

switch {
case d.Get("table_format_delta").(bool):
case d.Get("table_format").(string) == "delta":
err := client.ExternalTables.CreateDeltaLake(
ctx,
sdk.NewCreateDeltaLakeExternalTableRequest(id, location).
WithRawFileFormat(&fileFormat).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
WithAutoRefresh(autoRefresh).
WithCopyGrants(copyGrants).
WithComment(comment).
WithTag(tagAssociationRequests),
)
if err != nil {
return err
}
case d.Get("user_specified_partitions").(bool):
err := client.ExternalTables.CreateWithManualPartitioning(
ctx,
sdk.NewCreateWithManualPartitioningExternalTableRequest(id, location).
WithRawFileFormat(&fileFormat).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRawFileFormat(sdk.String(fileFormat)).
WithCopyGrants(copyGrants).
WithComment(comment).
WithTag(tagAssociationRequests),
Expand All @@ -254,7 +229,6 @@ func CreateExternalTable(d *schema.ResourceData, meta any) error {
err := client.ExternalTables.Create(
ctx,
sdk.NewCreateExternalTableRequest(id, location).
WithRawFileFormat(&fileFormat).
WithColumns(columnRequests).
WithPartitionBy(partitionBy).
WithRefreshOnCreate(refreshOnCreate).
Expand Down
Loading

0 comments on commit 35f2551

Please sign in to comment.