Skip to content

Commit

Permalink
Changes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed May 28, 2024
1 parent c52566d commit f0823fc
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 42 deletions.
14 changes: 8 additions & 6 deletions docs/resources/secondary_database.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ resource "snowflake_secondary_database" "test" {
value = 20
}
external_volume = "external_volume_name"
catalog = "catalog_name"
default_ddl_collation = "en_US"
log_level = "OFF"
trace_level = "OFF"
comment = "A secondary database"
external_volume = "external_volume_name"
catalog = "catalog_name"
replace_invalid_characters = false
default_ddl_collation = "en_US"
storage_serialization_policy = "OPTIMIZED"
log_level = "OFF"
trace_level = "OFF"
comment = "A secondary database"
}
```

Expand Down
24 changes: 13 additions & 11 deletions docs/resources/shared_database.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,19 @@ resource "snowflake_grant_privileges_to_share" "test" {
# 2. Creating shared database
resource "snowflake_shared_database" "test" {
provider = secondary_account
depends_on = [snowflake_grant_privileges_to_share.test]
name = snowflake_database.test.name # shared database should have the same as the "imported" one
from_share = "<primary_account_organization_name>.<primary_account_name>.${snowflake_share.test.name}"
is_transient = false
external_volume = "external_volume_name"
catalog = "catalog_name"
default_ddl_collation = "en_US"
log_level = "OFF"
trace_level = "OFF"
comment = "A shared database"
provider = secondary_account
depends_on = [snowflake_grant_privileges_to_share.test]
name = snowflake_database.test.name # shared database should have the same as the "imported" one
from_share = "<primary_account_organization_name>.<primary_account_name>.${snowflake_share.test.name}"
is_transient = false
external_volume = "external_volume_name"
catalog = "catalog_name"
replace_invalid_characters = false
default_ddl_collation = "en_US"
storage_serialization_policy = "OPTIMIZED"
log_level = "OFF"
trace_level = "OFF"
comment = "A shared database"
}
```

Expand Down
14 changes: 8 additions & 6 deletions examples/resources/snowflake_secondary_database/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ resource "snowflake_secondary_database" "test" {
value = 20
}

external_volume = "external_volume_name"
catalog = "catalog_name"
default_ddl_collation = "en_US"
log_level = "OFF"
trace_level = "OFF"
comment = "A secondary database"
external_volume = "external_volume_name"
catalog = "catalog_name"
replace_invalid_characters = false
default_ddl_collation = "en_US"
storage_serialization_policy = "OPTIMIZED"
log_level = "OFF"
trace_level = "OFF"
comment = "A secondary database"
}
24 changes: 13 additions & 11 deletions examples/resources/snowflake_shared_database/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ resource "snowflake_grant_privileges_to_share" "test" {

# 2. Creating shared database
resource "snowflake_shared_database" "test" {
provider = secondary_account
depends_on = [snowflake_grant_privileges_to_share.test]
name = snowflake_database.test.name # shared database should have the same as the "imported" one
from_share = "<primary_account_organization_name>.<primary_account_name>.${snowflake_share.test.name}"
is_transient = false
external_volume = "external_volume_name"
catalog = "catalog_name"
default_ddl_collation = "en_US"
log_level = "OFF"
trace_level = "OFF"
comment = "A shared database"
provider = secondary_account
depends_on = [snowflake_grant_privileges_to_share.test]
name = snowflake_database.test.name # shared database should have the same as the "imported" one
from_share = "<primary_account_organization_name>.<primary_account_name>.${snowflake_share.test.name}"
is_transient = false
external_volume = "external_volume_name"
catalog = "catalog_name"
replace_invalid_characters = false
default_ddl_collation = "en_US"
storage_serialization_policy = "OPTIMIZED"
log_level = "OFF"
trace_level = "OFF"
comment = "A shared database"
}
152 changes: 152 additions & 0 deletions pkg/resources/custom_diffs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package resources_test

import (
"context"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNestedValueComputedIf(t *testing.T) {
customDiff := resources.NestedValueComputedIf(
"nested_value",
func(client *sdk.Client) (*sdk.Parameter, error) {
return &sdk.Parameter{
Key: "Parameter",
Value: "snow-value",
}, nil
},
func(v any) string { return v.(string) },
)
providerConfig := createProviderWithNestedValueAndCustomDiff(t, schema.TypeString, customDiff)

t.Run("value set in the configuration and state", func(t *testing.T) {
diff := calculateDiff(t, providerConfig, cty.MapVal(map[string]cty.Value{
"nested_value": cty.ListVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"value": cty.NumberIntVal(123),
}),
}),
}), map[string]any{
"nested_value": []any{
map[string]any{
"value": 123,
},
},
})
assert.False(t, diff.Attributes["nested_value.#"].NewComputed)
})

t.Run("value set only in the configuration", func(t *testing.T) {
diff := calculateDiff(t, providerConfig, cty.MapVal(map[string]cty.Value{
"nested_value": cty.ListVal([]cty.Value{
cty.MapVal(map[string]cty.Value{
"value": cty.NumberIntVal(123),
}),
}),
}), map[string]any{})
assert.True(t, diff.Attributes["nested_value.#"].NewComputed)
})

