Skip to content

Commit

Permalink
Merge branch 'main' into grants-migration-guide
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak authored Feb 6, 2024
2 parents f45c1eb + 2de942a commit 9672d5a
Show file tree
Hide file tree
Showing 36 changed files with 2,330 additions and 1,328 deletions.
40 changes: 40 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
# Changelog

## [0.85.0](https://github.com/Snowflake-Labs/terraform-provider-snowflake/compare/v0.84.1...v0.85.0) (2024-02-01)


### 🎉 **What's new:**

* Add API integration to the SDK ([#2409](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2409)) ([23acda5](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/23acda5dba9c8378f3b5631446d380a27cf1732c))
* add application to sdk ([#2350](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2350)) ([de97ad8](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/de97ad84db925b62ab10046e0893a5c285a26d67))
* add external funcs to sdk ([#2440](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2440)) ([c8cf09b](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/c8cf09b2af605dc373a138e6ca6863b5546303d5))
* Add grant privileges to share resource ([#2447](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2447)) ([d8241a5](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/d8241a5cc76ea7b929abdada81cf6929b5f6ad9e))
* Add materialized view to the SDK ([#2403](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2403)) ([a5ce699](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/a5ce69920328cce899260249d319ff7726ae3911))
* Add notification integration to the SDK ([#2412](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2412)) ([d84240c](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/d84240cda369ed9106c7cb3e3eedf85b8d1fa944))
* add sequences to sdk ([#2351](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2351)) ([d2e5ffd](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/d2e5ffd5405f10ff30c5ad9f7cd58bd54a5cc028))
* add snowflake grant privileges to account role ([#2365](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2365)) ([e3d086e](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/e3d086eddc05e0d4963234f82e09e174a018bb08))
* add streamlits to sdk ([#2400](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2400)) ([129d24c](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/129d24c00fa244d1401cb2169b5b7fb0ba6c465c))
* add-call-with to sdk ([#2337](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2337)) ([ebcd1bc](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/ebcd1bc40d554abe6863b67d2ab76f2d992dfb32))
* stages migration follow-up ([#2372](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2372)) ([3939dbe](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/3939dbe2f9189968c087a883ed97dd3b7350787f))
* Use API integration from SDK ([#2429](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2429)) ([1ccc864](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/1ccc8641106a3ceb4de813ce7c0e5077ead5272e))
* Use managed account from the SDK ([#2420](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2420)) ([3aaa080](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/3aaa08071a14f820e08751cc7b1e8bef5db16e30))
* Use materialized views and views from SDK ([#2448](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2448)) ([dc66d02](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/dc66d02304a99a7cb152e91a8e942587cab7e60f))
* Use notification integration from sdk ([#2445](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2445)) ([e8915cc](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/e8915ccb99eeec1f0ac5777fe80be7ef443d8f5c))
* use roles from the SDK ([#2405](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2405)) ([c645b4d](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/c645b4d0e2036d932766480e9c1e0334ef79c16e))
* Use row access policy from SDK ([#2428](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2428)) ([119af5e](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/119af5ea74bb219ae822962096e6220ed00f5910))
* Use SDK in the storage integration ([#2380](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2380)) ([ce0741c](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/ce0741ce226be9464407b549e90cb179b0fe5880))
* use sequence from sdk and add ordering attr ([#2419](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2419)) ([973b8f7](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/973b8f76a8ed1540bfd948ba8cb57c212c0d4abc)), closes [#2387](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2387)
* Use stage from sdk ([#2427](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2427)) ([c17effd](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/c17effd16ccd77ba4c5d45f43dcc53a9f11601c6))


### 🔧 **Misc**

* add missing deprecation message ([#2451](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2451)) ([77de569](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/77de5694f73e5ad1443bb99407d2e8aec9a87320))


### 🐛 **Bug fixes:**

* account role test ([#2422](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2422)) ([c1b47d1](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/c1b47d1ade4b198b5bf14dc32162d34797a3b344))
* Adjust tests after Snowflake behavior change ([#2404](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2404)) ([8c03ffb](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/8c03ffb0430445c903168da9706e1ce2630675da))
* app-pkg unset ([#2399](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2399)) ([fedb1df](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/fedb1df2a731d139d68d2284bf3be47fcc4d0115))
* Fix some bugs ([#2421](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2421)) ([dec7cd9](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/dec7cd9e199ac8658f5c939f811686ba9f5e2e21)), closes [#2358](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2358) [#2369](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2369) [#2329](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2329)
* snowflake_grant_privileges_to_role read ([#2424](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2424)) ([5385cec](https://github.com/Snowflake-Labs/terraform-provider-snowflake/commit/5385cec3e5c03d2dbff762b63523bdddee8632d3))

## [0.85.0](https://github.com/Snowflake-Labs/terraform-provider-snowflake/compare/v0.84.1...v0.85.0) (2024-01-22)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Optional:
- `comment` (String) Column comment
- `default` (Block List, Max: 1) Defines the column default value; note due to limitations of Snowflake's ALTER TABLE ADD/MODIFY COLUMN updates to default will not be applied (see [below for nested schema](#nestedblock--column--default))
- `identity` (Block List, Max: 1) Defines the identity start/step values for a column. **Note** Identity/default are mutually exclusive. (see [below for nested schema](#nestedblock--column--identity))
- `masking_policy` (String) Masking policy to apply on column
- `masking_policy` (String) Masking policy to apply on column. It has to be a fully qualified name.
- `nullable` (Boolean) Whether this column can contain null values. **Note**: Depending on your Snowflake version, the default value will not suffice if this column is used in a primary key constraint.

<a id="nestedblock--column--default"></a>
Expand Down
44 changes: 22 additions & 22 deletions pkg/datasources/tables.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package datasources

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

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake"
"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 Down Expand Up @@ -58,38 +58,38 @@ func Tables() *schema.Resource {

func ReadTables(d *schema.ResourceData, meta interface{}) error {
db := meta.(*sql.DB)
ctx := context.Background()
client := sdk.NewClientFromDB(db)
databaseName := d.Get("database").(string)
schemaName := d.Get("schema").(string)

currentTables, err := snowflake.ListTables(databaseName, schemaName, 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] tables in schema (%s) not found", d.Id())
d.SetId("")
return nil
} else if err != nil {
log.Printf("[DEBUG] unable to parse tables in schema (%s)", d.Id())
schemaId := sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)
extractedTables, err := client.Tables.Show(ctx, sdk.NewShowTableRequest().WithIn(
&sdk.In{Schema: schemaId},
))
if err != nil {
log.Printf("[DEBUG] failed when searching tables in schema (%s), err = %s", schemaId.FullyQualifiedName(), err.Error())
d.SetId("")
return nil
}

tables := []map[string]interface{}{}
tables := make([]map[string]any, 0)

for _, table := range currentTables {
tableMap := map[string]interface{}{}

if table.IsExternal.String == "Y" {
for _, extractedTable := range extractedTables {
if extractedTable.IsExternal {
continue
}

tableMap["name"] = table.TableName.String
tableMap["database"] = table.DatabaseName.String
tableMap["schema"] = table.SchemaName.String
tableMap["comment"] = table.Comment.String
table := map[string]any{
"name": extractedTable.Name,
"database": extractedTable.DatabaseName,
"schema": extractedTable.SchemaName,
"comment": extractedTable.Comment,
}

tables = append(tables, tableMap)
tables = append(tables, table)
}

d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName))
d.SetId(helpers.EncodeSnowflakeID(databaseName, schemaName))
return d.Set("tables", tables)
}
9 changes: 8 additions & 1 deletion pkg/datasources/tables_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import (
"strings"
"testing"

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

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAcc_Tables(t *testing.T) {
Expand All @@ -16,7 +19,11 @@ func TestAcc_Tables(t *testing.T) {
stageName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
externalTableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Expand Down
3 changes: 2 additions & 1 deletion pkg/resources/database_grant.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func DatabaseGrant() *TerraformGrantResource {
Delete: DeleteDatabaseGrant,
Update: UpdateDatabaseGrant,

Schema: databaseGrantSchema,
DeprecationMessage: "This resource is deprecated and will be removed in a future major version release. Please use snowflake_grant_privileges_to_account_role instead.",
Schema: databaseGrantSchema,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), helpers.IDDelimiter)
Expand Down
6 changes: 3 additions & 3 deletions pkg/resources/materialized_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ func UpdateMaterializedView(d *schema.ResourceData, meta interface{}) error {
unsetRequest := sdk.NewMaterializedViewUnsetRequest()

if d.HasChange("comment") {
comment := d.Get("comment")
if c := comment.(string); c == "" {
comment := d.Get("comment").(string)
if comment == "" {
runUnsetStatement = true
unsetRequest.WithComment(sdk.Bool(true))
} else {
runSetStatement = true
setRequest.WithComment(sdk.String(d.Get("comment").(string)))
setRequest.WithComment(sdk.String(comment))
}
}
if d.HasChange("is_secure") {
Expand Down
52 changes: 29 additions & 23 deletions pkg/resources/materialized_view_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func TestAcc_MaterializedView(t *testing.T) {
func TestAcc_MaterializedView_Tags(t *testing.T) {
tableName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
viewName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
tag1Name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
tag2Name := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

queryEscaped := fmt.Sprintf("SELECT ID FROM \\\"%s\\\"", tableName)

Expand All @@ -115,20 +117,20 @@ func TestAcc_MaterializedView_Tags(t *testing.T) {
Steps: []resource.TestStep{
// create tags
{
Config: materializedViewConfigWithTags(acc.TestWarehouseName, tableName, viewName, queryEscaped, acc.TestDatabaseName, acc.TestSchemaName, "test_tag"),
Config: materializedViewConfigWithTags(acc.TestWarehouseName, tableName, viewName, queryEscaped, acc.TestDatabaseName, acc.TestSchemaName, "test_tag", tag1Name, tag2Name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "name", viewName),
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "tag.#", "1"),
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "tag.0.name", "tag1"),
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "tag.0.name", tag1Name),
),
},
// update tags
{
Config: materializedViewConfigWithTags(acc.TestWarehouseName, tableName, viewName, queryEscaped, acc.TestDatabaseName, acc.TestSchemaName, "test_tag_2"),
Config: materializedViewConfigWithTags(acc.TestWarehouseName, tableName, viewName, queryEscaped, acc.TestDatabaseName, acc.TestSchemaName, "test_tag_2", tag1Name, tag2Name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "name", viewName),
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "tag.#", "1"),
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "tag.0.name", "tag2"),
resource.TestCheckResourceAttr("snowflake_materialized_view.test", "tag.0.name", tag2Name),
),
},
// IMPORT
Expand Down Expand Up @@ -209,12 +211,12 @@ resource "snowflake_materialized_view" "test" {
`, tableName, databaseName, schemaName, viewName, comment, databaseName, schemaName, warehouseName, isSecure, orReplace, q)
}

func materializedViewConfigWithTags(warehouseName string, tableName string, viewName string, q string, databaseName string, schemaName string, tag string) string {
func materializedViewConfigWithTags(warehouseName string, tableName string, viewName string, q string, databaseName string, schemaName string, tag string, tag1Name string, tag2Name string) string {
return fmt.Sprintf(`
resource "snowflake_table" "test" {
name = "%s"
database = "%s"
schema = "%s"
name = "%[1]s"
database = "%[2]s"
schema = "%[3]s"
column {
name = "ID"
Expand All @@ -223,32 +225,36 @@ resource "snowflake_table" "test" {
}
resource "snowflake_tag" "test_tag" {
name = "tag1"
database = "%s"
schema = "%s"
name = "%[8]s"
database = "%[2]s"
schema = "%[3]s"
}
resource "snowflake_tag" "test_tag_2" {
name = "tag2"
database = "%s"
schema = "%s"
name = "%[9]s"
database = "%[2]s"
schema = "%[3]s"
}
resource "snowflake_materialized_view" "test" {
name = "%s"
database = "%s"
schema = "%s"
warehouse = "%s"
statement = "%s"
name = "%[4]s"
database = "%[2]s"
schema = "%[3]s"
warehouse = "%[5]s"
statement = "%[6]s"
tag {
name = snowflake_tag.%s.name
schema = snowflake_tag.%s.schema
database = snowflake_tag.%s.database
name = snowflake_tag.%[7]s.name
schema = snowflake_tag.%[7]s.schema
database = snowflake_tag.%[7]s.database
value = "some_value"
}
depends_on = [
snowflake_table.test
]
}
`, tableName, databaseName, schemaName, databaseName, schemaName, databaseName, schemaName, viewName, databaseName, schemaName, warehouseName, q, tag, tag, tag)
`, tableName, databaseName, schemaName, viewName, warehouseName, q, tag, tag1Name, tag2Name)
}

func testAccCheckMaterializedViewDestroy(s *terraform.State) error {
Expand Down
8 changes: 2 additions & 6 deletions pkg/resources/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"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 @@ -131,14 +129,12 @@ func CreateStream(d *schema.ResourceData, meta interface{}) error {
}
tableId := tableObjectIdentifier.(sdk.SchemaObjectIdentifier)

tq := snowflake.NewTableBuilder(tableId.Name(), tableId.DatabaseName(), tableId.SchemaName()).Show()
tableRow := snowflake.QueryRow(db, tq)
t, err := snowflake.ScanTable(tableRow)
table, err := client.Tables.ShowByID(ctx, tableId)
if err != nil {
return err
}

if t.IsExternal.String == "Y" {
if table.IsExternal {
req := sdk.NewCreateStreamOnExternalTableRequest(id, tableId)
if insertOnly {
req.WithInsertOnly(sdk.Bool(true))
Expand Down
Loading

0 comments on commit 9672d5a

Please sign in to comment.