Skip to content

Commit

Permalink
chore: parser and secret tests (#3192)
Browse files Browse the repository at this point in the history
<!-- Feel free to delete comments as you fill this in -->

<!-- summary of changes -->
## Changes
* added `../` to `primary_connection` documentation 
* added invalid test cases for
`ParseCommaSeparatedAccountIdentifierArray`
* changed `secretsData()` function parameters

## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
* [x] acceptance tests for secrets
* [x] unit tests for parser 

## References
<!-- issues documentation links, etc  -->
references to comments:
*
#3177 (comment)
*
#3177 (comment)
  • Loading branch information
sfc-gh-fbudzynski authored Nov 14, 2024
1 parent 003999e commit 5ec9c86
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 33 deletions.
6 changes: 3 additions & 3 deletions docs/resources/primary_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ 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:
-> **Note** To demote `snowflake_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:
```
CREATE CONNECTION <name> AS REPLICA OF <organization_name>.<account_name>.<connection_name>;
```
and then import it as the `snowflake_secondary_connection`.
and then import it as the `snowflake_secondary_connection`.
<!-- TODO(SNOW-1634854): include an example showing both methods-->

<!-- schema generated by tfplugindocs -->
Expand Down
4 changes: 2 additions & 2 deletions docs/resources/secondary_connection.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ resource "snowflake_secondary_connection" "complete" {

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

-> **Note** To promote [`snowflake_secondary_connection`](./secondary_connection) to [`snowflake_primary_connection`](./primary_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 promote it manually using:
-> **Note** To promote `snowflake_secondary_connection` to [`snowflake_primary_connection`](./primary_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 promote it manually using:
```
ALTER CONNECTION <name> PRIMARY;
```
and then import it as the `snowflake_primary_connection`.
and then import it as the `snowflake_primary_connection`.
<!-- TODO(SNOW-1634854): include an example showing both methods-->

<!-- schema generated by tfplugindocs -->
Expand Down
19 changes: 6 additions & 13 deletions pkg/datasources/secrets_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down
56 changes: 46 additions & 10 deletions pkg/sdk/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
})
}
Expand Down
6 changes: 3 additions & 3 deletions templates/resources/primary_connection.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ 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:
-> **Note** To demote `snowflake_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:
```
CREATE CONNECTION <name> AS REPLICA OF <organization_name>.<account_name>.<connection_name>;
```
and then import it as the `snowflake_secondary_connection`.
and then import it as the `snowflake_secondary_connection`.
<!-- TODO(SNOW-1634854): include an example showing both methods-->

{{- end }}
Expand Down
4 changes: 2 additions & 2 deletions templates/resources/secondary_connection.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ description: |-

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

-> **Note** To promote [`snowflake_secondary_connection`](./secondary_connection) to [`snowflake_primary_connection`](./primary_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 promote it manually using:
-> **Note** To promote `snowflake_secondary_connection` to [`snowflake_primary_connection`](./primary_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 promote it manually using:
```
ALTER CONNECTION <name> PRIMARY;
```
and then import it as the `snowflake_primary_connection`.
and then import it as the `snowflake_primary_connection`.
<!-- TODO(SNOW-1634854): include an example showing both methods-->

{{- end }}
Expand Down

0 comments on commit 5ec9c86

Please sign in to comment.