diff --git a/Makefile b/Makefile index dbf6cb019d..2f1c3a8c3c 100644 --- a/Makefile +++ b/Makefile @@ -43,7 +43,7 @@ lint-fix: ## Run static code analysis, check formatting and try to fix findings ./bin/golangci-lint run ./... -v --fix mod: ## add missing and remove unused modules - go mod tidy -compat=1.22 + go mod tidy -compat=1.23.6 mod-check: mod ## check if there are any missing/unused modules git diff --exit-code -- go.mod go.sum diff --git a/pkg/resources/account.go b/pkg/resources/account.go index c103cf3a52..7477ca6e89 100644 --- a/pkg/resources/account.go +++ b/pkg/resources/account.go @@ -204,10 +204,15 @@ func ImportAccount(ctx context.Context, d *schema.ResourceData, meta any) ([]*sc } } + var comment string + if account.Comment != nil { + comment = *account.Comment + } + if err := errors.Join( d.Set("edition", string(*account.Edition)), d.Set("region", account.SnowflakeRegion), - d.Set("comment", *account.Comment), + d.Set("comment", comment), d.Set("is_org_admin", booleanStringFromBool(*account.IsOrgAdmin)), ); err != nil { return nil, err @@ -336,12 +341,17 @@ func ReadAccount(withExternalChangesMarking bool) schema.ReadContextFunc { regionGroup = parts[0] } } + var comment string + if account.Comment != nil { + comment = *account.Comment + } + if err = handleExternalChangesToObjectInShow(d, outputMapping{"edition", "edition", *account.Edition, *account.Edition, nil}, outputMapping{"is_org_admin", "is_org_admin", *account.IsOrgAdmin, booleanStringFromBool(*account.IsOrgAdmin), nil}, outputMapping{"region_group", "region_group", regionGroup, regionGroup, nil}, outputMapping{"snowflake_region", "region", account.SnowflakeRegion, account.SnowflakeRegion, nil}, - outputMapping{"comment", "comment", *account.Comment, *account.Comment, nil}, + outputMapping{"comment", "comment", comment, comment, nil}, ); err != nil { return diag.FromErr(err) } diff --git a/pkg/resources/manual_tests/README.md b/pkg/resources/manual_tests/README.md index f9a06a475f..47b3611c9c 100644 --- a/pkg/resources/manual_tests/README.md +++ b/pkg/resources/manual_tests/README.md @@ -11,6 +11,7 @@ Here's the list of cases we currently cannot reproduce and write acceptance test - `upgrade_cloned_database` - `upgrade_secondary_database` - `upgrade_shared_database` +- Creating an object externally in Snowflake using SQL then import it into the state as a first step of the test (check [handling account null comment](./handling_account_null_comment/handling_account_null_comment.md) for more details). ## How to use manual tests - Choose the test you want to run and go into the test folder. diff --git a/pkg/resources/manual_tests/handling_account_null_comment/handling_account_null_comment.md b/pkg/resources/manual_tests/handling_account_null_comment/handling_account_null_comment.md new file mode 100644 index 0000000000..57388f779c --- /dev/null +++ b/pkg/resources/manual_tests/handling_account_null_comment/handling_account_null_comment.md @@ -0,0 +1,41 @@ +# Handling account null comment + +This test shows that the problem from [this issue](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/3402) +is now handled by the provider. The issue occurs when importing an account that has `null` comment. +Because of the limitations in the [terraform plugin testing framework](https://github.com/hashicorp/terraform-plugin-testing) +we cannot create account externally and then import that account in the first step of the test. This can only be tested manually. + +## Snowflake setup + +Before running Terraform tests you have to create an account we would like to import. +Run the following script to create an account: +```snowflake +CREATE ACCOUNT TESTING_ACCOUNT + ADMIN_NAME = '' -- TODO: Replace + ADMIN_PASSWORD = '' -- TODO: Replace + ADMIN_USER_TYPE = SERVICE + EMAIL = '' -- TODO: Replace + EDITION = STANDARD + COMMENT = NULL; +``` + +## Test steps + +In this test, we'll make a use of building the provider locally and overriding it in the `~/.terraformrc`. +For more details on that, please visit our [advanced debugging guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/CONTRIBUTING.md#advanced-debugging). + +1. Copy the Terraform code from `main.tf` and initialize the project by running `terraform init`. +2. Run `terraform import snowflake_account.test_account '.'`. +3. Right now, you should get the same error as in [this issue](https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/3402). +4. Modify your `~/.terraformrc` to use the locally built provider +5. Run `terraform init -upgrade` to make sure the overridden plugin is used (you will be notified by warning logged by Terraform CLI) +6. Run `terraform import snowflake_account.test_account '.'`. +7. The import should be passing now. Run `terraform plan` to make sure the Read operation is also passing. + +## Test cleanup + +To clean up the test either run `terraform apply -auto-approve -destroy` or in a case where import didn't work +run the following Snowflake script: +```snowflake +DROP ACCOUNT TESTING_ACCOUNT GRACE_PERIOD_IN_DAYS = 3; +``` diff --git a/pkg/resources/manual_tests/handling_account_null_comment/main.tf b/pkg/resources/manual_tests/handling_account_null_comment/main.tf new file mode 100644 index 0000000000..223d007c87 --- /dev/null +++ b/pkg/resources/manual_tests/handling_account_null_comment/main.tf @@ -0,0 +1,23 @@ +terraform { + required_providers { + snowflake = { + source = "Snowflake-Labs/snowflake" + version = "1.0.3" + } + } +} + +provider "snowflake" { +} + +resource "snowflake_account" "test_account" { + grace_period_in_days = 3 + name = "" # TODO: Replace + admin_name = "" # TODO: Replace + admin_password = "" # TODO: Replace + admin_user_type = "SERVICE" + email = "" # TODO: Replace + edition = "STANDARD" + region = "" # TODO: Replace (if needed; can be filled after the import) + comment = "" +} diff --git a/tools/go.mod b/tools/go.mod index c4c3b6aa5e..e168d15a54 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -1,6 +1,6 @@ module tools -go 1.22.10 +go 1.23.6 require ( github.com/hashicorp/terraform-plugin-docs v0.20.1