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

fix: account for null comment #3417

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions pkg/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line is funny TBH

Copy link
Collaborator Author

@sfc-gh-jcieslak sfc-gh-jcieslak Feb 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think that we should make some adjustments to handle null pointers inside handleExternalChangesToObjectInShow instead of defining non-pointer values before the mappings. It would simplify the codebase for some of the resources.

); err != nil {
return diag.FromErr(err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/resources/manual_tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = '<admin_name>' -- TODO: Replace
ADMIN_PASSWORD = '<password>' -- TODO: Replace
ADMIN_USER_TYPE = SERVICE
EMAIL = '<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 '<organization_name>.<account_name>'`.
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 '<organization_name>.<account_name>'`.
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;
```
23 changes: 23 additions & 0 deletions pkg/resources/manual_tests/handling_account_null_comment/main.tf
Original file line number Diff line number Diff line change
@@ -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 = "<name>" # TODO: Replace
admin_name = "<admin_name>" # TODO: Replace
admin_password = "<admin_password>" # TODO: Replace
admin_user_type = "SERVICE"
email = "<email>" # TODO: Replace
edition = "STANDARD"
region = "<region>" # TODO: Replace (if needed; can be filled after the import)
comment = ""
}
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tools

go 1.22.10
go 1.23.6

require (
github.com/hashicorp/terraform-plugin-docs v0.20.1
Expand Down
Loading