From 34dfea07807c825818d5457db1c9aa7e252aefe5 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Tue, 30 Jul 2024 13:14:22 +0200 Subject: [PATCH 01/18] ci: update pre-commit components --- .github/workflows/pre-commit.yml | 4 ++++ .pre-commit-config.yaml | 9 +++++---- .terraform-docs.yml | 4 ++++ .tflint.hcl | 16 +++++----------- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 3363345..4e06095 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -12,3 +12,7 @@ on: jobs: main: uses: getindata/github-workflows/.github/workflows/tf-pre-commit.yml@v1 + with: + # tflint v0.52.0 is the latest version we can use with pre-commit v0.1.23 + # See .pre-commit-config.yaml for more details. + tflint-version: v0.52.0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8aa4d46..d145559 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,27 +1,28 @@ repos: - repo: https://github.com/gruntwork-io/pre-commit - # Stick to v0.1.20 until this bug is fixed: https://github.com/gruntwork-io/pre-commit/issues/102 # When updating, also check if tflint version in pre-commit workflow can be updated. rev: "v0.1.23" # Get the latest from: https://github.com/gruntwork-io/pre-commit/releases hooks: - id: terraform-validate # It should be the first step as it runs terraform init required by tflint - id: terraform-fmt - id: tflint + args: + - "--config=__GIT_ROOT__/.tflint.hcl" - repo: https://github.com/terraform-docs/terraform-docs - rev: "v0.16.0" # Get the latest from: https://github.com/terraform-docs/terraform-docs/releases + rev: "v0.18.0" # Get the latest from: https://github.com/terraform-docs/terraform-docs/releases hooks: - id: terraform-docs-go args: ["."] - repo: https://github.com/bridgecrewio/checkov.git - rev: "2.5.13" # Get the latest from: https://github.com/bridgecrewio/checkov/releases + rev: "3.2.192" # Get the latest from: https://github.com/bridgecrewio/checkov/releases hooks: - id: checkov args: [--skip-check, "CKV_TF_1"] # Terraform module sources do not use a git url with a commit hash revision - repo: https://github.com/pre-commit/pre-commit-hooks - rev: "v4.5.0" # Get the latest from: https://github.com/pre-commit/pre-commit-hooks/releases + rev: "v4.6.0" # Get the latest from: https://github.com/pre-commit/pre-commit-hooks/releases hooks: - id: check-merge-conflict args: ["--assume-in-merge"] diff --git a/.terraform-docs.yml b/.terraform-docs.yml index 5d31cc9..f6ffcef 100644 --- a/.terraform-docs.yml +++ b/.terraform-docs.yml @@ -6,6 +6,10 @@ sections: hide: [] show: [all] +recursive: + enabled: true + path: examples + content: |- {{ .Header }} diff --git a/.tflint.hcl b/.tflint.hcl index 6a33dcb..54a9b6f 100644 --- a/.tflint.hcl +++ b/.tflint.hcl @@ -1,16 +1,10 @@ -config { - ignore_module = { - "Invicton-Labs/deepmerge/null" = true - } -} - plugin "terraform" { - enabled = true - version = "0.5.0" - source = "github.com/terraform-linters/tflint-ruleset-terraform" - preset = "all" + enabled = true + version = "0.5.0" + source = "github.com/terraform-linters/tflint-ruleset-terraform" + preset = "all" } rule "terraform_standard_module_structure" { - enabled = false # Fails on context.tf + enabled = false # Fails on context.tf } From ca080a3f26eb396a36a024c9efdd3ef4c22ea98f Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Tue, 30 Jul 2024 13:26:29 +0200 Subject: [PATCH 02/18] feat: Align module with Snowflake provider changes Due to breaking changes in Snowflake provider and additional code optimizations, **breaking changes** were introduced in this module. List of code and variable (API) changes: - Switched to `snowflake_database_role` module to leverage new `database_roles` mechanism - `default_roles` and `custom_roles` are now combined and controlled by single logic in snowflake_database_role module - snowflake_database resource was updated to use newly introduced changes in Snowflake provider - snowflake_schema resource was updated to use newly introduced changes in Snowflake provider - variable `add_grants_to_existing_objects` was removed as it is no longer needed - minimum Snowflake provider version is `0.90.0` For more information, refer to [variables.tf](variables.tf), list of inputs below and Snowflake provider documentation When upgrading from `v1.x`, expect most of the resources to be recreated - if recreation is impossible, then it is possible to import some existing resources. --- README.md | 64 +++++++---- examples/complete/README.md | 144 +++++++++++-------------- examples/complete/main.tf | 44 +++----- examples/complete/outputs.tf | 5 - examples/complete/versions.tf | 2 +- examples/simple/README.md | 65 +++++++++-- examples/simple/versions.tf | 2 +- locals.tf | 52 ++++----- main.tf | 145 ++++++++++--------------- variables.tf | 198 ++++++++++++++++++++++++++-------- versions.tf | 2 +- 11 files changed, 427 insertions(+), 296 deletions(-) diff --git a/README.md b/README.md index 55162ab..f35683d 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,12 @@ Terraform module for Snowflake database management. * Creates Snowflake database -* Can create custom Snowflake roles with role-to-role, role-to-user assignments -* Can create a set of default roles to simplify access management: - * `READONLY` - granted `USAGE` privilege on the database +* Can create custom Snowflake database roles with role-to-role and role-to-user assignments +* Can create a set of default database roles to simplify access management: + * `READONLY` - granted `USAGE` and `MONITOR' privileges on the database * `TRANSFORMER` - allows creating schemas and some Snowflake objects in them * `ADMIN` - full access, including database options like `data_retention_time_in_days` -* Can create number of schemas in the database with their specific access roles +* Can create number of schemas in the database with their specific stages and access roles ## USAGE @@ -42,6 +42,24 @@ module "snowflake_database" { - [Simple](examples/simple) - Basic usage of the module - [Complete](examples/complete) - Advanced usage of the module +## BREAKING CHANGES IN v2.x.x +Due to breaking changes in Snowflake provider and additional code optimizations, **breaking changes** were introduced in `v2.0.0` version of this module. + + +List of code and variable (API) changes: +- Switched to `snowflake_database_role` module to leverage new `database_roles` mechanism +- `default_roles` and `custom_roles` are now combined and controlled by single logic in snowflake_database_role module +- snowflake_database resource was updated to use newly introduced changes in Snowflake provider +- snowflake_schema resource was updated to use newly introduced changes in Snowflake provider +- variable `add_grants_to_existing_objects` was removed as it is no longer needed +- minimum Snowflake provider version is `0.90.0` + + +For more information, refer to [variables.tf](variables.tf), list of inputs below and Snowflake provider documentation + + +When upgrading from `v1.x`, expect most of the resources to be recreated - if recreation is impossible, then it is possible to import some existing resources. + @@ -53,32 +71,44 @@ module "snowflake_database" { |------|-------------|------|---------|:--------:| | [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | | [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | +| [catalog](#input\_catalog) | The database parameter that specifies the default catalog to use for Iceberg tables | `string` | `null` | no | | [comment](#input\_comment) | Specifies a comment for the database | `string` | `null` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | -| [create\_default\_roles](#input\_create\_default\_roles) | Whether the default roles should be created | `bool` | `false` | no | -| [data\_retention\_time\_in\_days](#input\_data\_retention\_time\_in\_days) | Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table | `number` | `1` | no | +| [create\_default\_roles](#input\_create\_default\_roles) | Whether the default database roles should be created | `bool` | `false` | no | +| [data\_retention\_time\_in\_days](#input\_data\_retention\_time\_in\_days) | Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table | `number` | `null` | no | +| [default\_ddl\_collation](#input\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | `string` | `null` | no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | | [descriptor\_name](#input\_descriptor\_name) | Name of the descriptor used to form a resource name | `string` | `"snowflake-database"` | no | +| [enable\_console\_output](#input\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | `bool` | `false` | no | | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | -| [from\_database](#input\_from\_database) | Specify a database to create a clone from | `string` | `null` | no | -| [from\_replica](#input\_from\_replica) | Specify a fully-qualified path to a database to create a replica from | `string` | `null` | no | -| [from\_share](#input\_from\_share) | Specify a provider and a share in this map to create a database from a share |
object({
provider = string
share = string
})
| `null` | no | +| [external\_volume](#input\_external\_volume) | The database parameter that specifies the default external volume to use for Iceberg tables | `string` | `null` | no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | | [is\_transient](#input\_is\_transient) | Specifies a database as transient. Transient databases do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss | `bool` | `false` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | | [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | +| [log\_level](#input\_log\_level) | Specifies the severity level of messages that should be ingested and made available in the active event table. Valid options are: [TRACE DEBUG INFO WARN ERROR FATAL OFF] | `string` | `null` | no | +| [max\_data\_extension\_time\_in\_days](#input\_max\_data\_extension\_time\_in\_days) | Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for tables in the database to prevent streams on the tables from becoming stale | `number` | `null` | no | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | +| [quoted\_identifiers\_ignore\_case](#input\_quoted\_identifiers\_ignore\_case) | If true, the case of quoted identifiers is ignored | `bool` | `false` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | -| [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(list(string))
schema_grants = optional(list(string))
}))
| `{}` | no | -| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string)
data_retention_days = optional(number, 1)
is_transient = optional(bool, false)
is_managed = optional(bool, false)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
stage_grants = optional(list(string))
})), {})
})), {})
create_default_roles = optional(bool)
add_grants_to_existing_objects = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
add_grants_to_existing_objects = optional(bool)
schema_grants = optional(list(string))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | +| [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `false` | no | +| [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
}))
| `{}` | no | +| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_days_time_in_days = optional(number, 1)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, false)
with_managed_access = optional(bool, false)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, false)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, false)
enable_console_output = optional(bool, false)
pipe_execution_paused = optional(bool, false)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | +| [storage\_serialization\_policy](#input\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | `string` | `null` | no | +| [suspend\_task\_after\_num\_failures](#input\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | `number` | `null` | no | | [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | +| [task\_auto\_retry\_attempts](#input\_task\_auto\_retry\_attempts) | Maximum automatic retries allowed for a user task | `number` | `null` | no | | [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | +| [trace\_level](#input\_trace\_level) | Controls how trace events are ingested into the event table. Valid options are: [ALWAYS ON\_EVENT OFF] | `string` | `null` | no | +| [user\_task\_managed\_initial\_warehouse\_size](#input\_user\_task\_managed\_initial\_warehouse\_size) | The initial size of warehouse to use for managed warehouses in the absence of history | `string` | `null` | no | +| [user\_task\_minimum\_trigger\_interval\_in\_seconds](#input\_user\_task\_minimum\_trigger\_interval\_in\_seconds) | Minimum amount of time between Triggered Task executions in seconds | `number` | `null` | no | +| [user\_task\_timeout\_ms](#input\_user\_task\_timeout\_ms) | User task execution timeout in milliseconds | `number` | `null` | no | ## Modules @@ -86,9 +116,8 @@ module "snowflake_database" { |------|--------|---------| | [database\_label](#module\_database\_label) | cloudposse/label/null | 0.25.0 | | [roles\_deep\_merge](#module\_roles\_deep\_merge) | Invicton-Labs/deepmerge/null | 0.1.5 | -| [snowflake\_custom\_role](#module\_snowflake\_custom\_role) | getindata/role/snowflake | 1.4.0 | -| [snowflake\_default\_role](#module\_snowflake\_default\_role) | getindata/role/snowflake | 1.4.0 | -| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | 1.5.0 | +| [snowflake\_database\_role](#module\_snowflake\_database\_role) | getindata/database-role/snowflake | 1.1.0 | +| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | v2.0.0 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | ## Outputs @@ -105,23 +134,20 @@ module "snowflake_database" { | Name | Version | |------|---------| -| [snowflake](#provider\_snowflake) | ~> 0.54 | +| [snowflake](#provider\_snowflake) | ~> 0.90 | ## Requirements | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [snowflake](#requirement\_snowflake) | ~> 0.54 | +| [snowflake](#requirement\_snowflake) | ~> 0.90 | ## Resources | Name | Type | |------|------| | [snowflake_database.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/database) | resource | -| [snowflake_database_grant.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/database_grant) | resource | -| [snowflake_schema_grant.existing](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/schema_grant) | resource | -| [snowflake_schema_grant.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/schema_grant) | resource | ## CONTRIBUTING diff --git a/examples/complete/README.md b/examples/complete/README.md index 5d58994..3c4bb88 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -1,85 +1,6 @@ # Complete Example -```terraform -resource "snowflake_user" "dbt" { - name = "DBT user" - login_name = "dbt_user" - comment = "DBT user." -} - -module "snowflake_admin_role" { - source = "getindata/role/snowflake" - version = "1.0.3" - context = module.this.context - name = "admin" -} - -module "snowflake_dev_role" { - source = "getindata/role/snowflake" - version = "1.0.3" - context = module.this.context - name = "dev" -} - -module "prod_database" { - source = "../../" - context = module.this.context - environment = "prod" - - name = "analytics" - comment = "my database" - data_retention_time_in_days = 1 - is_transient = false - - create_default_roles = true - - roles = { - readonly = { - granted_to_roles = [module.snowflake_dev_role.name] - } - admin = { - granted_to_roles = [module.snowflake_admin_role.name] - } - } - - schemas = { - bronze = { - roles = { - admin = { - granted_to_roles = [module.snowflake_admin_role.name] - } - readonly = { - granted_to_roles = [module.snowflake_dev_role.name] - } - } - } - silver = { - roles = { - admin = { - granted_to_roles = [module.snowflake_admin_role.name] - } - } - } - gold = { - roles = { - admin = { - granted_to_roles = [module.snowflake_admin_role.name] - } - } - } - } -} - - -module "dev_database" { - source = "../../" - context = module.this.context - environment = "dev" - - name = "analytics" - from_database = module.prod_database.name -} -``` +This is complete usage example of the module. ## Usage Populate `.env` file with Snowflake credentials and make sure it's sourced to your shell. @@ -89,3 +10,66 @@ terraform init terraform plan -var-file fixtures.tfvars -out tfplan terraform apply tfplan ``` + + + + + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | +| [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | +| [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | +| [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | +| [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | +| [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | +| [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | +| [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | +| [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | +| [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | +| [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | +| [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | +| [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | +| [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | +| [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | +| [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | +| [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | +| [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [prod\_database](#module\_prod\_database) | ../../ | n/a | +| [this](#module\_this) | cloudposse/label/null | 0.25.0 | + +## Outputs + +| Name | Description | +|------|-------------| +| [prod\_database](#output\_prod\_database) | Prod database module outputs | + +## Providers + +| Name | Version | +|------|---------| +| [snowflake](#provider\_snowflake) | ~> 0.90 | + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.3 | +| [snowflake](#requirement\_snowflake) | ~> 0.90 | + +## Resources + +| Name | Type | +|------|------| +| [snowflake_account_role.admin_role](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/account_role) | resource | +| [snowflake_account_role.dev_role](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/account_role) | resource | +| [snowflake_user.dbt](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/user) | resource | + diff --git a/examples/complete/main.tf b/examples/complete/main.tf index e7d5767..d63470d 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -4,18 +4,12 @@ resource "snowflake_user" "dbt" { comment = "DBT user." } -module "snowflake_admin_role" { - source = "getindata/role/snowflake" - version = "1.0.3" - context = module.this.context - name = "admin" +resource "snowflake_account_role" "admin_role" { + name = "admin" } -module "snowflake_dev_role" { - source = "getindata/role/snowflake" - version = "1.0.3" - context = module.this.context - name = "dev" +resource "snowflake_account_role" "dev_role" { + name = "dev" } module "prod_database" { @@ -32,50 +26,46 @@ module "prod_database" { roles = { readonly = { - granted_to_roles = [module.snowflake_dev_role.name] + granted_to_roles = [snowflake_account_role.dev_role.name] + } + transformer = { + enabled = false } admin = { - granted_to_roles = [module.snowflake_admin_role.name] + granted_to_roles = [snowflake_account_role.admin_role.name] } } schemas = { bronze = { + data_retention_time_in_days = 7 stages = { - example = {} + example = { + comment = "my example stage" + } } roles = { admin = { - granted_to_roles = [module.snowflake_admin_role.name] + granted_to_roles = [snowflake_account_role.admin_role.name] } readonly = { - granted_to_roles = [module.snowflake_dev_role.name] + granted_to_roles = [snowflake_account_role.dev_role.name] } } } silver = { roles = { admin = { - granted_to_roles = [module.snowflake_admin_role.name] + granted_to_roles = [snowflake_account_role.admin_role.name] } } } gold = { roles = { admin = { - granted_to_roles = [module.snowflake_admin_role.name] + granted_to_roles = [snowflake_account_role.admin_role.name] } } } } } - - -module "dev_database" { - source = "../../" - context = module.this.context - environment = "dev" - - name = "analytics" - from_database = module.prod_database.name -} diff --git a/examples/complete/outputs.tf b/examples/complete/outputs.tf index 19982ff..1252330 100644 --- a/examples/complete/outputs.tf +++ b/examples/complete/outputs.tf @@ -1,8 +1,3 @@ -output "dev_database" { - description = "Dev database module outputs" - value = module.dev_database -} - output "prod_database" { description = "Prod database module outputs" value = module.prod_database diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf index beae9eb..33afd12 100644 --- a/examples/complete/versions.tf +++ b/examples/complete/versions.tf @@ -3,7 +3,7 @@ terraform { required_providers { snowflake = { source = "Snowflake-Labs/snowflake" - version = "~> 0.54" + version = "~> 0.90" } } } diff --git a/examples/simple/README.md b/examples/simple/README.md index 2fd10a7..8b9ebc5 100644 --- a/examples/simple/README.md +++ b/examples/simple/README.md @@ -1,12 +1,6 @@ # Simple Example -```terraform -module "database" { - source = "../../" - - name = "analytics" -} -``` +This is a simple usage example of the module. ## Usage Populate `.env` file with Snowflake credentials and make sure it's sourced to your shell. @@ -16,3 +10,60 @@ terraform init terraform plan -out tfplan terraform apply tfplan ``` + + + + + + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [additional\_tag\_map](#input\_additional\_tag\_map) | Additional key-value pairs to add to each map in `tags_as_list_of_maps`. Not added to `tags` or `id`.
This is for some rare cases where resources want additional configuration of tags
and therefore take a list of maps with tag key, value, and additional configuration. | `map(string)` | `{}` | no | +| [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,
in the order they appear in the list. New attributes are appended to the
end of the list. The elements of the list are joined by the `delimiter`
and treated as a single ID element. | `list(string)` | `[]` | no | +| [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | +| [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | +| [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | +| [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | +| [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | +| [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | +| [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | +| [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | +| [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | +| [labels\_as\_tags](#input\_labels\_as\_tags) | Set of labels (ID elements) to include as tags in the `tags` output.
Default is to include all labels.
Tags with empty values will not be included in the `tags` output.
Set to `[]` to suppress all generated tags.
**Notes:**
The value of the `name` tag, if included, will be the `id`, not the `name`.
Unlike other `null-label` inputs, the initial setting of `labels_as_tags` cannot be
changed in later chained modules. Attempts to change it will be silently ignored. | `set(string)` |
[
"default"
]
| no | +| [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | +| [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | +| [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | +| [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | +| [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).
Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no | +| [tenant](#input\_tenant) | ID element \_(Rarely used, not included by default)\_. A customer identifier, indicating who this instance of a resource is for | `string` | `null` | no | + +## Modules + +| Name | Source | Version | +|------|--------|---------| +| [database](#module\_database) | ../../ | n/a | +| [this](#module\_this) | cloudposse/label/null | 0.25.0 | + +## Outputs + +| Name | Description | +|------|-------------| +| [database](#output\_database) | Database module outputs | + +## Providers + +No providers. + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.3 | +| [snowflake](#requirement\_snowflake) | ~> 0.90 | + +## Resources + +No resources. + diff --git a/examples/simple/versions.tf b/examples/simple/versions.tf index beae9eb..33afd12 100644 --- a/examples/simple/versions.tf +++ b/examples/simple/versions.tf @@ -3,7 +3,7 @@ terraform { required_providers { snowflake = { source = "Snowflake-Labs/snowflake" - version = "~> 0.54" + version = "~> 0.90" } } } diff --git a/locals.tf b/locals.tf index 698c047..35738d5 100644 --- a/locals.tf +++ b/locals.tf @@ -17,23 +17,40 @@ locals { granted_roles = [] granted_to_roles = [] granted_to_users = [] - database_grants = [] + database_grants = {} schema_grants = [] } - default_roles_definition = { + + default_roles_definition = local.create_default_roles ? { readonly = { - database_grants = ["USAGE", "MONITOR"] - schema_grants = ["USAGE"] + database_grants = { + privileges = ["USAGE", "MONITOR"] + } + schema_grants = [{ + privileges = ["USAGE"] + all_schemas_in_database = true + }] } transformer = { - database_grants = ["USAGE", "MONITOR", "CREATE SCHEMA"] - schema_grants = ["USAGE", "CREATE TEMPORARY TABLE", "CREATE TAG", "CREATE PIPE", "CREATE PROCEDURE", "CREATE MATERIALIZED VIEW", "CREATE TABLE", "CREATE FILE FORMAT", "CREATE STAGE", "CREATE TASK", "CREATE FUNCTION", "CREATE EXTERNAL TABLE", "CREATE SEQUENCE", "CREATE VIEW", "CREATE STREAM", "CREATE DYNAMIC TABLE"] + database_grants = { + privileges = ["USAGE", "MONITOR", "CREATE SCHEMA"] + } + schema_grants = [{ + privileges = ["USAGE", "CREATE TEMPORARY TABLE", "CREATE TAG", "CREATE PIPE", "CREATE PROCEDURE", "CREATE MATERIALIZED VIEW", "CREATE TABLE", "CREATE FILE FORMAT", "CREATE STAGE", "CREATE TASK", "CREATE FUNCTION", "CREATE EXTERNAL TABLE", "CREATE SEQUENCE", "CREATE VIEW", "CREATE STREAM", "CREATE DYNAMIC TABLE"] + all_schemas_in_database = true + }] } admin = { - database_grants = ["ALL PRIVILEGES"] - schema_grants = ["ALL PRIVILEGES"] + database_grants = { + all_privileges = true + } + schema_grants = [{ + all_privileges = true + all_schemas_in_database = true + future_schemas_in_database = true + }] } - } + } : {} provided_roles = { for role_name, role in var.roles : role_name => { for k, v in role : k => v @@ -47,22 +64,9 @@ locals { ) } - default_roles = { - for role_name, role in local.roles_definition : role_name => role - if contains(keys(local.default_roles_definition), role_name) - } - - custom_roles = { - for role_name, role in local.roles_definition : role_name => role - if !contains(keys(local.default_roles_definition), role_name) - } - roles = { - for role_name, role in merge( - module.snowflake_default_role, - module.snowflake_custom_role - ) : role_name => role - if role.name != null + for role_name, role in local.roles_definition : role_name => role + if role_name != null && role.enabled } schemas = var.schemas diff --git a/main.tf b/main.tf index f9ca770..e60e662 100644 --- a/main.tf +++ b/main.tf @@ -11,111 +11,84 @@ module "database_label" { resource "snowflake_database" "this" { count = module.this.enabled ? 1 : 0 - name = local.name_from_descriptor - comment = var.comment - - data_retention_time_in_days = var.data_retention_time_in_days - is_transient = var.is_transient - - from_database = var.from_database - from_replica = var.from_replica - from_share = var.from_share + name = local.name_from_descriptor + is_transient = var.is_transient + comment = var.comment + + data_retention_time_in_days = var.data_retention_time_in_days + max_data_extension_time_in_days = var.max_data_extension_time_in_days + external_volume = var.external_volume + catalog = var.catalog + replace_invalid_characters = var.replace_invalid_characters + default_ddl_collation = var.default_ddl_collation + storage_serialization_policy = var.storage_serialization_policy + log_level = var.log_level + trace_level = var.trace_level + suspend_task_after_num_failures = var.suspend_task_after_num_failures + task_auto_retry_attempts = var.task_auto_retry_attempts + user_task_managed_initial_warehouse_size = var.user_task_managed_initial_warehouse_size + user_task_timeout_ms = var.user_task_timeout_ms + user_task_minimum_trigger_interval_in_seconds = var.user_task_minimum_trigger_interval_in_seconds + quoted_identifiers_ignore_case = var.quoted_identifiers_ignore_case + enable_console_output = var.enable_console_output } -module "snowflake_default_role" { - for_each = local.default_roles - - source = "getindata/role/snowflake" - version = "1.4.0" - - context = module.this.context - enabled = local.create_default_roles && each.value.enabled - descriptor_name = each.value.descriptor_name - - name = each.key - attributes = [one(snowflake_database.this[*].name)] +module "snowflake_database_role" { + for_each = local.roles - role_ownership_grant = each.value.role_ownership_grant - granted_to_users = each.value.granted_to_users - granted_to_roles = each.value.granted_to_roles - granted_roles = each.value.granted_roles -} - -module "snowflake_custom_role" { - for_each = local.custom_roles + source = "getindata/database-role/snowflake" + version = "1.1.0" + context = module.this.context - source = "getindata/role/snowflake" - version = "1.4.0" + database_name = one(snowflake_database.this[*].name) + name = each.key - context = module.this.context - enabled = local.enabled && each.value.enabled - descriptor_name = each.value.descriptor_name + granted_to_roles = lookup(each.value, "granted_to_roles", []) + granted_to_database_roles = lookup(each.value, "granted_to_database_roles", []) + granted_database_roles = lookup(each.value, "granted_database_roles", []) + database_grants = lookup(each.value, "database_grants", {}) + schema_grants = lookup(each.value, "schema_grants", []) - name = each.key attributes = [one(snowflake_database.this[*].name)] - - role_ownership_grant = each.value.role_ownership_grant - granted_to_users = each.value.granted_to_users - granted_to_roles = each.value.granted_to_roles - granted_roles = each.value.granted_roles } module "snowflake_schema" { for_each = local.schemas source = "getindata/schema/snowflake" - version = "1.5.0" + version = "v2.0.0" context = module.this.context enabled = local.enabled && each.value.enabled descriptor_name = each.value.descriptor_name - name = each.key - database = one(snowflake_database.this[*].name) - - skip_schema_creation = each.value.skip_schema_creation - data_retention_days = each.value.data_retention_days - is_transient = each.value.is_transient - is_managed = each.value.is_managed + name = each.key + is_transient = each.value.is_transient + with_managed_access = each.value.with_managed_access + comment = each.value.comment + database = one(snowflake_database.this[*].name) + + skip_schema_creation = each.value.skip_schema_creation + data_retention_time_in_days = each.value.data_retention_time_in_days + max_data_extension_time_in_days = each.value.max_data_extension_time_in_days + external_volume = each.value.external_volume + catalog = each.value.catalog + replace_invalid_characters = each.value.replace_invalid_characters + default_ddl_collation = each.value.default_ddl_collation + storage_serialization_policy = each.value.storage_serialization_policy + log_level = each.value.log_level + trace_level = each.value.trace_level + suspend_task_after_num_failures = each.value.suspend_task_after_num_failures + task_auto_retry_attempts = each.value.task_auto_retry_attempts + user_task_managed_initial_warehouse_size = each.value.user_task_managed_initial_warehouse_size + user_task_timeout_ms = each.value.user_task_timeout_ms + user_task_minimum_trigger_interval_in_seconds = each.value.user_task_minimum_trigger_interval_in_seconds + quoted_identifiers_ignore_case = each.value.quoted_identifiers_ignore_case + enable_console_output = each.value.enable_console_output + pipe_execution_paused = each.value.pipe_execution_paused stages = each.value.stages + roles = each.value.roles - create_default_roles = coalesce(each.value.create_default_roles, var.create_default_roles) - add_grants_to_existing_objects = coalesce(each.value.add_grants_to_existing_objects, false) - roles = each.value.roles -} - -resource "snowflake_database_grant" "this" { - for_each = local.enabled ? transpose({ for role_name, role in local.roles : local.roles[role_name].name => - lookup(local.roles_definition[role_name], "database_grants", []) - if lookup(local.roles_definition[role_name], "enabled", true) - }) : {} - - database_name = one(snowflake_database.this[*].name) - privilege = each.key - roles = each.value -} - -resource "snowflake_schema_grant" "this" { - for_each = local.enabled ? transpose({ for role_name, role in local.roles : local.roles[role_name].name => - lookup(local.roles_definition[role_name], "schema_grants", []) - if lookup(local.roles_definition[role_name], "enabled", true) - }) : {} - - database_name = one(snowflake_database.this[*].name) - on_future = true - privilege = each.key - roles = each.value -} - -resource "snowflake_schema_grant" "existing" { - for_each = local.enabled ? transpose({ for role_name, role in local.roles : local.roles[role_name].name => - lookup(local.roles_definition[role_name], "schema_grants", []) - if lookup(local.roles_definition[role_name], "enabled", true) - }) : {} - - database_name = one(snowflake_database.this[*].name) - on_all = true - privilege = each.key - roles = each.value + create_default_roles = coalesce(each.value.create_default_roles, var.create_default_roles) } diff --git a/variables.tf b/variables.tf index c9a8a50..8acc61a 100644 --- a/variables.tf +++ b/variables.tf @@ -13,7 +13,13 @@ variable "comment" { variable "data_retention_time_in_days" { description = "Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table" type = number - default = 1 + default = null +} + +variable "max_data_extension_time_in_days" { + description = "Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for tables in the database to prevent streams on the tables from becoming stale" + type = number + default = null } variable "is_transient" { @@ -22,29 +28,92 @@ variable "is_transient" { default = false } -variable "from_database" { - description = "Specify a database to create a clone from" +variable "external_volume" { + description = "The database parameter that specifies the default external volume to use for Iceberg tables" + type = string + default = null +} + +variable "catalog" { + description = "The database parameter that specifies the default catalog to use for Iceberg tables" + type = string + default = null +} + +variable "replace_invalid_characters" { + description = "Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table" + type = bool + default = false +} + +variable "default_ddl_collation" { + description = "Specifies a default collation specification for all schemas and tables added to the database." + type = string + default = null +} + +variable "storage_serialization_policy" { + description = "The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED]" type = string default = null } -variable "from_replica" { - description = "Specify a fully-qualified path to a database to create a replica from" +variable "log_level" { + description = "Specifies the severity level of messages that should be ingested and made available in the active event table. Valid options are: [TRACE DEBUG INFO WARN ERROR FATAL OFF]" type = string default = null } -variable "from_share" { - description = "Specify a provider and a share in this map to create a database from a share" - type = object({ - provider = string - share = string - }) - default = null +variable "trace_level" { + description = "Controls how trace events are ingested into the event table. Valid options are: [ALWAYS ON_EVENT OFF]" + type = string + default = null +} + +variable "suspend_task_after_num_failures" { + description = "How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending" + type = number + default = null +} + +variable "task_auto_retry_attempts" { + description = "Maximum automatic retries allowed for a user task" + type = number + default = null +} + +variable "user_task_managed_initial_warehouse_size" { + description = "The initial size of warehouse to use for managed warehouses in the absence of history" + type = string + default = null +} + +variable "user_task_minimum_trigger_interval_in_seconds" { + description = "Minimum amount of time between Triggered Task executions in seconds" + type = number + default = null +} + +variable "user_task_timeout_ms" { + description = "User task execution timeout in milliseconds" + type = number + default = null +} + +variable "quoted_identifiers_ignore_case" { + description = "If true, the case of quoted identifiers is ignored" + type = bool + default = false +} + +variable "enable_console_output" { + description = "If true, enables stdout/stderr fast path logging for anonymous stored procedures" + type = bool + default = false } variable "create_default_roles" { - description = "Whether the default roles should be created" + description = "Whether the default database roles should be created" type = bool default = false } @@ -59,8 +128,19 @@ variable "roles" { granted_roles = optional(list(string)) granted_to_roles = optional(list(string)) granted_to_users = optional(list(string)) - database_grants = optional(list(string)) - schema_grants = optional(list(string)) + database_grants = optional(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool, false) + privileges = optional(list(string), null) + })) + schema_grants = optional(list(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool, false) + privileges = optional(list(string), null) + all_schemas_in_database = optional(bool, false) + future_schemas_in_database = optional(bool, false) + schema_name = optional(string, null) + }))) })) default = {} } @@ -68,13 +148,29 @@ variable "roles" { variable "schemas" { description = "Schemas to be created in the database" type = map(object({ - enabled = optional(bool, true) - skip_schema_creation = optional(bool, false) - descriptor_name = optional(string, "snowflake-schema") - comment = optional(string) - data_retention_days = optional(number, 1) - is_transient = optional(bool, false) - is_managed = optional(bool, false) + enabled = optional(bool, true) + skip_schema_creation = optional(bool, false) + descriptor_name = optional(string, "snowflake-schema") + comment = optional(string, null) + data_retention_days_time_in_days = optional(number, 1) + max_data_extension_time_in_days = optional(number, null) + is_transient = optional(bool, false) + with_managed_access = optional(bool, false) + external_volume = optional(string, null) + catalog = optional(string, null) + replace_invalid_characters = optional(bool, false) + default_ddl_collation = optional(string, null) + storage_serialization_policy = optional(string, null) + log_level = optional(string, null) + trace_level = optional(string, null) + suspend_task_after_num_failures = optional(number, null) + task_auto_retry_attempts = optional(number, null) + user_task_managed_initial_warehouse_size = optional(string, null) + user_task_timeout_ms = optional(number, null) + user_task_minimum_trigger_interval_in_seconds = optional(number, null) + quoted_identifiers_ignore_case = optional(bool, false) + enable_console_output = optional(bool, false) + pipe_execution_paused = optional(bool, false) stages = optional(map(object({ enabled = optional(bool, true) descriptor_name = optional(string, "snowflake-stage") @@ -97,32 +193,44 @@ variable "schemas" { granted_roles = optional(list(string)) granted_to_roles = optional(list(string)) granted_to_users = optional(list(string)) - stage_grants = optional(list(string)) + schema_grants = optional(list(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool, false) + privileges = optional(list(string), null) + all_schemas_in_database = optional(bool, false) + future_schemas_in_database = optional(bool, false) + schema_name = optional(string, null) + }))) })), {}) })), {}) - create_default_roles = optional(bool) - add_grants_to_existing_objects = optional(bool) + create_default_roles = optional(bool) roles = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-role") - comment = optional(string) - role_ownership_grant = optional(string) - granted_roles = optional(list(string)) - granted_to_roles = optional(list(string)) - granted_to_users = optional(list(string)) - add_grants_to_existing_objects = optional(bool) - schema_grants = optional(list(string)) - table_grants = optional(list(string)) - external_table_grants = optional(list(string)) - view_grants = optional(list(string)) - materialized_view_grants = optional(list(string)) - file_format_grants = optional(list(string)) - function_grants = optional(list(string)) - stage_grants = optional(list(string)) - task_grants = optional(list(string)) - procedure_grants = optional(list(string)) - sequence_grants = optional(list(string)) - stream_grants = optional(list(string)) + enabled = optional(bool, true) + descriptor_name = optional(string, "snowflake-role") + comment = optional(string) + role_ownership_grant = optional(string) + granted_roles = optional(list(string)) + granted_to_roles = optional(list(string)) + granted_to_users = optional(list(string)) + schema_grants = optional(list(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool, false) + privileges = optional(list(string), null) + all_schemas_in_database = optional(bool, false) + future_schemas_in_database = optional(bool, false) + schema_name = optional(string, null) + }))) + table_grants = optional(list(string)) + external_table_grants = optional(list(string)) + view_grants = optional(list(string)) + materialized_view_grants = optional(list(string)) + file_format_grants = optional(list(string)) + function_grants = optional(list(string)) + stage_grants = optional(list(string)) + task_grants = optional(list(string)) + procedure_grants = optional(list(string)) + sequence_grants = optional(list(string)) + stream_grants = optional(list(string)) })), {}) })) default = {} diff --git a/versions.tf b/versions.tf index beae9eb..33afd12 100644 --- a/versions.tf +++ b/versions.tf @@ -3,7 +3,7 @@ terraform { required_providers { snowflake = { source = "Snowflake-Labs/snowflake" - version = "~> 0.54" + version = "~> 0.90" } } } From 4f12759cf5b2a0e58b7aa6633544ead44b27315c Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Tue, 30 Jul 2024 14:57:22 +0200 Subject: [PATCH 03/18] add new variables into outputs --- README.md | 17 +++++++++++- outputs.tf | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f35683d..8c8ccd0 100644 --- a/README.md +++ b/README.md @@ -124,11 +124,26 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | Name | Description | |------|-------------| +| [catalog](#output\_catalog) | The database parameter that specifies the default catalog to use for Iceberg tables | | [data\_retention\_time\_in\_days](#output\_data\_retention\_time\_in\_days) | Data retention days for the database | -| [is\_transient](#output\_is\_transient) | Is databse transient | +| [default\_ddl\_collation](#output\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | +| [enable\_console\_output](#output\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | +| [external\_volume](#output\_external\_volume) | The database parameter that specifies the default external volume to use for Iceberg tables | +| [is\_transient](#output\_is\_transient) | Specifies a database as transient. Transient databases do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss | +| [log\_level](#output\_log\_level) | Specifies the severity level of messages that should be ingested and made available in the active event table. Valid options are: [TRACE DEBUG INFO WARN ERROR FATAL OFF] | +| [max\_data\_extension\_time\_in\_days](#output\_max\_data\_extension\_time\_in\_days) | Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for tables in the database to prevent streams on the tables from becoming stale | | [name](#output\_name) | Name of the database | +| [quoted\_identifiers\_ignore\_case](#output\_quoted\_identifiers\_ignore\_case) | If true, the case of quoted identifiers is ignored | +| [replace\_invalid\_characters](#output\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | | [roles](#output\_roles) | Snowflake Database roles | | [schemas](#output\_schemas) | This database schemas | +| [storage\_serialization\_policy](#output\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | +| [suspend\_task\_after\_num\_failures](#output\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | +| [task\_auto\_retry\_attempts](#output\_task\_auto\_retry\_attempts) | Maximum automatic retries allowed for a user task | +| [trace\_level](#output\_trace\_level) | Controls how trace events are ingested into the event table. Valid options are: [ALWAYS ON\_EVENT OFF] | +| [user\_task\_managed\_initial\_warehouse\_size](#output\_user\_task\_managed\_initial\_warehouse\_size) | The initial size of warehouse to use for managed warehouses in the absence of history | +| [user\_task\_minimum\_trigger\_interval\_in\_seconds](#output\_user\_task\_minimum\_trigger\_interval\_in\_seconds) | Minimum amount of time between Triggered Task executions in seconds | +| [user\_task\_timeout\_ms](#output\_user\_task\_timeout\_ms) | User task execution timeout in milliseconds | ## Providers diff --git a/outputs.tf b/outputs.tf index fc83142..6df50c0 100644 --- a/outputs.tf +++ b/outputs.tf @@ -8,11 +8,86 @@ output "data_retention_time_in_days" { value = one(snowflake_database.this[*].data_retention_time_in_days) } +output "max_data_extension_time_in_days" { + description = "Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for tables in the database to prevent streams on the tables from becoming stale" + value = one(snowflake_database.this[*].max_data_extension_time_in_days) +} + output "is_transient" { - description = "Is databse transient" + description = "Specifies a database as transient. Transient databases do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss" value = one(snowflake_database.this[*].is_transient) } +output "external_volume" { + description = "The database parameter that specifies the default external volume to use for Iceberg tables" + value = one(snowflake_database.this[*].external_volume) +} + +output "catalog" { + description = "The database parameter that specifies the default catalog to use for Iceberg tables" + value = one(snowflake_database.this[*].catalog) +} + +output "replace_invalid_characters" { + description = "Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table" + value = one(snowflake_database.this[*].replace_invalid_characters) +} + +output "default_ddl_collation" { + description = "Specifies a default collation specification for all schemas and tables added to the database." + value = one(snowflake_database.this[*].default_ddl_collation) +} + +output "storage_serialization_policy" { + description = "The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED]" + value = one(snowflake_database.this[*].storage_serialization_policy) +} + +output "log_level" { + description = "Specifies the severity level of messages that should be ingested and made available in the active event table. Valid options are: [TRACE DEBUG INFO WARN ERROR FATAL OFF]" + value = one(snowflake_database.this[*].log_level) +} + +output "trace_level" { + description = "Controls how trace events are ingested into the event table. Valid options are: [ALWAYS ON_EVENT OFF]" + value = one(snowflake_database.this[*].trace_level) +} + +output "suspend_task_after_num_failures" { + description = "How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending" + value = one(snowflake_database.this[*].suspend_task_after_num_failures) +} + +output "task_auto_retry_attempts" { + description = "Maximum automatic retries allowed for a user task" + value = one(snowflake_database.this[*].task_auto_retry_attempts) +} + +output "user_task_managed_initial_warehouse_size" { + description = "The initial size of warehouse to use for managed warehouses in the absence of history" + value = one(snowflake_database.this[*].user_task_managed_initial_warehouse_size) +} + +output "user_task_minimum_trigger_interval_in_seconds" { + description = "Minimum amount of time between Triggered Task executions in seconds" + value = one(snowflake_database.this[*].user_task_minimum_trigger_interval_in_seconds) +} + +output "user_task_timeout_ms" { + description = "User task execution timeout in milliseconds" + value = one(snowflake_database.this[*].user_task_timeout_ms) +} + +output "quoted_identifiers_ignore_case" { + description = "If true, the case of quoted identifiers is ignored" + value = one(snowflake_database.this[*].quoted_identifiers_ignore_case) +} + +output "enable_console_output" { + description = "If true, enables stdout/stderr fast path logging for anonymous stored procedures" + value = one(snowflake_database.this[*].enable_console_output) +} + output "roles" { description = "Snowflake Database roles" value = local.roles From 90546c9188bdc6a4b3fc64f19ee3021cd6b95ffc Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Tue, 30 Jul 2024 15:00:35 +0200 Subject: [PATCH 04/18] style: docs formatting --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8c8ccd0..842f77c 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Terraform module for Snowflake database management. * Creates Snowflake database * Can create custom Snowflake database roles with role-to-role and role-to-user assignments * Can create a set of default database roles to simplify access management: - * `READONLY` - granted `USAGE` and `MONITOR' privileges on the database + * `READONLY` - granted `USAGE` and `MONITOR` privileges on the database * `TRANSFORMER` - allows creating schemas and some Snowflake objects in them * `ADMIN` - full access, including database options like `data_retention_time_in_days` * Can create number of schemas in the database with their specific stages and access roles From a52525016f329f58524c30b7a380a19566e8e32f Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Wed, 31 Jul 2024 09:02:38 +0200 Subject: [PATCH 05/18] chore: remove version prefix --- README.md | 2 +- main.tf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 842f77c..e42d7f7 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [database\_label](#module\_database\_label) | cloudposse/label/null | 0.25.0 | | [roles\_deep\_merge](#module\_roles\_deep\_merge) | Invicton-Labs/deepmerge/null | 0.1.5 | | [snowflake\_database\_role](#module\_snowflake\_database\_role) | getindata/database-role/snowflake | 1.1.0 | -| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | v2.0.0 | +| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | 2.0.0 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | ## Outputs diff --git a/main.tf b/main.tf index e60e662..bc5cf9e 100644 --- a/main.tf +++ b/main.tf @@ -56,7 +56,7 @@ module "snowflake_schema" { for_each = local.schemas source = "getindata/schema/snowflake" - version = "v2.0.0" + version = "2.0.0" context = module.this.context enabled = local.enabled && each.value.enabled From 231f553dd9f2d17bc52409df7293325ba47f737a Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Wed, 31 Jul 2024 09:21:32 +0200 Subject: [PATCH 06/18] use provider default values for optional variables --- README.md | 10 +++++----- variables.tf | 34 +++++++++++++++++----------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index e42d7f7..d7064c3 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,12 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | | [descriptor\_name](#input\_descriptor\_name) | Name of the descriptor used to form a resource name | `string` | `"snowflake-database"` | no | -| [enable\_console\_output](#input\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | `bool` | `false` | no | +| [enable\_console\_output](#input\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | `bool` | `null` | no | | [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no | | [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no | | [external\_volume](#input\_external\_volume) | The database parameter that specifies the default external volume to use for Iceberg tables | `string` | `null` | no | | [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).
Set to `0` for unlimited length.
Set to `null` for keep the existing setting, which defaults to `0`.
Does not affect `id_full`. | `number` | `null` | no | -| [is\_transient](#input\_is\_transient) | Specifies a database as transient. Transient databases do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss | `bool` | `false` | no | +| [is\_transient](#input\_is\_transient) | Specifies a database as transient. Transient databases do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss | `bool` | `null` | no | | [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.
Does not affect keys of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper`.
Default value: `title`. | `string` | `null` | no | | [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.
Defaults to ["namespace", "environment", "stage", "name", "attributes"].
You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no | | [label\_value\_case](#input\_label\_value\_case) | Controls the letter case of ID elements (labels) as included in `id`,
set as tag values, and output by this module individually.
Does not affect values of tags passed in via the `tags` input.
Possible values: `lower`, `title`, `upper` and `none` (no transformation).
Set this to `title` and set `delimiter` to `""` to yield Pascal Case IDs.
Default value: `lower`. | `string` | `null` | no | @@ -94,11 +94,11 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [max\_data\_extension\_time\_in\_days](#input\_max\_data\_extension\_time\_in\_days) | Object parameter that specifies the maximum number of days for which Snowflake can extend the data retention period for tables in the database to prevent streams on the tables from becoming stale | `number` | `null` | no | | [name](#input\_name) | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'.
This is the only ID element not also included as a `tag`.
The "name" tag is set to the full `id` string. There is no tag with the value of the `name` input. | `string` | `null` | no | | [namespace](#input\_namespace) | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | `string` | `null` | no | -| [quoted\_identifiers\_ignore\_case](#input\_quoted\_identifiers\_ignore\_case) | If true, the case of quoted identifiers is ignored | `bool` | `false` | no | +| [quoted\_identifiers\_ignore\_case](#input\_quoted\_identifiers\_ignore\_case) | If true, the case of quoted identifiers is ignored | `bool` | `null` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | -| [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `false` | no | +| [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `null` | no | | [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
}))
| `{}` | no | -| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_days_time_in_days = optional(number, 1)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, false)
with_managed_access = optional(bool, false)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, false)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, false)
enable_console_output = optional(bool, false)
pipe_execution_paused = optional(bool, false)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | +| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_days_time_in_days = optional(number, 1)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, false)
with_managed_access = optional(bool, false)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, false)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, false)
enable_console_output = optional(bool, false)
pipe_execution_paused = optional(bool, false)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_stage_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_serialization\_policy](#input\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | `string` | `null` | no | | [suspend\_task\_after\_num\_failures](#input\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | `number` | `null` | no | diff --git a/variables.tf b/variables.tf index 8acc61a..181232c 100644 --- a/variables.tf +++ b/variables.tf @@ -25,7 +25,7 @@ variable "max_data_extension_time_in_days" { variable "is_transient" { description = "Specifies a database as transient. Transient databases do not have a Fail-safe period so they do not incur additional storage costs once they leave Time Travel; however, this means they are also not protected by Fail-safe in the event of a data loss" type = bool - default = false + default = null } variable "external_volume" { @@ -43,7 +43,7 @@ variable "catalog" { variable "replace_invalid_characters" { description = "Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table" type = bool - default = false + default = null } variable "default_ddl_collation" { @@ -103,13 +103,13 @@ variable "user_task_timeout_ms" { variable "quoted_identifiers_ignore_case" { description = "If true, the case of quoted identifiers is ignored" type = bool - default = false + default = null } variable "enable_console_output" { description = "If true, enables stdout/stderr fast path logging for anonymous stored procedures" type = bool - default = false + default = null } variable "create_default_roles" { @@ -172,19 +172,19 @@ variable "schemas" { enable_console_output = optional(bool, false) pipe_execution_paused = optional(bool, false) stages = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-stage") - aws_external_id = optional(string) - comment = optional(string) - copy_options = optional(string) - credentials = optional(string) - directory = optional(string) - encryption = optional(string) - file_format = optional(string) - snowflake_iam_user = optional(string) - storage_integration = optional(string) - url = optional(string) - create_default_roles = optional(bool) + enabled = optional(bool, true) + descriptor_name = optional(string, "snowflake-stage") + aws_external_id = optional(string) + comment = optional(string) + copy_options = optional(string) + credentials = optional(string) + directory = optional(string) + encryption = optional(string) + file_format = optional(string) + snowflake_iam_user = optional(string) + storage_integration = optional(string) + url = optional(string) + create_default_stage_roles = optional(bool) roles = optional(map(object({ enabled = optional(bool, true) descriptor_name = optional(string, "snowflake-role") From e842569c9d4a987337445d2601b1c0f15fc10d0a Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Wed, 31 Jul 2024 15:27:17 +0200 Subject: [PATCH 07/18] chore: remove not needed tflint tf plugin config terraform plugin is already available in tflint --- .tflint.hcl | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.tflint.hcl b/.tflint.hcl index 54a9b6f..3b6ace1 100644 --- a/.tflint.hcl +++ b/.tflint.hcl @@ -1,10 +1,3 @@ -plugin "terraform" { - enabled = true - version = "0.5.0" - source = "github.com/terraform-linters/tflint-ruleset-terraform" - preset = "all" -} - rule "terraform_standard_module_structure" { enabled = false # Fails on context.tf } From 499b395b08a4a9937d856d8ddeb930cfa25c6fd2 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Wed, 31 Jul 2024 15:43:13 +0200 Subject: [PATCH 08/18] split default and custom roles to avoid deepmerge issues --- README.md | 9 +++++---- locals.tf | 21 +++++++++++++++++---- main.tf | 45 ++++++++++++++++++++++++++++++++++++++++----- variables.tf | 40 ++++++++++++++++++++-------------------- 4 files changed, 82 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index d7064c3..e54a0e2 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Terraform module for Snowflake database management. * Creates Snowflake database -* Can create custom Snowflake database roles with role-to-role and role-to-user assignments +* Can create custom Snowflake database roles with role-to-role assignments * Can create a set of default database roles to simplify access management: * `READONLY` - granted `USAGE` and `MONITOR` privileges on the database * `TRANSFORMER` - allows creating schemas and some Snowflake objects in them @@ -48,7 +48,7 @@ Due to breaking changes in Snowflake provider and additional code optimizations, List of code and variable (API) changes: - Switched to `snowflake_database_role` module to leverage new `database_roles` mechanism -- `default_roles` and `custom_roles` are now combined and controlled by single logic in snowflake_database_role module +- database `default_roles` and `custom_roles` are now managed by `getindata/database_role/snowflake` module - snowflake_database resource was updated to use newly introduced changes in Snowflake provider - snowflake_schema resource was updated to use newly introduced changes in Snowflake provider - variable `add_grants_to_existing_objects` was removed as it is no longer needed @@ -98,7 +98,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `null` | no | | [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
}))
| `{}` | no | -| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_days_time_in_days = optional(number, 1)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, false)
with_managed_access = optional(bool, false)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, false)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, false)
enable_console_output = optional(bool, false)
pipe_execution_paused = optional(bool, false)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_stage_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | +| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, 1)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_serialization\_policy](#input\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | `string` | `null` | no | | [suspend\_task\_after\_num\_failures](#input\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | `number` | `null` | no | @@ -116,7 +116,8 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re |------|--------|---------| | [database\_label](#module\_database\_label) | cloudposse/label/null | 0.25.0 | | [roles\_deep\_merge](#module\_roles\_deep\_merge) | Invicton-Labs/deepmerge/null | 0.1.5 | -| [snowflake\_database\_role](#module\_snowflake\_database\_role) | getindata/database-role/snowflake | 1.1.0 | +| [snowflake\_custom\_role](#module\_snowflake\_custom\_role) | getindata/database-role/snowflake | 1.1.1 | +| [snowflake\_default\_role](#module\_snowflake\_default\_role) | getindata/database-role/snowflake | 1.1.1 | | [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | 2.0.0 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | diff --git a/locals.tf b/locals.tf index 35738d5..7c8bb55 100644 --- a/locals.tf +++ b/locals.tf @@ -21,7 +21,7 @@ locals { schema_grants = [] } - default_roles_definition = local.create_default_roles ? { + default_roles_definition = { readonly = { database_grants = { privileges = ["USAGE", "MONITOR"] @@ -50,7 +50,7 @@ locals { future_schemas_in_database = true }] } - } : {} + } provided_roles = { for role_name, role in var.roles : role_name => { for k, v in role : k => v @@ -64,9 +64,22 @@ locals { ) } - roles = { + default_roles = { + for role_name, role in local.roles_definition : role_name => role + if contains(keys(local.default_roles_definition), role_name) + } + + custom_roles = { for role_name, role in local.roles_definition : role_name => role - if role_name != null && role.enabled + if !contains(keys(local.default_roles_definition), role_name) + } + + roles = { + for role_name, role in merge( + module.snowflake_default_role, + module.snowflake_custom_role + ) : role_name => role + if role_name != null } schemas = var.schemas diff --git a/main.tf b/main.tf index bc5cf9e..57f9219 100644 --- a/main.tf +++ b/main.tf @@ -33,15 +33,17 @@ resource "snowflake_database" "this" { enable_console_output = var.enable_console_output } -module "snowflake_database_role" { - for_each = local.roles +module "snowflake_default_role" { + for_each = local.default_roles source = "getindata/database-role/snowflake" - version = "1.1.0" + version = "1.1.1" context = module.this.context - database_name = one(snowflake_database.this[*].name) - name = each.key + database_name = one(snowflake_database.this[*].name) + name = each.key + enabled = local.create_default_roles && lookup(each.value, "enabled", true) + descriptor_name = lookup(each.value, "descriptor_name", "snowflake-role") granted_to_roles = lookup(each.value, "granted_to_roles", []) granted_to_database_roles = lookup(each.value, "granted_to_database_roles", []) @@ -50,6 +52,35 @@ module "snowflake_database_role" { schema_grants = lookup(each.value, "schema_grants", []) attributes = [one(snowflake_database.this[*].name)] + + depends_on = [ + snowflake_database.this + ] +} + +module "snowflake_custom_role" { + for_each = local.custom_roles + + source = "getindata/database-role/snowflake" + version = "1.1.1" + context = module.this.context + + database_name = one(snowflake_database.this[*].name) + name = each.key + enabled = lookup(each.value, "enabled", true) + descriptor_name = lookup(each.value, "descriptor_name", "snowflake-role") + + granted_to_roles = lookup(each.value, "granted_to_roles", []) + granted_to_database_roles = lookup(each.value, "granted_to_database_roles", []) + granted_database_roles = lookup(each.value, "granted_database_roles", []) + database_grants = lookup(each.value, "database_grants", {}) + schema_grants = lookup(each.value, "schema_grants", []) + + attributes = [one(snowflake_database.this[*].name)] + + depends_on = [ + snowflake_database.this + ] } module "snowflake_schema" { @@ -91,4 +122,8 @@ module "snowflake_schema" { roles = each.value.roles create_default_roles = coalesce(each.value.create_default_roles, var.create_default_roles) + + depends_on = [ + snowflake_database.this + ] } diff --git a/variables.tf b/variables.tf index 181232c..c3e937a 100644 --- a/variables.tf +++ b/variables.tf @@ -152,13 +152,13 @@ variable "schemas" { skip_schema_creation = optional(bool, false) descriptor_name = optional(string, "snowflake-schema") comment = optional(string, null) - data_retention_days_time_in_days = optional(number, 1) + data_retention_time_in_days = optional(number, 1) max_data_extension_time_in_days = optional(number, null) - is_transient = optional(bool, false) - with_managed_access = optional(bool, false) + is_transient = optional(bool, null) + with_managed_access = optional(bool, null) external_volume = optional(string, null) catalog = optional(string, null) - replace_invalid_characters = optional(bool, false) + replace_invalid_characters = optional(bool, null) default_ddl_collation = optional(string, null) storage_serialization_policy = optional(string, null) log_level = optional(string, null) @@ -168,23 +168,23 @@ variable "schemas" { user_task_managed_initial_warehouse_size = optional(string, null) user_task_timeout_ms = optional(number, null) user_task_minimum_trigger_interval_in_seconds = optional(number, null) - quoted_identifiers_ignore_case = optional(bool, false) - enable_console_output = optional(bool, false) - pipe_execution_paused = optional(bool, false) + quoted_identifiers_ignore_case = optional(bool, null) + enable_console_output = optional(bool, null) + pipe_execution_paused = optional(bool, null) stages = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-stage") - aws_external_id = optional(string) - comment = optional(string) - copy_options = optional(string) - credentials = optional(string) - directory = optional(string) - encryption = optional(string) - file_format = optional(string) - snowflake_iam_user = optional(string) - storage_integration = optional(string) - url = optional(string) - create_default_stage_roles = optional(bool) + enabled = optional(bool, true) + descriptor_name = optional(string, "snowflake-stage") + aws_external_id = optional(string) + comment = optional(string) + copy_options = optional(string) + credentials = optional(string) + directory = optional(string) + encryption = optional(string) + file_format = optional(string) + snowflake_iam_user = optional(string) + storage_integration = optional(string) + url = optional(string) + create_default_roles = optional(bool) roles = optional(map(object({ enabled = optional(bool, true) descriptor_name = optional(string, "snowflake-role") From 5c093a5d31be90c8784913c6e221b5d0b7b11480 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Wed, 31 Jul 2024 15:43:56 +0200 Subject: [PATCH 09/18] docs: update examples --- examples/complete/README.md | 1 - examples/complete/main.tf | 19 +++++++++++-------- examples/simple/main.tf | 3 +++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/examples/complete/README.md b/examples/complete/README.md index 3c4bb88..1e40971 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -71,5 +71,4 @@ terraform apply tfplan |------|------| | [snowflake_account_role.admin_role](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/account_role) | resource | | [snowflake_account_role.dev_role](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/account_role) | resource | -| [snowflake_user.dbt](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/user) | resource | diff --git a/examples/complete/main.tf b/examples/complete/main.tf index d63470d..917ca6a 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -1,15 +1,9 @@ -resource "snowflake_user" "dbt" { - name = "DBT user" - login_name = "dbt_user" - comment = "DBT user." -} - resource "snowflake_account_role" "admin_role" { - name = "admin" + name = "administrator" } resource "snowflake_account_role" "dev_role" { - name = "dev" + name = "developer" } module "prod_database" { @@ -51,6 +45,9 @@ module "prod_database" { readonly = { granted_to_roles = [snowflake_account_role.dev_role.name] } + transformer = { + enabled = false + } } } silver = { @@ -58,6 +55,9 @@ module "prod_database" { admin = { granted_to_roles = [snowflake_account_role.admin_role.name] } + transformer = { + enabled = false + } } } gold = { @@ -65,6 +65,9 @@ module "prod_database" { admin = { granted_to_roles = [snowflake_account_role.admin_role.name] } + transformer = { + enabled = false + } } } } diff --git a/examples/simple/main.tf b/examples/simple/main.tf index e87a32b..105c6a1 100644 --- a/examples/simple/main.tf +++ b/examples/simple/main.tf @@ -2,4 +2,7 @@ module "database" { source = "../../" name = "analytics" + + log_level = "ERROR" + create_default_roles = true } From 21053807be8409f5f69ab1c7f38be7c81192f063 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Wed, 31 Jul 2024 15:48:44 +0200 Subject: [PATCH 10/18] use provider default values for optional variables --- README.md | 4 ++-- variables.tf | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e54a0e2..89283bf 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [catalog](#input\_catalog) | The database parameter that specifies the default catalog to use for Iceberg tables | `string` | `null` | no | | [comment](#input\_comment) | Specifies a comment for the database | `string` | `null` | no | | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | -| [create\_default\_roles](#input\_create\_default\_roles) | Whether the default database roles should be created | `bool` | `false` | no | +| [create\_default\_roles](#input\_create\_default\_roles) | Whether the default roles should be created | `bool` | `false` | no | | [data\_retention\_time\_in\_days](#input\_data\_retention\_time\_in\_days) | Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table | `number` | `null` | no | | [default\_ddl\_collation](#input\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | `string` | `null` | no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | @@ -98,7 +98,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `null` | no | | [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
}))
| `{}` | no | -| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, 1)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | +| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, null)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_serialization\_policy](#input\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | `string` | `null` | no | | [suspend\_task\_after\_num\_failures](#input\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | `number` | `null` | no | diff --git a/variables.tf b/variables.tf index c3e937a..ee633e7 100644 --- a/variables.tf +++ b/variables.tf @@ -113,7 +113,7 @@ variable "enable_console_output" { } variable "create_default_roles" { - description = "Whether the default database roles should be created" + description = "Whether the default roles should be created" type = bool default = false } @@ -152,7 +152,7 @@ variable "schemas" { skip_schema_creation = optional(bool, false) descriptor_name = optional(string, "snowflake-schema") comment = optional(string, null) - data_retention_time_in_days = optional(number, 1) + data_retention_time_in_days = optional(number, null) max_data_extension_time_in_days = optional(number, null) is_transient = optional(bool, null) with_managed_access = optional(bool, null) From 60b6cec0f6c156599468cf3b7df48e5b89e81524 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Thu, 1 Aug 2024 16:28:04 +0200 Subject: [PATCH 11/18] chore: align variables between deps modules --- README.md | 4 +-- locals.tf | 27 ++++++++++++++---- main.tf | 8 +++--- variables.tf | 77 ++++++++++++++++++++++++++++------------------------ 4 files changed, 69 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 89283bf..c84a86b 100644 --- a/README.md +++ b/README.md @@ -97,8 +97,8 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [quoted\_identifiers\_ignore\_case](#input\_quoted\_identifiers\_ignore\_case) | If true, the case of quoted identifiers is ignored | `bool` | `null` | no | | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `null` | no | -| [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
}))
| `{}` | no | -| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, null)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
})), {})
})), {})
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
table_grants = optional(list(string))
external_table_grants = optional(list(string))
view_grants = optional(list(string))
materialized_view_grants = optional(list(string))
file_format_grants = optional(list(string))
function_grants = optional(list(string))
stage_grants = optional(list(string))
task_grants = optional(list(string))
procedure_grants = optional(list(string))
sequence_grants = optional(list(string))
stream_grants = optional(list(string))
})), {})
}))
| `{}` | no | +| [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
schema_objects_grants = optional(map(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool)
privileges = optional(list(string), null)
object_name = optional(string)
on_all = optional(bool, false)
schema_name = optional(string)
on_future = optional(bool, false)
}))), {})
}))
| `{}` | no | +| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, null)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
create_default_roles = optional(bool)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
with_grant_option = optional(bool)
granted_to_roles = optional(list(string))
granted_to_database_roles = optional(list(string))
granted_database_roles = optional(list(string))
stage_grants = optional(list(string))
all_privileges = optional(bool)
on_all = optional(bool, false)
schema_name = optional(string)
on_future = optional(bool, false)
})), {})
})), {})
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
comment = optional(string)
granted_to_roles = optional(list(string))
granted_to_database_roles = optional(list(string))
granted_database_roles = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
schema_objects_grants = optional(map(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool)
privileges = optional(list(string), null)
object_name = optional(string)
on_all = optional(bool, false)
schema_name = optional(string)
on_future = optional(bool, false)
}))), {})
})), {})
}))
| `{}` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_serialization\_policy](#input\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | `string` | `null` | no | | [suspend\_task\_after\_num\_failures](#input\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | `number` | `null` | no | diff --git a/locals.tf b/locals.tf index 7c8bb55..e62167b 100644 --- a/locals.tf +++ b/locals.tf @@ -5,8 +5,7 @@ locals { lookup(module.database_label.descriptors, var.descriptor_name, module.database_label.id), "/${module.database_label.delimiter}${module.database_label.delimiter}+/", module.database_label.delimiter ), module.database_label.delimiter) : null - enabled = module.this.enabled - create_default_roles = local.enabled && var.create_default_roles + create_default_roles = module.this.enabled && var.create_default_roles #This needs to be the same as an object in roles variable role_template = { @@ -19,6 +18,20 @@ locals { granted_to_users = [] database_grants = {} schema_grants = [] + schema_objects_grants = { + "TABLE" = [] + "DYNAMIC TABLE" = [] + "EXTERNAL TABLE" = [] + "VIEW" = [] + "MATERIALIZED VIEW" = [] + "FILE FORMAT" = [] + "FUNCTION" = [] + "STAGE" = [] + "TASK" = [] + "PROCEDURE" = [] + "SEQUENCE" = [] + "STREAM" = [] + } } default_roles_definition = { @@ -27,8 +40,9 @@ locals { privileges = ["USAGE", "MONITOR"] } schema_grants = [{ - privileges = ["USAGE"] - all_schemas_in_database = true + privileges = ["USAGE"] + all_schemas_in_database = true + future_schemas_in_database = true }] } transformer = { @@ -36,8 +50,9 @@ locals { privileges = ["USAGE", "MONITOR", "CREATE SCHEMA"] } schema_grants = [{ - privileges = ["USAGE", "CREATE TEMPORARY TABLE", "CREATE TAG", "CREATE PIPE", "CREATE PROCEDURE", "CREATE MATERIALIZED VIEW", "CREATE TABLE", "CREATE FILE FORMAT", "CREATE STAGE", "CREATE TASK", "CREATE FUNCTION", "CREATE EXTERNAL TABLE", "CREATE SEQUENCE", "CREATE VIEW", "CREATE STREAM", "CREATE DYNAMIC TABLE"] - all_schemas_in_database = true + privileges = ["USAGE", "CREATE TEMPORARY TABLE", "CREATE TAG", "CREATE PIPE", "CREATE PROCEDURE", "CREATE MATERIALIZED VIEW", "CREATE TABLE", "CREATE FILE FORMAT", "CREATE STAGE", "CREATE TASK", "CREATE FUNCTION", "CREATE EXTERNAL TABLE", "CREATE SEQUENCE", "CREATE VIEW", "CREATE STREAM", "CREATE DYNAMIC TABLE"] + all_schemas_in_database = true + future_schemas_in_database = true }] } admin = { diff --git a/main.tf b/main.tf index 57f9219..ebb78fe 100644 --- a/main.tf +++ b/main.tf @@ -42,6 +42,7 @@ module "snowflake_default_role" { database_name = one(snowflake_database.this[*].name) name = each.key + comment = lookup(each.value, "comment", null) enabled = local.create_default_roles && lookup(each.value, "enabled", true) descriptor_name = lookup(each.value, "descriptor_name", "snowflake-role") @@ -50,8 +51,7 @@ module "snowflake_default_role" { granted_database_roles = lookup(each.value, "granted_database_roles", []) database_grants = lookup(each.value, "database_grants", {}) schema_grants = lookup(each.value, "schema_grants", []) - - attributes = [one(snowflake_database.this[*].name)] + schema_objects_grants = lookup(each.value, "schema_objects_grants", {}) depends_on = [ snowflake_database.this @@ -67,6 +67,7 @@ module "snowflake_custom_role" { database_name = one(snowflake_database.this[*].name) name = each.key + comment = lookup(each.value, "comment", null) enabled = lookup(each.value, "enabled", true) descriptor_name = lookup(each.value, "descriptor_name", "snowflake-role") @@ -75,8 +76,7 @@ module "snowflake_custom_role" { granted_database_roles = lookup(each.value, "granted_database_roles", []) database_grants = lookup(each.value, "database_grants", {}) schema_grants = lookup(each.value, "schema_grants", []) - - attributes = [one(snowflake_database.this[*].name)] + schema_objects_grants = lookup(each.value, "schema_objects_grants", {}) depends_on = [ snowflake_database.this diff --git a/variables.tf b/variables.tf index ee633e7..c4b8ab7 100644 --- a/variables.tf +++ b/variables.tf @@ -122,7 +122,7 @@ variable "roles" { description = "Roles created in the database scope" type = map(object({ enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-role") + descriptor_name = optional(string, "snowflake-database-role") comment = optional(string) role_ownership_grant = optional(string) granted_roles = optional(list(string)) @@ -141,6 +141,15 @@ variable "roles" { future_schemas_in_database = optional(bool, false) schema_name = optional(string, null) }))) + schema_objects_grants = optional(map(list(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool) + privileges = optional(list(string), null) + object_name = optional(string) + on_all = optional(bool, false) + schema_name = optional(string) + on_future = optional(bool, false) + }))), {}) })) default = {} } @@ -171,6 +180,7 @@ variable "schemas" { quoted_identifiers_ignore_case = optional(bool, null) enable_console_output = optional(bool, null) pipe_execution_paused = optional(bool, null) + create_default_roles = optional(bool) stages = optional(map(object({ enabled = optional(bool, true) descriptor_name = optional(string, "snowflake-stage") @@ -186,32 +196,31 @@ variable "schemas" { url = optional(string) create_default_roles = optional(bool) roles = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-role") - comment = optional(string) - role_ownership_grant = optional(string) - granted_roles = optional(list(string)) - granted_to_roles = optional(list(string)) - granted_to_users = optional(list(string)) - schema_grants = optional(list(object({ - all_privileges = optional(bool) - with_grant_option = optional(bool, false) - privileges = optional(list(string), null) - all_schemas_in_database = optional(bool, false) - future_schemas_in_database = optional(bool, false) - schema_name = optional(string, null) - }))) + enabled = optional(bool, true) + descriptor_name = optional(string, "snowflake-database-role") + with_grant_option = optional(bool) + granted_to_roles = optional(list(string)) + granted_to_database_roles = optional(list(string)) + granted_database_roles = optional(list(string)) + stage_grants = optional(list(string)) + all_privileges = optional(bool) + on_all = optional(bool, false) + schema_name = optional(string) + on_future = optional(bool, false) })), {}) })), {}) - create_default_roles = optional(bool) roles = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-role") - comment = optional(string) - role_ownership_grant = optional(string) - granted_roles = optional(list(string)) - granted_to_roles = optional(list(string)) - granted_to_users = optional(list(string)) + enabled = optional(bool, true) + descriptor_name = optional(string, "snowflake-database-role") + comment = optional(string) + granted_to_roles = optional(list(string)) + granted_to_database_roles = optional(list(string)) + granted_database_roles = optional(list(string)) + database_grants = optional(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool, false) + privileges = optional(list(string), null) + })) schema_grants = optional(list(object({ all_privileges = optional(bool) with_grant_option = optional(bool, false) @@ -220,17 +229,15 @@ variable "schemas" { future_schemas_in_database = optional(bool, false) schema_name = optional(string, null) }))) - table_grants = optional(list(string)) - external_table_grants = optional(list(string)) - view_grants = optional(list(string)) - materialized_view_grants = optional(list(string)) - file_format_grants = optional(list(string)) - function_grants = optional(list(string)) - stage_grants = optional(list(string)) - task_grants = optional(list(string)) - procedure_grants = optional(list(string)) - sequence_grants = optional(list(string)) - stream_grants = optional(list(string)) + schema_objects_grants = optional(map(list(object({ + all_privileges = optional(bool) + with_grant_option = optional(bool) + privileges = optional(list(string), null) + object_name = optional(string) + on_all = optional(bool, false) + schema_name = optional(string) + on_future = optional(bool, false) + }))), {}) })), {}) })) default = {} From 0302335ca841d2e24ba4c53db9b45e0b5f03bb2e Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Fri, 2 Aug 2024 08:29:43 +0200 Subject: [PATCH 12/18] chore: align locals --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index ebb78fe..dc75845 100644 --- a/main.tf +++ b/main.tf @@ -90,7 +90,7 @@ module "snowflake_schema" { version = "2.0.0" context = module.this.context - enabled = local.enabled && each.value.enabled + enabled = module.this.enabled && each.value.enabled descriptor_name = each.value.descriptor_name name = each.key From d5653e00a5996617299636a7066414063de0149a Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Fri, 2 Aug 2024 14:12:15 +0200 Subject: [PATCH 13/18] chore: align to schema and stage vars --- README.md | 4 ++-- main.tf | 2 +- variables.tf | 14 +------------- 3 files changed, 4 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c84a86b..29443e8 100644 --- a/README.md +++ b/README.md @@ -98,7 +98,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.
Characters matching the regex will be removed from the ID elements.
If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no | | [replace\_invalid\_characters](#input\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | `bool` | `null` | no | | [roles](#input\_roles) | Roles created in the database scope |
map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
comment = optional(string)
role_ownership_grant = optional(string)
granted_roles = optional(list(string))
granted_to_roles = optional(list(string))
granted_to_users = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
schema_objects_grants = optional(map(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool)
privileges = optional(list(string), null)
object_name = optional(string)
on_all = optional(bool, false)
schema_name = optional(string)
on_future = optional(bool, false)
}))), {})
}))
| `{}` | no | -| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, null)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
create_default_roles = optional(bool)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
with_grant_option = optional(bool)
granted_to_roles = optional(list(string))
granted_to_database_roles = optional(list(string))
granted_database_roles = optional(list(string))
stage_grants = optional(list(string))
all_privileges = optional(bool)
on_all = optional(bool, false)
schema_name = optional(string)
on_future = optional(bool, false)
})), {})
})), {})
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
comment = optional(string)
granted_to_roles = optional(list(string))
granted_to_database_roles = optional(list(string))
granted_database_roles = optional(list(string))
database_grants = optional(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
}))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
all_schemas_in_database = optional(bool, false)
future_schemas_in_database = optional(bool, false)
schema_name = optional(string, null)
})))
schema_objects_grants = optional(map(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool)
privileges = optional(list(string), null)
object_name = optional(string)
on_all = optional(bool, false)
schema_name = optional(string)
on_future = optional(bool, false)
}))), {})
})), {})
}))
| `{}` | no | +| [schemas](#input\_schemas) | Schemas to be created in the database |
map(object({
enabled = optional(bool, true)
skip_schema_creation = optional(bool, false)
descriptor_name = optional(string, "snowflake-schema")
comment = optional(string, null)
data_retention_time_in_days = optional(number, null)
max_data_extension_time_in_days = optional(number, null)
is_transient = optional(bool, null)
with_managed_access = optional(bool, null)
external_volume = optional(string, null)
catalog = optional(string, null)
replace_invalid_characters = optional(bool, null)
default_ddl_collation = optional(string, null)
storage_serialization_policy = optional(string, null)
log_level = optional(string, null)
trace_level = optional(string, null)
suspend_task_after_num_failures = optional(number, null)
task_auto_retry_attempts = optional(number, null)
user_task_managed_initial_warehouse_size = optional(string, null)
user_task_timeout_ms = optional(number, null)
user_task_minimum_trigger_interval_in_seconds = optional(number, null)
quoted_identifiers_ignore_case = optional(bool, null)
enable_console_output = optional(bool, null)
pipe_execution_paused = optional(bool, null)
create_default_roles = optional(bool)
stages = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-stage")
aws_external_id = optional(string)
comment = optional(string)
copy_options = optional(string)
credentials = optional(string)
directory = optional(string)
encryption = optional(string)
file_format = optional(string)
snowflake_iam_user = optional(string)
storage_integration = optional(string)
url = optional(string)
create_default_roles = optional(bool)
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
with_grant_option = optional(bool)
granted_to_roles = optional(list(string))
granted_to_database_roles = optional(list(string))
granted_database_roles = optional(list(string))
stage_grants = optional(list(string))
all_privileges = optional(bool)
})), {})
})), {})
roles = optional(map(object({
enabled = optional(bool, true)
descriptor_name = optional(string, "snowflake-database-role")
comment = optional(string)
granted_to_roles = optional(list(string))
granted_to_database_roles = optional(list(string))
granted_database_roles = optional(list(string))
schema_grants = optional(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool, false)
privileges = optional(list(string), null)
})))
schema_objects_grants = optional(map(list(object({
all_privileges = optional(bool)
with_grant_option = optional(bool)
privileges = optional(list(string), null)
object_name = optional(string)
on_all = optional(bool, false)
on_future = optional(bool, false)
}))), {})
})), {})
}))
| `{}` | no | | [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no | | [storage\_serialization\_policy](#input\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | `string` | `null` | no | | [suspend\_task\_after\_num\_failures](#input\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | `number` | `null` | no | @@ -118,7 +118,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [roles\_deep\_merge](#module\_roles\_deep\_merge) | Invicton-Labs/deepmerge/null | 0.1.5 | | [snowflake\_custom\_role](#module\_snowflake\_custom\_role) | getindata/database-role/snowflake | 1.1.1 | | [snowflake\_default\_role](#module\_snowflake\_default\_role) | getindata/database-role/snowflake | 1.1.1 | -| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | 2.0.0 | +| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | 2.0.1 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | ## Outputs diff --git a/main.tf b/main.tf index dc75845..a8e1b28 100644 --- a/main.tf +++ b/main.tf @@ -87,7 +87,7 @@ module "snowflake_schema" { for_each = local.schemas source = "getindata/schema/snowflake" - version = "2.0.0" + version = "2.0.1" context = module.this.context enabled = module.this.enabled && each.value.enabled diff --git a/variables.tf b/variables.tf index c4b8ab7..6fc5dd1 100644 --- a/variables.tf +++ b/variables.tf @@ -204,9 +204,6 @@ variable "schemas" { granted_database_roles = optional(list(string)) stage_grants = optional(list(string)) all_privileges = optional(bool) - on_all = optional(bool, false) - schema_name = optional(string) - on_future = optional(bool, false) })), {}) })), {}) roles = optional(map(object({ @@ -216,18 +213,10 @@ variable "schemas" { granted_to_roles = optional(list(string)) granted_to_database_roles = optional(list(string)) granted_database_roles = optional(list(string)) - database_grants = optional(object({ + schema_grants = optional(list(object({ all_privileges = optional(bool) with_grant_option = optional(bool, false) privileges = optional(list(string), null) - })) - schema_grants = optional(list(object({ - all_privileges = optional(bool) - with_grant_option = optional(bool, false) - privileges = optional(list(string), null) - all_schemas_in_database = optional(bool, false) - future_schemas_in_database = optional(bool, false) - schema_name = optional(string, null) }))) schema_objects_grants = optional(map(list(object({ all_privileges = optional(bool) @@ -235,7 +224,6 @@ variable "schemas" { privileges = optional(list(string), null) object_name = optional(string) on_all = optional(bool, false) - schema_name = optional(string) on_future = optional(bool, false) }))), {}) })), {}) From 08518a7872e75bd2019090939742ba1f049ff4b6 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Fri, 2 Aug 2024 14:29:50 +0200 Subject: [PATCH 14/18] docs: align examples --- examples/complete/fixtures.tfvars | 2 +- examples/complete/main.tf | 1 + locals.tf | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/complete/fixtures.tfvars b/examples/complete/fixtures.tfvars index 5c706f7..4584d42 100644 --- a/examples/complete/fixtures.tfvars +++ b/examples/complete/fixtures.tfvars @@ -1,5 +1,5 @@ descriptor_formats = { - snowflake-role = { + snowflake-database-role = { labels = ["attributes", "name"] format = "%v_%v" } diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 917ca6a..aba2210 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -33,6 +33,7 @@ module "prod_database" { schemas = { bronze = { data_retention_time_in_days = 7 + comment = "my bronze schema" stages = { example = { comment = "my example stage" diff --git a/locals.tf b/locals.tf index e62167b..7cd4062 100644 --- a/locals.tf +++ b/locals.tf @@ -91,7 +91,7 @@ locals { roles = { for role_name, role in merge( - module.snowflake_default_role, + var.create_default_roles ? module.snowflake_default_role : {}, module.snowflake_custom_role ) : role_name => role if role_name != null From a72d65bc524ced692fba6a90a26e6f5927c26e2c Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Fri, 2 Aug 2024 14:37:30 +0200 Subject: [PATCH 15/18] chore: more readable output --- README.md | 2 +- main.tf | 2 +- outputs.tf | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 29443e8..bdd3692 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re |------|-------------| | [catalog](#output\_catalog) | The database parameter that specifies the default catalog to use for Iceberg tables | | [data\_retention\_time\_in\_days](#output\_data\_retention\_time\_in\_days) | Data retention days for the database | +| [database\_roles](#output\_database\_roles) | Snowflake Database roles | | [default\_ddl\_collation](#output\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | | [enable\_console\_output](#output\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | | [external\_volume](#output\_external\_volume) | The database parameter that specifies the default external volume to use for Iceberg tables | @@ -136,7 +137,6 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [name](#output\_name) | Name of the database | | [quoted\_identifiers\_ignore\_case](#output\_quoted\_identifiers\_ignore\_case) | If true, the case of quoted identifiers is ignored | | [replace\_invalid\_characters](#output\_replace\_invalid\_characters) | Specifies whether to replace invalid UTF-8 characters with the Unicode replacement character () in query results for an Iceberg table | -| [roles](#output\_roles) | Snowflake Database roles | | [schemas](#output\_schemas) | This database schemas | | [storage\_serialization\_policy](#output\_storage\_serialization\_policy) | The storage serialization policy for Iceberg tables that use Snowflake as the catalog. Valid options are: [COMPATIBLE OPTIMIZED] | | [suspend\_task\_after\_num\_failures](#output\_suspend\_task\_after\_num\_failures) | How many times a task must fail in a row before it is automatically suspended. 0 disables auto-suspending | diff --git a/main.tf b/main.tf index a8e1b28..71cb4f5 100644 --- a/main.tf +++ b/main.tf @@ -68,7 +68,7 @@ module "snowflake_custom_role" { database_name = one(snowflake_database.this[*].name) name = each.key comment = lookup(each.value, "comment", null) - enabled = lookup(each.value, "enabled", true) + enabled = module.this.enabled && lookup(each.value, "enabled", true) descriptor_name = lookup(each.value, "descriptor_name", "snowflake-role") granted_to_roles = lookup(each.value, "granted_to_roles", []) diff --git a/outputs.tf b/outputs.tf index 6df50c0..fd63ece 100644 --- a/outputs.tf +++ b/outputs.tf @@ -88,7 +88,7 @@ output "enable_console_output" { value = one(snowflake_database.this[*].enable_console_output) } -output "roles" { +output "database_roles" { description = "Snowflake Database roles" value = local.roles } From 54e18b37b15ad1678996894c9e3892aec691aae2 Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Fri, 2 Aug 2024 16:41:17 +0200 Subject: [PATCH 16/18] feat: database ownership configuration --- README.md | 4 ++++ examples/complete/main.tf | 2 ++ main.tf | 18 +++++++++++++++++- outputs.tf | 5 +++++ variables.tf | 6 ++++++ 5 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bdd3692..77da87e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Terraform module for Snowflake database management. * `TRANSFORMER` - allows creating schemas and some Snowflake objects in them * `ADMIN` - full access, including database options like `data_retention_time_in_days` * Can create number of schemas in the database with their specific stages and access roles +* Can create database ownership to specified account role ## USAGE @@ -76,6 +77,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | | [create\_default\_roles](#input\_create\_default\_roles) | Whether the default roles should be created | `bool` | `false` | no | | [data\_retention\_time\_in\_days](#input\_data\_retention\_time\_in\_days) | Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table | `number` | `null` | no | +| [database\_ownership\_account\_role\_name](#input\_database\_ownership\_account\_role\_name) | The fully qualified name of the account role to which database privileges will be granted | `string` | `null` | no | | [default\_ddl\_collation](#input\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | `string` | `null` | no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | @@ -127,6 +129,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re |------|-------------| | [catalog](#output\_catalog) | The database parameter that specifies the default catalog to use for Iceberg tables | | [data\_retention\_time\_in\_days](#output\_data\_retention\_time\_in\_days) | Data retention days for the database | +| [database\_ownership\_account\_role\_name](#output\_database\_ownership\_account\_role\_name) | The fully qualified name of the account role to which database ownership will be granted | | [database\_roles](#output\_database\_roles) | Snowflake Database roles | | [default\_ddl\_collation](#output\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | | [enable\_console\_output](#output\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | @@ -164,6 +167,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | Name | Type | |------|------| | [snowflake_database.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/database) | resource | +| [snowflake_grant_ownership.database_ownership](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/grant_ownership) | resource | ## CONTRIBUTING diff --git a/examples/complete/main.tf b/examples/complete/main.tf index aba2210..a57f3d0 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -18,6 +18,8 @@ module "prod_database" { create_default_roles = true + database_ownership_account_role_name = snowflake_account_role.admin_role.name + roles = { readonly = { granted_to_roles = [snowflake_account_role.dev_role.name] diff --git a/main.tf b/main.tf index 71cb4f5..e8a5966 100644 --- a/main.tf +++ b/main.tf @@ -124,6 +124,22 @@ module "snowflake_schema" { create_default_roles = coalesce(each.value.create_default_roles, var.create_default_roles) depends_on = [ - snowflake_database.this + module.snowflake_default_role, + module.snowflake_custom_role + ] +} + +resource "snowflake_grant_ownership" "database_ownership" { + count = var.database_ownership_account_role_name != null ? 1 : 0 + + account_role_name = var.database_ownership_account_role_name + outbound_privileges = "REVOKE" + on { + object_type = "DATABASE" + object_name = one(snowflake_database.this[*].name) + } + + depends_on = [ + module.snowflake_schema ] } diff --git a/outputs.tf b/outputs.tf index fd63ece..70392c1 100644 --- a/outputs.tf +++ b/outputs.tf @@ -97,3 +97,8 @@ output "schemas" { description = "This database schemas" value = module.snowflake_schema } + +output "database_ownership_account_role_name" { + description = "The fully qualified name of the account role to which database ownership will be granted" + value = one(snowflake_grant_ownership.database_ownership[*]) +} diff --git a/variables.tf b/variables.tf index 6fc5dd1..df534ef 100644 --- a/variables.tf +++ b/variables.tf @@ -230,3 +230,9 @@ variable "schemas" { })) default = {} } + +variable "database_ownership_account_role_name" { + description = "The fully qualified name of the account role to which database privileges will be granted" + type = string + default = null +} From e6674373fed944ad437e995c722b8281646d860d Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Mon, 5 Aug 2024 09:11:16 +0200 Subject: [PATCH 17/18] chore: rename database ownership variables --- README.md | 4 ++-- main.tf | 4 ++-- outputs.tf | 4 ++-- variables.tf | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 77da87e..f407638 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | [context](#input\_context) | Single object for setting entire context at once.
See description of individual variables for details.
Leave string and numeric variables as `null` to use default value.
Individual variable settings (non-null) override settings in context object,
except for attributes, tags, and additional\_tag\_map, which are merged. | `any` |
{
"additional_tag_map": {},
"attributes": [],
"delimiter": null,
"descriptor_formats": {},
"enabled": true,
"environment": null,
"id_length_limit": null,
"label_key_case": null,
"label_order": [],
"label_value_case": null,
"labels_as_tags": [
"unset"
],
"name": null,
"namespace": null,
"regex_replace_chars": null,
"stage": null,
"tags": {},
"tenant": null
}
| no | | [create\_default\_roles](#input\_create\_default\_roles) | Whether the default roles should be created | `bool` | `false` | no | | [data\_retention\_time\_in\_days](#input\_data\_retention\_time\_in\_days) | Number of days for which Snowflake retains historical data for performing Time Travel actions (SELECT, CLONE, UNDROP) on the object. A value of 0 effectively disables Time Travel for the specified database, schema, or table | `number` | `null` | no | -| [database\_ownership\_account\_role\_name](#input\_database\_ownership\_account\_role\_name) | The fully qualified name of the account role to which database privileges will be granted | `string` | `null` | no | +| [database\_ownership\_grant](#input\_database\_ownership\_grant) | The name of the account role to which database privileges will be granted | `string` | `null` | no | | [default\_ddl\_collation](#input\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | `string` | `null` | no | | [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.
Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no | | [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.
Map of maps. Keys are names of descriptors. Values are maps of the form
`{
format = string
labels = list(string)
}`
(Type is `any` so the map values can later be enhanced to provide additional options.)
`format` is a Terraform format string to be passed to the `format()` function.
`labels` is a list of labels, in order, to pass to `format()` function.
Label values will be normalized before being passed to `format()` so they will be
identical to how they appear in `id`.
Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no | @@ -129,7 +129,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re |------|-------------| | [catalog](#output\_catalog) | The database parameter that specifies the default catalog to use for Iceberg tables | | [data\_retention\_time\_in\_days](#output\_data\_retention\_time\_in\_days) | Data retention days for the database | -| [database\_ownership\_account\_role\_name](#output\_database\_ownership\_account\_role\_name) | The fully qualified name of the account role to which database ownership will be granted | +| [database\_ownership\_grant](#output\_database\_ownership\_grant) | The name of the account role to which database ownership will be granted | | [database\_roles](#output\_database\_roles) | Snowflake Database roles | | [default\_ddl\_collation](#output\_default\_ddl\_collation) | Specifies a default collation specification for all schemas and tables added to the database. | | [enable\_console\_output](#output\_enable\_console\_output) | If true, enables stdout/stderr fast path logging for anonymous stored procedures | diff --git a/main.tf b/main.tf index e8a5966..13c0cee 100644 --- a/main.tf +++ b/main.tf @@ -130,9 +130,9 @@ module "snowflake_schema" { } resource "snowflake_grant_ownership" "database_ownership" { - count = var.database_ownership_account_role_name != null ? 1 : 0 + count = var.database_ownership_grant != null ? 1 : 0 - account_role_name = var.database_ownership_account_role_name + account_role_name = var.database_ownership_grant outbound_privileges = "REVOKE" on { object_type = "DATABASE" diff --git a/outputs.tf b/outputs.tf index 70392c1..b61f1be 100644 --- a/outputs.tf +++ b/outputs.tf @@ -98,7 +98,7 @@ output "schemas" { value = module.snowflake_schema } -output "database_ownership_account_role_name" { - description = "The fully qualified name of the account role to which database ownership will be granted" +output "database_ownership_grant" { + description = "The name of the account role to which database ownership will be granted" value = one(snowflake_grant_ownership.database_ownership[*]) } diff --git a/variables.tf b/variables.tf index df534ef..f312793 100644 --- a/variables.tf +++ b/variables.tf @@ -231,8 +231,8 @@ variable "schemas" { default = {} } -variable "database_ownership_account_role_name" { - description = "The fully qualified name of the account role to which database privileges will be granted" +variable "database_ownership_grant" { + description = "The name of the account role to which database privileges will be granted" type = string default = null } From 6997abcea83a468442cf32119ffb95ffc35e2bbf Mon Sep 17 00:00:00 2001 From: Piotr Mossakowski Date: Mon, 5 Aug 2024 09:15:24 +0200 Subject: [PATCH 18/18] docs: align example variables --- examples/complete/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/complete/main.tf b/examples/complete/main.tf index a57f3d0..42d81d0 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -18,7 +18,7 @@ module "prod_database" { create_default_roles = true - database_ownership_account_role_name = snowflake_account_role.admin_role.name + database_ownership_grant = snowflake_account_role.admin_role.name roles = { readonly = {