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..3b6ace1 100644 --- a/.tflint.hcl +++ b/.tflint.hcl @@ -1,16 +1,3 @@ -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" -} - rule "terraform_standard_module_structure" { - enabled = false # Fails on context.tf + enabled = false # Fails on context.tf } diff --git a/README.md b/README.md index 55162ab..f407638 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,13 @@ 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 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 +* Can create database ownership to specified account role ## USAGE @@ -42,6 +43,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 +- 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 +- 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 +72,45 @@ 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 | +| [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\_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 | | [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` | `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 | -| [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 | +| [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 | | [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` | `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 | -| [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` | `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)
})), {})
})), {})
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 | | [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,42 +118,56 @@ 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\_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.1 | | [this](#module\_this) | cloudposse/label/null | 0.25.0 | ## Outputs | 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 | +| [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 | +| [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 | -| [roles](#output\_roles) | Snowflake Database roles | +| [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 | | [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 | 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 | +| [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/README.md b/examples/complete/README.md index 5d58994..1e40971 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,65 @@ 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 | + 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 e7d5767..42d81d0 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -1,21 +1,9 @@ -resource "snowflake_user" "dbt" { - name = "DBT user" - login_name = "dbt_user" - comment = "DBT user." +resource "snowflake_account_role" "admin_role" { + name = "administrator" } -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" +resource "snowflake_account_role" "dev_role" { + name = "developer" } module "prod_database" { @@ -30,52 +18,60 @@ module "prod_database" { create_default_roles = true + database_ownership_grant = snowflake_account_role.admin_role.name + 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 + comment = "my bronze schema" 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] + } + transformer = { + enabled = false } } } silver = { roles = { admin = { - granted_to_roles = [module.snowflake_admin_role.name] + granted_to_roles = [snowflake_account_role.admin_role.name] + } + transformer = { + enabled = false } } } gold = { roles = { admin = { - granted_to_roles = [module.snowflake_admin_role.name] + granted_to_roles = [snowflake_account_role.admin_role.name] + } + transformer = { + enabled = false } } } } } - - -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/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 } 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..7cd4062 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 = { @@ -17,21 +16,54 @@ locals { granted_roles = [] granted_to_roles = [] granted_to_users = [] - database_grants = [] + 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 = { readonly = { - database_grants = ["USAGE", "MONITOR"] - schema_grants = ["USAGE"] + database_grants = { + privileges = ["USAGE", "MONITOR"] + } + schema_grants = [{ + privileges = ["USAGE"] + all_schemas_in_database = true + future_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 + future_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 + }] } } @@ -59,10 +91,10 @@ 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 + if role_name != null } schemas = var.schemas diff --git a/main.tf b/main.tf index f9ca770..13c0cee 100644 --- a/main.tf +++ b/main.tf @@ -11,111 +11,135 @@ 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)] + source = "getindata/database-role/snowflake" + version = "1.1.1" + context = module.this.context - 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 + 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") + + 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", []) + schema_objects_grants = lookup(each.value, "schema_objects_grants", {}) + + depends_on = [ + snowflake_database.this + ] } module "snowflake_custom_role" { for_each = local.custom_roles - source = "getindata/role/snowflake" - version = "1.4.0" - - context = module.this.context - enabled = local.enabled && each.value.enabled - descriptor_name = each.value.descriptor_name - - name = each.key - attributes = [one(snowflake_database.this[*].name)] + source = "getindata/database-role/snowflake" + version = "1.1.1" + context = module.this.context - 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 + database_name = one(snowflake_database.this[*].name) + name = each.key + comment = lookup(each.value, "comment", null) + 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", []) + 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", []) + schema_objects_grants = lookup(each.value, "schema_objects_grants", {}) + + depends_on = [ + snowflake_database.this + ] } module "snowflake_schema" { for_each = local.schemas source = "getindata/schema/snowflake" - version = "1.5.0" + version = "2.0.1" 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 - 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) - }) : {} + create_default_roles = coalesce(each.value.create_default_roles, var.create_default_roles) - database_name = one(snowflake_database.this[*].name) - privilege = each.key - roles = each.value + depends_on = [ + module.snowflake_default_role, + module.snowflake_custom_role + ] } -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_grant_ownership" "database_ownership" { + count = var.database_ownership_grant != null ? 1 : 0 -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) - }) : {} + account_role_name = var.database_ownership_grant + outbound_privileges = "REVOKE" + on { + object_type = "DATABASE" + object_name = one(snowflake_database.this[*].name) + } - database_name = one(snowflake_database.this[*].name) - on_all = true - privilege = each.key - roles = each.value + depends_on = [ + module.snowflake_schema + ] } diff --git a/outputs.tf b/outputs.tf index fc83142..b61f1be 100644 --- a/outputs.tf +++ b/outputs.tf @@ -8,12 +8,87 @@ 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 "roles" { +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 "database_roles" { description = "Snowflake Database roles" value = local.roles } @@ -22,3 +97,8 @@ output "schemas" { description = "This database schemas" value = module.snowflake_schema } + +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 c9a8a50..f312793 100644 --- a/variables.tf +++ b/variables.tf @@ -13,34 +13,103 @@ 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" { 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" { + 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 "from_database" { - description = "Specify a database to create a clone from" +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 = null +} + +variable "default_ddl_collation" { + description = "Specifies a default collation specification for all schemas and tables added to the database." type = string default = null } -variable "from_replica" { - description = "Specify a fully-qualified path to a database to create a replica from" +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_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 "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 "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 = null +} + +variable "enable_console_output" { + description = "If true, enables stdout/stderr fast path logging for anonymous stored procedures" + type = bool + default = null } variable "create_default_roles" { @@ -53,14 +122,34 @@ 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)) 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) + }))) + 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 = {} } @@ -68,13 +157,30 @@ 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_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") @@ -90,40 +196,43 @@ 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)) - stage_grants = optional(list(string)) + 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) })), {}) })), {}) - 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)) + 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) + }))), {}) })), {}) })) default = {} } + +variable "database_ownership_grant" { + description = "The name of the account role to which database privileges will be granted" + type = string + default = null +} 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" } } }