t.Run("value set in the state and not equals with parameter", func(t *testing.T) {
diff := calculateDiff(t, providerConfig, cty.MapValEmpty(cty.Type{}), map[string]any{
"nested_value": []any{
map[string]any{
"value": "value-to-change",
},
},
})
assert.True(t, diff.Attributes["nested_value.#"].NewComputed)
})

t.Run("value set in the state and equals with parameter", func(t *testing.T) {
diff := calculateDiff(t, providerConfig, cty.MapValEmpty(cty.Type{}), map[string]any{
"nested_value": []any{
map[string]any{
"value": "snow-value",
},
},
})
assert.False(t, diff.Attributes["nested_value.#"].NewComputed)
})
}

func TestNestedIntValueAccountObjectComputedIf(t *testing.T) {
providerConfig := createProviderWithNestedValueAndCustomDiff(t, schema.TypeInt, resources.NestedIntValueAccountObjectComputedIf("nested_value", sdk.AccountParameterDataRetentionTimeInDays))

t.Run("different value than on the Snowflake side", func(t *testing.T) {
diff := calculateDiff(t, providerConfig, cty.MapValEmpty(cty.Type{}), map[string]any{
"nested_value": []any{
map[string]any{
"value": 999, // value outside of valid range
},
},
})
assert.True(t, diff.Attributes["nested_value.#"].NewComputed)
})

t.Run("same value as in Snowflake", func(t *testing.T) {
dataRetentionTimeInDays, err := acc.Client(t).Parameters.ShowAccountParameter(context.Background(), sdk.AccountParameterDataRetentionTimeInDays)
require.NoError(t, err)

diff := calculateDiff(t, providerConfig, cty.MapValEmpty(cty.Type{}), map[string]any{
"nested_value": []any{
map[string]any{
"value": dataRetentionTimeInDays.Value,
},
},
})
assert.False(t, diff.Attributes["nested_value.#"].NewComputed)
})
}

func createProviderWithNestedValueAndCustomDiff(t *testing.T, valueType schema.ValueType, customDiffFunc schema.CustomizeDiffFunc) *schema.Provider {
t.Helper()
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"test": {
Schema: map[string]*schema.Schema{
"nested_value": {
Type: schema.TypeList,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"value": {
Type: valueType,
Required: true,
},
},
},
Computed: true,
Optional: true,
},
},
CustomizeDiff: customDiffFunc,
},
},
}
}

func calculateDiff(t *testing.T, providerConfig *schema.Provider, rawConfigValue cty.Value, stateValue map[string]any) *terraform.InstanceDiff {
t.Helper()
diff, err := providerConfig.ResourcesMap["test"].Diff(
context.Background(),
&terraform.InstanceState{
RawConfig: rawConfigValue,
},
&terraform.ResourceConfig{
Config: stateValue,
},
&provider.Context{Client: acc.Client(t)},
)
require.NoError(t, err)
return diff
}
14 changes: 6 additions & 8 deletions pkg/sdk/testint/databases_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,11 @@ func TestInt_DatabasesCreateSecondary(t *testing.T) {
secondaryClient := testSecondaryClient(t)
ctx := testContext(t)

// Database name will be shared between two accounts
databaseId := testClientHelper().Ids.RandomAccountObjectIdentifier()

sharedDatabase, sharedDatabaseCleanup := secondaryTestClientHelper().Database.CreateDatabaseWithName(t, databaseId.Name())
sharedDatabase, sharedDatabaseCleanup := secondaryTestClientHelper().Database.CreateDatabase(t)
t.Cleanup(sharedDatabaseCleanup)

databaseId := sharedDatabase.ID()

err := secondaryClient.Databases.AlterReplication(ctx, sharedDatabase.ID(), &sdk.AlterDatabaseReplicationOptions{
EnableReplication: &sdk.EnableReplication{
ToAccounts: []sdk.AccountIdentifier{
Expand Down Expand Up @@ -366,12 +365,11 @@ func TestInt_DatabasesAlter(t *testing.T) {
shareTest, shareCleanup := secondaryTestClientHelper().Share.CreateShare(t)
t.Cleanup(shareCleanup)

// Database name will be shared between two accounts
databaseId := testClientHelper().Ids.RandomAccountObjectIdentifier()

sharedDatabase, sharedDatabaseCleanup := secondaryTestClientHelper().Database.CreateDatabaseWithName(t, databaseId.Name())
sharedDatabase, sharedDatabaseCleanup := secondaryTestClientHelper().Database.CreateDatabase(t)
t.Cleanup(sharedDatabaseCleanup)

databaseId := sharedDatabase.ID()

err := secondaryClient.Grants.GrantPrivilegeToShare(ctx, []sdk.ObjectPrivilege{sdk.ObjectPrivilegeUsage}, &sdk.ShareGrantOn{
Database: sharedDatabase.ID(),
}, shareTest.ID())
Expand Down

0 comments on commit f0823fc

Please sign in to comment.