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

feat: Rework account parameter resource #3264

Merged
merged 10 commits into from
Dec 12, 2024
8 changes: 8 additions & 0 deletions MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ across different versions.

## v0.99.0 ➞ v0.100.0

### snowflake_account_parameter resource changes

#### *(behavior change)* resource deletion
During resource deleting, provider now uses `UNSET` instead of `SET` with the default value.

#### *(behavior change)* changes in `key` field
The value of `key` field is now case-insensitive and is validated. The list of supported values is available in the resource documentation.

### snowflake_tag_association resource changes
#### *(behavior change)* new id format
In order to provide more functionality for tagging objects, we have changed the resource id from `"TAG_DATABASE"."TAG_SCHEMA"."TAG_NAME"` to `"TAG_DATABASE"."TAG_SCHEMA"."TAG_NAME"|TAG_VALUE|OBJECT_TYPE`. This allows to group tags associations per tag ID, tag value and object type in one resource.
Expand Down
15 changes: 9 additions & 6 deletions docs/resources/account_parameter.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
page_title: "snowflake_account_parameter Resource - terraform-provider-snowflake"
subcategory: ""
description: |-

Resource used to manage current account parameters. For more information, check parameters documentation https://docs.snowflake.com/en/sql-reference/parameters. To manage the whole account resource, see account ./account resource.
---

