Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: parser and secret tests #3192

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
adressed comments from connection fix
sfc-gh-fbudzynski committed Nov 12, 2024
commit a677547ac24c7b3f4d470b6f5fa3541341581fc0
2 changes: 1 addition & 1 deletion docs/resources/primary_connection.md
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@ resource "snowflake_primary_connection" "complete" {
}
```

-> **Note** Instead of using fully_qualified_name, you can reference objects managed outside Terraform by constructing a correct ID, consult [identifiers guide](./docs/guides/identifiers#new-computed-fully-qualified-name-field-in-resources).
-> **Note** Instead of using fully_qualified_name, you can reference objects managed outside Terraform by constructing a correct ID, consult [identifiers guide](../docs/guides/identifiers#new-computed-fully-qualified-name-field-in-resources).

-> **Note** To demote [`snowflake_primary_connection`](./primary_connection) to [`snowflake_secondary_connection`](./secondary_connection), resources need to be migrated manually. For guidance on removing and importing resources into the state check [resource migration](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/docs/technical-documentation/resource_migration.md). Remove the resource from the state, then recreate it in manually using:
```
19 changes: 6 additions & 13 deletions pkg/datasources/secrets_acceptance_test.go
Original file line number Diff line number Diff line change
@@ -20,13 +20,6 @@ import (
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

const (
secretWithClientCredentials = "snowflake_secret_with_client_credentials"
secretWithAuthorizationCodeGrant = "snowflake_secret_with_authorization_code_grant"
secretWithBasicAuthentication = "snowflake_secret_with_basic_authentication"
secretWithGenericString = "snowflake_secret_with_generic_string"
)

func TestAcc_Secrets_WithClientCredentials(t *testing.T) {
_ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance)
acc.TestAccPreCheck(t)
@@ -42,7 +35,7 @@ func TestAcc_Secrets_WithClientCredentials(t *testing.T) {

secretModel := model.SecretWithClientCredentials("test", integrationId.Name(), id.DatabaseName(), id.SchemaName(), id.Name(), []string{"username", "test_scope"})

dataSecretsClientCredentials := accConfig.FromModel(t, secretModel) + secretsData(secretWithClientCredentials, id.DatabaseId().FullyQualifiedName())
dataSecretsClientCredentials := accConfig.FromModel(t, secretModel) + secretsData(secretModel, id)

dsName := "data.snowflake_secrets.test"
resource.Test(t, resource.TestCase{
@@ -97,7 +90,7 @@ func TestAcc_Secrets_WithAuthorizationCodeGrant(t *testing.T) {

secretModel := model.SecretWithAuthorizationCodeGrant("test", integrationId.Name(), id.DatabaseName(), id.SchemaName(), id.Name(), "test_token", time.Now().Add(24*time.Hour).Format(time.DateTime)).WithComment("test_comment")

dataSecretsAuthorizationCode := accConfig.FromModel(t, secretModel) + secretsData(secretWithAuthorizationCodeGrant, id.DatabaseId().FullyQualifiedName())
dataSecretsAuthorizationCode := accConfig.FromModel(t, secretModel) + secretsData(secretModel, id)

dsName := "data.snowflake_secrets.test"
resource.Test(t, resource.TestCase{
@@ -138,7 +131,7 @@ func TestAcc_Secrets_WithBasicAuthentication(t *testing.T) {
id := acc.TestClient().Ids.RandomSchemaObjectIdentifier()

secretModel := model.SecretWithBasicAuthentication("test", id.DatabaseName(), id.Name(), "test_passwd", id.SchemaName(), "test_username")
dataSecretsAuthorizationCode := accConfig.FromModel(t, secretModel) + secretsData(secretWithBasicAuthentication, id.DatabaseId().FullyQualifiedName())
dataSecretsAuthorizationCode := accConfig.FromModel(t, secretModel) + secretsData(secretModel, id)

dsName := "data.snowflake_secrets.test"
resource.Test(t, resource.TestCase{
@@ -179,7 +172,7 @@ func TestAcc_Secrets_WithGenericString(t *testing.T) {

secretModel := model.SecretWithGenericString("test", id.DatabaseName(), id.Name(), id.SchemaName(), "test_secret_string")

dataSecretsAuthorizationCode := accConfig.FromModel(t, secretModel) + secretsData(secretWithGenericString, id.DatabaseId().FullyQualifiedName())
dataSecretsAuthorizationCode := accConfig.FromModel(t, secretModel) + secretsData(secretModel, id)

dsName := "data.snowflake_secrets.test"
resource.Test(t, resource.TestCase{
@@ -216,14 +209,14 @@ func TestAcc_Secrets_WithGenericString(t *testing.T) {
})
}

func secretsData(secretResourceName string, inDatabaseName string) string {
func secretsData(secretModel accConfig.ResourceModel, secretId sdk.SchemaObjectIdentifier) string {
return fmt.Sprintf(`
data "snowflake_secrets" "test" {
depends_on = [%s.test]
in {
database = %s
}
}`, secretResourceName, inDatabaseName)
}`, secretModel.Resource(), secretId.DatabaseId().FullyQualifiedName())
}

func TestAcc_Secrets_Filtering(t *testing.T) {
56 changes: 46 additions & 10 deletions pkg/sdk/parsers_test.go
Original file line number Diff line number Diff line change
@@ -168,6 +168,42 @@ func TestParseCommaSeparatedSchemaObjectIdentifierArray(t *testing.T) {
}
}

func TestParseCommaSeparatedSchemaObjectIdentifierArray_Invalid(t *testing.T) {
testCases := []struct {
Name string
Value string
Error string
}{
{
Name: "bad quotes",
Value: `["a]`,
Error: "unable to read identifier: \"a, err = parse error on line 1, column 3: extraneous or missing \" in quoted-field",
},
{
Name: "missing parts",
Value: "[a.b.c, a.b]",
Error: "unexpected number of parts 2 in identifier a.b, expected 3 in a form of \"<database_name>.<schema_name>.<schema_object_name>\"",
},
{
Name: "too many parts",
Value: "[a.b.c, a.b.c.d]",
Error: "unexpected number of parts 4 in identifier a.b.c.d, expected 3 in a form of \"<database_name>.<schema_name>.<schema_object_name>\"",
},
{
Name: "missing parts - empty id",
Value: "[a.b.c, ]",
Error: "incompatible identifier",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
_, err := ParseCommaSeparatedSchemaObjectIdentifierArray(tc.Value)
require.ErrorContains(t, err, tc.Error)
})
}
}

func TestParseCommaSeparatedAccountIdentifierArray(t *testing.T) {
testCases := []struct {
Name string
@@ -230,37 +266,37 @@ func TestParseCommaSeparatedAccountIdentifierArray(t *testing.T) {
}
}

func TestParseCommaSeparatedSchemaObjectIdentifierArray_Invalid(t *testing.T) {
func TestParseCommaSeparatedAccountIdentifierArray_Invalid(t *testing.T) {
testCases := []struct {
Name string
Value string
Error string
}{
{
Name: "bad quotes",
Value: `["a]`,
Error: "unable to read identifier: \"a, err = parse error on line 1, column 3: extraneous or missing \" in quoted-field",
Name: "invalid qoutes",
Value: `["a.b]`,
Error: "unable to read identifier: \"a.b, err = parse error on line 1, column 5: extraneous or missing \" in quoted-field",
},
{
Name: "missing parts",
Value: "[a.b.c, a.b]",
Error: "unexpected number of parts 2 in identifier a.b, expected 3 in a form of \"<database_name>.<schema_name>.<schema_object_name>\"",
Value: "[a.b, a]",
Error: "unexpected number of parts 1 in identifier a, expected 2 in a form of \"<organization_name>.<account_name>\"",
},
{
Name: "too many parts",
Value: "[a.b.c, a.b.c.d]",
Error: "unexpected number of parts 4 in identifier a.b.c.d, expected 3 in a form of \"<database_name>.<schema_name>.<schema_object_name>\"",
Value: "[a.b, a.b.c]",
Error: "unexpected number of parts 3 in identifier a.b.c, expected 2 in a form of \"<organization_name>.<account_name>\"",
},
{
Name: "missing parts - empty id",
Value: "[a.b.c, ]",
Value: "[a.b, ]",
Error: "incompatible identifier",
},
}

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
_, err := ParseCommaSeparatedSchemaObjectIdentifierArray(tc.Value)
_, err := ParseCommaSeparatedAccountIdentifierArray(tc.Value)
require.ErrorContains(t, err, tc.Error)
})
}
2 changes: 1 addition & 1 deletion templates/resources/primary_connection.md.tmpl
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ description: |-

{{ tffile (printf "examples/resources/%s/resource.tf" .Name)}}

-> **Note** Instead of using fully_qualified_name, you can reference objects managed outside Terraform by constructing a correct ID, consult [identifiers guide](./docs/guides/identifiers#new-computed-fully-qualified-name-field-in-resources).
-> **Note** Instead of using fully_qualified_name, you can reference objects managed outside Terraform by constructing a correct ID, consult [identifiers guide](../docs/guides/identifiers#new-computed-fully-qualified-name-field-in-resources).

-> **Note** To demote [`snowflake_primary_connection`](./primary_connection) to [`snowflake_secondary_connection`](./secondary_connection), resources need to be migrated manually. For guidance on removing and importing resources into the state check [resource migration](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/docs/technical-documentation/resource_migration.md). Remove the resource from the state, then recreate it in manually using:
sfc-gh-fbudzynski marked this conversation as resolved.
Show resolved Hide resolved
```