# snowflake_account_parameter (Resource)
!> **V1 release candidate** This resource was reworked and is a release candidate for the V1. We do not expect significant changes in it before the V1. We will welcome any feedback and adjust the resource if needed. Any errors reported will be resolved with a higher priority. We encourage checking this resource out before the V1 release. Please follow the [migration guide](https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/MIGRATION_GUIDE.md#v0990--v01000) to use it.

-> **Note** This resource does not support all account parameters. The supported ones are listed below. This feature gap will be addressed in future releases.

# snowflake_account_parameter (Resource)

Resource used to manage current account parameters. For more information, check [parameters documentation](https://docs.snowflake.com/en/sql-reference/parameters). To manage the whole account resource, see [account](./account) resource.
Copy link
Collaborator

Choose a reason for hiding this comment

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

./account won't apply the parameter so this can be removed it should point to the new resource for account (which is not yet present)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.


## Example Usage

Expand All @@ -22,7 +26,6 @@ resource "snowflake_account_parameter" "p2" {
value = "256"
}
```

-> **Note** Instead of using fully_qualified_name, you can reference objects managed outside Terraform by constructing a correct ID, consult [identifiers guide](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/guides/identifiers#new-computed-fully-qualified-name-field-in-resources).
<!-- TODO(SNOW-1634854): include an example showing both methods-->

Expand All @@ -31,8 +34,8 @@ resource "snowflake_account_parameter" "p2" {

### Required

- `key` (String) Name of account parameter. Valid values are those in [account parameters](https://docs.snowflake.com/en/sql-reference/parameters.html#account-parameters).
- `value` (String) Value of account parameter, as a string. Constraints are the same as those for the parameters in Snowflake documentation.
- `key` (String) Name of account parameter. Valid values are (case-insensitive): `ALLOW_CLIENT_MFA_CACHING` | `ALLOW_ID_TOKEN` | `CLIENT_ENCRYPTION_KEY_SIZE` | `ENABLE_IDENTIFIER_FIRST_LOGIN` | `ENABLE_INTERNAL_STAGES_PRIVATELINK` | `ENABLE_TRI_SECRET_AND_REKEY_OPT_OUT_FOR_IMAGE_REPOSITORY` | `ENABLE_TRI_SECRET_AND_REKEY_OPT_OUT_FOR_SPCS_BLOCK_STORAGE` | `EVENT_TABLE` | `EXTERNAL_OAUTH_ADD_PRIVILEGED_ROLES_TO_BLOCKED_LIST` | `INITIAL_REPLICATION_SIZE_LIMIT_IN_TB` | `MIN_DATA_RETENTION_TIME_IN_DAYS` | `NETWORK_POLICY` | `OAUTH_ADD_PRIVILEGED_ROLES_TO_BLOCKED_LIST` | `PERIODIC_DATA_REKEYING` | `PREVENT_LOAD_FROM_INLINE_URL` | `PREVENT_UNLOAD_TO_INLINE_URL` | `PREVENT_UNLOAD_TO_INTERNAL_STAGES` | `REQUIRE_STORAGE_INTEGRATION_FOR_STAGE_CREATION` | `REQUIRE_STORAGE_INTEGRATION_FOR_STAGE_OPERATION` | `SSO_LOGIN_PAGE` | `ABORT_DETACHED_QUERY` | `AUTOCOMMIT` | `BINARY_INPUT_FORMAT` | `BINARY_OUTPUT_FORMAT` | `CLIENT_METADATA_REQUEST_USE_CONNECTION_CTX` | `CLIENT_METADATA_USE_SESSION_DATABASE` | `CLIENT_RESULT_COLUMN_CASE_INSENSITIVE` | `DATE_INPUT_FORMAT` | `DATE_OUTPUT_FORMAT` | `ERROR_ON_NONDETERMINISTIC_MERGE` | `ERROR_ON_NONDETERMINISTIC_UPDATE` | `GEOGRAPHY_OUTPUT_FORMAT` | `JSON_INDENT` | `LOCK_TIMEOUT` | `MULTI_STATEMENT_COUNT` | `QUERY_TAG` | `QUOTED_IDENTIFIERS_IGNORE_CASE` | `ROWS_PER_RESULTSET` | `S3_STAGE_VPCE_DNS_NAME` | `SIMULATED_DATA_SHARING_CONSUMER` | `STATEMENT_TIMEOUT_IN_SECONDS` | `STRICT_JSON_OUTPUT` | `TIME_INPUT_FORMAT` | `TIME_OUTPUT_FORMAT` | `TIMESTAMP_DAY_IS_ALWAYS_24H` | `TIMESTAMP_INPUT_FORMAT` | `TIMESTAMP_LTZ_OUTPUT_FORMAT` | `TIMESTAMP_NTZ_OUTPUT_FORMAT` | `TIMESTAMP_OUTPUT_FORMAT` | `TIMESTAMP_TYPE_MAPPING` | `TIMESTAMP_TZ_OUTPUT_FORMAT` | `TIMEZONE` | `TRANSACTION_ABORT_ON_ERROR` | `TRANSACTION_DEFAULT_ISOLATION_LEVEL` | `TWO_DIGIT_CENTURY_START` | `UNSUPPORTED_DDL_ACTION` | `USE_CACHED_RESULT` | `WEEK_OF_YEAR_POLICY` | `WEEK_START` | `CATALOG` | `DATA_RETENTION_TIME_IN_DAYS` | `DEFAULT_DDL_COLLATION` | `EXTERNAL_VOLUME` | `LOG_LEVEL` | `MAX_CONCURRENCY_LEVEL` | `MAX_DATA_EXTENSION_TIME_IN_DAYS` | `PIPE_EXECUTION_PAUSED` | `REPLACE_INVALID_CHARACTERS` | `STATEMENT_QUEUED_TIMEOUT_IN_SECONDS` | `STORAGE_SERIALIZATION_POLICY` | `SHARE_RESTRICTIONS` | `SUSPEND_TASK_AFTER_NUM_FAILURES` | `TRACE_LEVEL` | `USER_TASK_MANAGED_INITIAL_WAREHOUSE_SIZE` | `USER_TASK_TIMEOUT_MS` | `TASK_AUTO_RETRY_ATTEMPTS` | `USER_TASK_MINIMUM_TRIGGER_INTERVAL_IN_SECONDS` | `METRIC_LEVEL` | `ENABLE_CONSOLE_OUTPUT` | `ENABLE_UNREDACTED_QUERY_SYNTAX_ERROR`.
- `value` (String) Value of account parameter, as a string. Constraints are the same as those for the parameters in Snowflake documentation. The parameter values are validated in Snowflake.

### Read-Only

Expand All @@ -43,5 +46,5 @@ resource "snowflake_account_parameter" "p2" {
Import is supported using the following syntax:

```shell
terraform import snowflake_account_parameter.p <parameter_name>
terraform import snowflake_account_parameter.p '<parameter_name>'
```
2 changes: 1 addition & 1 deletion examples/resources/snowflake_account_parameter/import.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
terraform import snowflake_account_parameter.p <parameter_name>
terraform import snowflake_account_parameter.p '<parameter_name>'

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ var allResourceSchemaDefs = []ResourceSchemaDef{
name: "Account",
schema: resources.Account().Schema,
},
{
name: "AccountParameter",
schema: resources.AccountParameter().Schema,
},
{
name: "FunctionJava",
schema: resources.FunctionJava().Schema,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 35 additions & 27 deletions pkg/resources/account_parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package resources

import (
"context"
"errors"
"fmt"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

Expand All @@ -15,15 +17,17 @@ import (

var accountParameterSchema = map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of account parameter. Valid values are those in [account parameters](https://docs.snowflake.com/en/sql-reference/parameters.html#account-parameters).",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: sdkValidation(sdk.ToAccountParameter),
DiffSuppressFunc: NormalizeAndCompare(sdk.ToAccountParameter),
Description: fmt.Sprintf("Name of account parameter. Valid values are (case-insensitive): %s.", possibleValuesListed(sdk.AsStringList(sdk.AllAccountParameters))),
},
"value": {
Type: schema.TypeString,
Required: true,
Description: "Value of account parameter, as a string. Constraints are the same as those for the parameters in Snowflake documentation.",
Description: "Value of account parameter, as a string. Constraints are the same as those for the parameters in Snowflake documentation. The parameter values are validated in Snowflake.",
},
}

Expand All @@ -34,6 +38,8 @@ func AccountParameter() *schema.Resource {
UpdateContext: TrackingUpdateWrapper(resources.AccountParameter, UpdateAccountParameter),
DeleteContext: TrackingDeleteWrapper(resources.AccountParameter, DeleteAccountParameter),

Description: "Resource used to manage current account parameters. For more information, check [parameters documentation](https://docs.snowflake.com/en/sql-reference/parameters). To manage the whole account resource, see [account](./account) resource.",

Schema: accountParameterSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
Expand All @@ -42,56 +48,58 @@ func AccountParameter() *schema.Resource {
}

// CreateAccountParameter implements schema.CreateFunc.
func CreateAccountParameter(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func CreateAccountParameter(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
key := d.Get("key").(string)
value := d.Get("value").(string)
parameter := sdk.AccountParameter(key)
err := client.Parameters.SetAccountParameter(ctx, parameter, value)
parameter, err := sdk.ToAccountParameter(key)
if err != nil {
return diag.FromErr(err)
}
err = client.Parameters.SetAccountParameter(ctx, parameter, value)
if err != nil {
return diag.FromErr(err)
}
d.SetId(key)
d.SetId(helpers.EncodeResourceIdentifier(string(parameter)))
return ReadAccountParameter(ctx, d, meta)
}

// ReadAccountParameter implements schema.ReadFunc.
func ReadAccountParameter(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func ReadAccountParameter(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
parameterName := d.Id()
parameter, err := client.Parameters.ShowAccountParameter(ctx, sdk.AccountParameter(parameterName))
parameterNameRaw := d.Id()
parameterName, err := sdk.ToAccountParameter(parameterNameRaw)
if err != nil {
return diag.FromErr(fmt.Errorf("error reading account parameter err = %w", err))
return diag.FromErr(err)
}
err = d.Set("value", parameter.Value)
parameter, err := client.Parameters.ShowAccountParameter(ctx, parameterName)
if err != nil {
return diag.FromErr(fmt.Errorf("error setting account parameter err = %w", err))
return diag.FromErr(fmt.Errorf("reading account parameter: %w", err))
}
err = d.Set("key", parameter.Key)
if err != nil {
return diag.FromErr(fmt.Errorf("error setting account parameter err = %w", err))
errs := errors.Join(
d.Set("value", parameter.Value),
d.Set("key", parameter.Key),
)
if errs != nil {
return diag.FromErr(errs)
}
return nil
}

// UpdateAccountParameter implements schema.UpdateFunc.
func UpdateAccountParameter(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func UpdateAccountParameter(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
return CreateAccountParameter(ctx, d, meta)
}

// DeleteAccountParameter implements schema.DeleteFunc.
func DeleteAccountParameter(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
func DeleteAccountParameter(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
key := d.Get("key").(string)
parameter := sdk.AccountParameter(key)
defaultParameter, err := client.Parameters.ShowAccountParameter(ctx, sdk.AccountParameter(key))
if err != nil {
return diag.FromErr(err)
}
defaultValue := defaultParameter.Default
err = client.Parameters.SetAccountParameter(ctx, parameter, defaultValue)

err := client.Parameters.UnsetAccountParameter(ctx, parameter)
if err != nil {
return diag.FromErr(fmt.Errorf("error resetting account parameter err = %w", err))
return diag.FromErr(fmt.Errorf("unsetting account parameter: %w", err))
}

d.SetId("")
Expand Down
Loading
Loading