diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index d145559..1f08dec 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -6,8 +6,6 @@ repos: - 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.18.0" # Get the latest from: https://github.com/terraform-docs/terraform-docs/releases diff --git a/.tflint.hcl b/.tflint.hcl deleted file mode 100644 index 3b6ace1..0000000 --- a/.tflint.hcl +++ /dev/null @@ -1,3 +0,0 @@ -rule "terraform_standard_module_structure" { - enabled = false # Fails on context.tf -} diff --git a/README.md b/README.md index 13f30f3..b060e9a 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,14 @@ Terraform module for Snowflake database management. -* Creates Snowflake 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 stages and access roles -* Can create database ownership to specified account role +- Creates Snowflake 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 stages and access roles +- Can create database ownership to specified account role ## USAGE @@ -44,10 +44,12 @@ module "snowflake_database" { - [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 @@ -58,9 +60,39 @@ List of code and variable (API) changes: 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. +## Breaking changes in v3.x of the module + +Due to replacement of nulllabel (`context.tf`) with context provider, some **breaking changes** were introduced in `v3.0.0` version of this module. + +List od code and variable (API) changes: + +- Removed `context.tf` file (a single-file module with additional variables), which implied a removal of all its variables (except `name`): + - `descriptor_formats` + - `label_value_case` + - `label_key_case` + - `id_length_limit` + - `regex_replace_chars` + - `label_order` + - `additional_tag_map` + - `tags` + - `labels_as_tags` + - `attributes` + - `delimiter` + - `stage` + - `environment` + - `tenant` + - `namespace` + - `enabled` + - `context` +- Remove support `enabled` flag - that might cause some backward compatibility issues with terraform state (please take into account that proper `move` clauses were added to minimize the impact), but proceed with caution +- Additional `context` provider configuration +- New variables were added, to allow naming configuration via `context` provider: + - `context_templates` + - `name_schema` + - `drop_public_schema_on_creation` which is `true` by default + @@ -70,43 +102,28 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | 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 | | [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 | +| [context\_templates](#input\_context\_templates) | Map of context templates used for naming conventions - this variable supersedes `naming_scheme.properties` and `naming_scheme.delimiter` configuration | `map(string)` | `{}` | 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\_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 | +| [drop\_public\_schema\_on\_creation](#input\_drop\_public\_schema\_on\_creation) | Whether the `PUBLIC` schema should be dropped after the database creation | `bool` | `true` | 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` | `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 | +| [name](#input\_name) | Name of the resource | `string` | n/a | yes | +| [name\_scheme](#input\_name\_scheme) | Naming scheme configuration for the resource. This configuration is used to generate names using context provider:
- `properties` - list of properties to use when creating the name - is superseded by `var.context_templates`
- `delimiter` - delimited used to create the name from `properties` - is superseded by `var.context_templates`
- `context_template_name` - name of the context template used to create the name
- `replace_chars_regex` - regex to use for replacing characters in property-values created by the provider - any characters that match the regex will be removed from the name
- `extra_values` - map of extra label-value pairs, used to create a name |
object({
properties = optional(list(string), ["environment", "name"])
delimiter = optional(string, "_")
context_template_name = optional(string, "snowflake-database")
replace_chars_regex = optional(string, "[^a-zA-Z0-9_]")
extra_values = optional(map(string))
})
| `{}` | 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` | `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 | +| [roles](#input\_roles) | Roles created in the database scope |
map(object({
name_scheme = optional(object({
properties = optional(list(string))
delimiter = optional(string)
context_template_name = optional(string)
replace_chars_regex = optional(string)
extra_labels = optional(map(string))
}))
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({
name_scheme = optional(object({
properties = optional(list(string))
delimiter = optional(string)
context_template_name = optional(string)
replace_chars_regex = optional(string)
extra_labels = optional(map(string))
}))
skip_schema_creation = optional(bool, false)
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({
name_scheme = optional(object({
properties = optional(list(string))
delimiter = optional(string)
context_template_name = optional(string)
replace_chars_regex = optional(string)
extra_labels = optional(map(string))
}))
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({
name_scheme = optional(object({
properties = optional(list(string))
delimiter = optional(string)
context_template_name = optional(string)
replace_chars_regex = optional(string)
extra_labels = optional(map(string))
}))
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({
name_scheme = optional(object({
properties = optional(list(string))
delimiter = optional(string)
context_template_name = optional(string)
replace_chars_regex = optional(string)
extra_labels = optional(map(string))
}))
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 | | [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 | @@ -116,12 +133,10 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | Name | Source | Version | |------|--------|---------| -| [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/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.2 | -| [this](#module\_this) | cloudposse/label/null | 0.25.0 | +| [snowflake\_custom\_role](#module\_snowflake\_custom\_role) | getindata/database-role/snowflake | 2.0.1 | +| [snowflake\_default\_role](#module\_snowflake\_default\_role) | getindata/database-role/snowflake | 2.0.1 | +| [snowflake\_schema](#module\_snowflake\_schema) | getindata/schema/snowflake | 3.0.0 | ## Outputs @@ -153,14 +168,16 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re | Name | Version | |------|---------| -| [snowflake](#provider\_snowflake) | ~> 0.90 | +| [context](#provider\_context) | >=0.4.0 | +| [snowflake](#provider\_snowflake) | ~> 0.95 | ## Requirements | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [snowflake](#requirement\_snowflake) | ~> 0.90 | +| [context](#requirement\_context) | >=0.4.0 | +| [snowflake](#requirement\_snowflake) | ~> 0.95 | ## Resources @@ -168,6 +185,7 @@ When upgrading from `v1.x`, expect most of the resources to be recreated - if re |------|------| | [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 | +| [context_label.this](https://registry.terraform.io/providers/cloudposse/context/latest/docs/data-sources/label) | data source | ## CONTRIBUTING diff --git a/context.tf b/context.tf deleted file mode 100644 index 5e0ef88..0000000 --- a/context.tf +++ /dev/null @@ -1,279 +0,0 @@ -# -# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label -# All other instances of this file should be a copy of that one -# -# -# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf -# and then place it in your Terraform module to automatically get -# Cloud Posse's standard configuration inputs suitable for passing -# to Cloud Posse modules. -# -# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf -# -# Modules should access the whole context as `module.this.context` -# to get the input variables with nulls for defaults, -# for example `context = module.this.context`, -# and access individual variables as `module.this.`, -# with final values filled in. -# -# For example, when using defaults, `module.this.context.delimiter` -# will be null, and `module.this.delimiter` will be `-` (hyphen). -# - -module "this" { - source = "cloudposse/label/null" - version = "0.25.0" # requires Terraform >= 0.13.0 - - enabled = var.enabled - namespace = var.namespace - tenant = var.tenant - environment = var.environment - stage = var.stage - name = var.name - delimiter = var.delimiter - attributes = var.attributes - tags = var.tags - additional_tag_map = var.additional_tag_map - label_order = var.label_order - regex_replace_chars = var.regex_replace_chars - id_length_limit = var.id_length_limit - label_key_case = var.label_key_case - label_value_case = var.label_value_case - descriptor_formats = var.descriptor_formats - labels_as_tags = var.labels_as_tags - - context = var.context -} - -# Copy contents of cloudposse/terraform-null-label/variables.tf here - -variable "context" { - type = any - default = { - enabled = true - namespace = null - tenant = null - environment = null - stage = null - name = null - delimiter = null - attributes = [] - tags = {} - additional_tag_map = {} - regex_replace_chars = null - label_order = [] - id_length_limit = null - label_key_case = null - label_value_case = null - descriptor_formats = {} - # Note: we have to use [] instead of null for unset lists due to - # https://github.com/hashicorp/terraform/issues/28137 - # which was not fixed until Terraform 1.0.0, - # but we want the default to be all the labels in `label_order` - # and we want users to be able to prevent all tag generation - # by setting `labels_as_tags` to `[]`, so we need - # a different sentinel to indicate "default" - labels_as_tags = ["unset"] - } - description = <<-EOT - 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. - EOT - - validation { - condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) - error_message = "Allowed values: `lower`, `title`, `upper`." - } - - validation { - condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) - error_message = "Allowed values: `lower`, `title`, `upper`, `none`." - } -} - -variable "enabled" { - type = bool - default = null - description = "Set to false to prevent the module from creating any resources" -} - -variable "namespace" { - type = string - default = null - description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" -} - -variable "tenant" { - type = string - default = null - description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" -} - -variable "environment" { - type = string - default = null - description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" -} - -variable "stage" { - type = string - default = null - description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" -} - -variable "name" { - type = string - default = null - description = <<-EOT - 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. - EOT -} - -variable "delimiter" { - type = string - default = null - description = <<-EOT - Delimiter to be used between ID elements. - Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. - EOT -} - -variable "attributes" { - type = list(string) - default = [] - description = <<-EOT - 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. - EOT -} - -variable "labels_as_tags" { - type = set(string) - default = ["default"] - description = <<-EOT - 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. - EOT -} - -variable "tags" { - type = map(string) - default = {} - description = <<-EOT - Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). - Neither the tag keys nor the tag values will be modified by this module. - EOT -} - -variable "additional_tag_map" { - type = map(string) - default = {} - description = <<-EOT - 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. - EOT -} - -variable "label_order" { - type = list(string) - default = null - description = <<-EOT - 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. - EOT -} - -variable "regex_replace_chars" { - type = string - default = null - description = <<-EOT - 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. - EOT -} - -variable "id_length_limit" { - type = number - default = null - description = <<-EOT - 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`. - EOT - validation { - condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 - error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." - } -} - -variable "label_key_case" { - type = string - default = null - description = <<-EOT - 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`. - EOT - - validation { - condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) - error_message = "Allowed values: `lower`, `title`, `upper`." - } -} - -variable "label_value_case" { - type = string - default = null - description = <<-EOT - 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`. - EOT - - validation { - condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) - error_message = "Allowed values: `lower`, `title`, `upper`, `none`." - } -} - -variable "descriptor_formats" { - type = any - default = {} - description = <<-EOT - 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). - EOT -} - -#### End of copy of cloudposse/terraform-null-label/variables.tf diff --git a/examples/complete/README.md b/examples/complete/README.md index 1e40971..958d103 100644 --- a/examples/complete/README.md +++ b/examples/complete/README.md @@ -20,50 +20,35 @@ terraform apply tfplan | 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 | +| [context\_templates](#input\_context\_templates) | A map of context templates used to generate names | `map(string)` | n/a | yes | ## Modules | Name | Source | Version | |------|--------|---------| -| [prod\_database](#module\_prod\_database) | ../../ | n/a | -| [this](#module\_this) | cloudposse/label/null | 0.25.0 | +| [database](#module\_database) | ../../ | n/a | +| [project\_database](#module\_project\_database) | ../../ | n/a | ## Outputs | Name | Description | |------|-------------| -| [prod\_database](#output\_prod\_database) | Prod database module outputs | +| [database](#output\_database) | Database module outputs | +| [project\_database](#output\_project\_database) | Database module outputs | ## Providers | Name | Version | |------|---------| -| [snowflake](#provider\_snowflake) | ~> 0.90 | +| [snowflake](#provider\_snowflake) | >= 0.95 | ## Requirements | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 1.3 | -| [snowflake](#requirement\_snowflake) | ~> 0.90 | +| [terraform](#requirement\_terraform) | >= 1.5 | +| [context](#requirement\_context) | >=0.4.0 | +| [snowflake](#requirement\_snowflake) | >= 0.95 | ## Resources diff --git a/examples/complete/context.tf b/examples/complete/context.tf deleted file mode 100644 index 5e0ef88..0000000 --- a/examples/complete/context.tf +++ /dev/null @@ -1,279 +0,0 @@ -# -# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label -# All other instances of this file should be a copy of that one -# -# -# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf -# and then place it in your Terraform module to automatically get -# Cloud Posse's standard configuration inputs suitable for passing -# to Cloud Posse modules. -# -# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf -# -# Modules should access the whole context as `module.this.context` -# to get the input variables with nulls for defaults, -# for example `context = module.this.context`, -# and access individual variables as `module.this.`, -# with final values filled in. -# -# For example, when using defaults, `module.this.context.delimiter` -# will be null, and `module.this.delimiter` will be `-` (hyphen). -# - -module "this" { - source = "cloudposse/label/null" - version = "0.25.0" # requires Terraform >= 0.13.0 - - enabled = var.enabled - namespace = var.namespace - tenant = var.tenant - environment = var.environment - stage = var.stage - name = var.name - delimiter = var.delimiter - attributes = var.attributes - tags = var.tags - additional_tag_map = var.additional_tag_map - label_order = var.label_order - regex_replace_chars = var.regex_replace_chars - id_length_limit = var.id_length_limit - label_key_case = var.label_key_case - label_value_case = var.label_value_case - descriptor_formats = var.descriptor_formats - labels_as_tags = var.labels_as_tags - - context = var.context -} - -# Copy contents of cloudposse/terraform-null-label/variables.tf here - -variable "context" { - type = any - default = { - enabled = true - namespace = null - tenant = null - environment = null - stage = null - name = null - delimiter = null - attributes = [] - tags = {} - additional_tag_map = {} - regex_replace_chars = null - label_order = [] - id_length_limit = null - label_key_case = null - label_value_case = null - descriptor_formats = {} - # Note: we have to use [] instead of null for unset lists due to - # https://github.com/hashicorp/terraform/issues/28137 - # which was not fixed until Terraform 1.0.0, - # but we want the default to be all the labels in `label_order` - # and we want users to be able to prevent all tag generation - # by setting `labels_as_tags` to `[]`, so we need - # a different sentinel to indicate "default" - labels_as_tags = ["unset"] - } - description = <<-EOT - 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. - EOT - - validation { - condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) - error_message = "Allowed values: `lower`, `title`, `upper`." - } - - validation { - condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) - error_message = "Allowed values: `lower`, `title`, `upper`, `none`." - } -} - -variable "enabled" { - type = bool - default = null - description = "Set to false to prevent the module from creating any resources" -} - -variable "namespace" { - type = string - default = null - description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" -} - -variable "tenant" { - type = string - default = null - description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" -} - -variable "environment" { - type = string - default = null - description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" -} - -variable "stage" { - type = string - default = null - description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" -} - -variable "name" { - type = string - default = null - description = <<-EOT - 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. - EOT -} - -variable "delimiter" { - type = string - default = null - description = <<-EOT - Delimiter to be used between ID elements. - Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. - EOT -} - -variable "attributes" { - type = list(string) - default = [] - description = <<-EOT - 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. - EOT -} - -variable "labels_as_tags" { - type = set(string) - default = ["default"] - description = <<-EOT - 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. - EOT -} - -variable "tags" { - type = map(string) - default = {} - description = <<-EOT - Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). - Neither the tag keys nor the tag values will be modified by this module. - EOT -} - -variable "additional_tag_map" { - type = map(string) - default = {} - description = <<-EOT - 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. - EOT -} - -variable "label_order" { - type = list(string) - default = null - description = <<-EOT - 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. - EOT -} - -variable "regex_replace_chars" { - type = string - default = null - description = <<-EOT - 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. - EOT -} - -variable "id_length_limit" { - type = number - default = null - description = <<-EOT - 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`. - EOT - validation { - condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 - error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." - } -} - -variable "label_key_case" { - type = string - default = null - description = <<-EOT - 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`. - EOT - - validation { - condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) - error_message = "Allowed values: `lower`, `title`, `upper`." - } -} - -variable "label_value_case" { - type = string - default = null - description = <<-EOT - 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`. - EOT - - validation { - condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) - error_message = "Allowed values: `lower`, `title`, `upper`, `none`." - } -} - -variable "descriptor_formats" { - type = any - default = {} - description = <<-EOT - 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). - EOT -} - -#### End of copy of cloudposse/terraform-null-label/variables.tf diff --git a/examples/complete/fixtures.tfvars b/examples/complete/fixtures.tfvars index 4584d42..a008029 100644 --- a/examples/complete/fixtures.tfvars +++ b/examples/complete/fixtures.tfvars @@ -1,22 +1,7 @@ -descriptor_formats = { - snowflake-database-role = { - labels = ["attributes", "name"] - format = "%v_%v" - } - snowflake-database = { - labels = ["environment", "name", "attributes"] - format = "%v_%v_%v" - } - snowflake-schema = { - labels = ["name", "attributes"] - format = "%v_%v" - } - snowflake-stage = { - labels = ["name", "attributes"] - format = "%v_%v" - } -} - -tags = { - Terraform = "True" +context_templates = { + snowflake-database = "{{.environment}}_{{.name}}" + snowflake-project-database = "{{.environment}}_{{.project}}_{{.name}}" + snowflake-database-database-role = "{{.name}}" + snowflake-schema = "{{.name}}" + snowflake-schema-database-role = "{{.schema}}_{{.name}}" } diff --git a/examples/complete/main.tf b/examples/complete/main.tf index 42d81d0..931f380 100644 --- a/examples/complete/main.tf +++ b/examples/complete/main.tf @@ -6,27 +6,23 @@ resource "snowflake_account_role" "dev_role" { name = "developer" } -module "prod_database" { - source = "../../" - context = module.this.context - environment = "prod" +module "database" { + source = "../../" - name = "analytics" - comment = "my database" + context_templates = var.context_templates + + name = "example_db" + comment = "test database" data_retention_time_in_days = 1 is_transient = false - create_default_roles = true - + create_default_roles = true database_ownership_grant = snowflake_account_role.admin_role.name roles = { readonly = { granted_to_roles = [snowflake_account_role.dev_role.name] } - transformer = { - enabled = false - } admin = { granted_to_roles = [snowflake_account_role.admin_role.name] } @@ -48,9 +44,6 @@ module "prod_database" { readonly = { granted_to_roles = [snowflake_account_role.dev_role.name] } - transformer = { - enabled = false - } } } silver = { @@ -58,9 +51,6 @@ module "prod_database" { admin = { granted_to_roles = [snowflake_account_role.admin_role.name] } - transformer = { - enabled = false - } } } gold = { @@ -68,10 +58,21 @@ module "prod_database" { admin = { granted_to_roles = [snowflake_account_role.admin_role.name] } - transformer = { - enabled = false - } } } } } + +module "project_database" { + source = "../../" + + context_templates = var.context_templates + + name = "analytics" + name_scheme = { + context_template_name = "snowflake-project-database" + extra_values = { + project = "project" + } + } +} diff --git a/examples/complete/outputs.tf b/examples/complete/outputs.tf index 1252330..ab83384 100644 --- a/examples/complete/outputs.tf +++ b/examples/complete/outputs.tf @@ -1,4 +1,9 @@ -output "prod_database" { - description = "Prod database module outputs" - value = module.prod_database +output "database" { + description = "Database module outputs" + value = module.database +} + +output "project_database" { + description = "Database module outputs" + value = module.project_database } diff --git a/examples/complete/providers.tf b/examples/complete/providers.tf index d343f0d..5b943b9 100644 --- a/examples/complete/providers.tf +++ b/examples/complete/providers.tf @@ -1 +1,12 @@ provider "snowflake" {} + +provider "context" { + properties = { + "environment" = {} + "name" = { required = true } + } + + values = { + environment = "dev" + } +} diff --git a/examples/complete/variables.tf b/examples/complete/variables.tf new file mode 100644 index 0000000..f09a47d --- /dev/null +++ b/examples/complete/variables.tf @@ -0,0 +1,4 @@ +variable "context_templates" { + description = "A map of context templates used to generate names" + type = map(string) +} diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf index 33afd12..0c16dd3 100644 --- a/examples/complete/versions.tf +++ b/examples/complete/versions.tf @@ -1,9 +1,13 @@ terraform { - required_version = ">= 1.3" + required_version = ">= 1.5" required_providers { snowflake = { source = "Snowflake-Labs/snowflake" - version = "~> 0.90" + version = ">= 0.95" + } + context = { + source = "cloudposse/context" + version = ">=0.4.0" } } } diff --git a/examples/simple/README.md b/examples/simple/README.md index 8b9ebc5..96df73e 100644 --- a/examples/simple/README.md +++ b/examples/simple/README.md @@ -18,33 +18,13 @@ 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 | +No inputs. ## Modules | Name | Source | Version | |------|--------|---------| | [database](#module\_database) | ../../ | n/a | -| [this](#module\_this) | cloudposse/label/null | 0.25.0 | ## Outputs @@ -61,7 +41,7 @@ No providers. | Name | Version | |------|---------| | [terraform](#requirement\_terraform) | >= 1.3 | -| [snowflake](#requirement\_snowflake) | ~> 0.90 | +| [snowflake](#requirement\_snowflake) | ~> 0.95 | ## Resources diff --git a/examples/simple/context.tf b/examples/simple/context.tf deleted file mode 100644 index 5e0ef88..0000000 --- a/examples/simple/context.tf +++ /dev/null @@ -1,279 +0,0 @@ -# -# ONLY EDIT THIS FILE IN github.com/cloudposse/terraform-null-label -# All other instances of this file should be a copy of that one -# -# -# Copy this file from https://github.com/cloudposse/terraform-null-label/blob/master/exports/context.tf -# and then place it in your Terraform module to automatically get -# Cloud Posse's standard configuration inputs suitable for passing -# to Cloud Posse modules. -# -# curl -sL https://raw.githubusercontent.com/cloudposse/terraform-null-label/master/exports/context.tf -o context.tf -# -# Modules should access the whole context as `module.this.context` -# to get the input variables with nulls for defaults, -# for example `context = module.this.context`, -# and access individual variables as `module.this.`, -# with final values filled in. -# -# For example, when using defaults, `module.this.context.delimiter` -# will be null, and `module.this.delimiter` will be `-` (hyphen). -# - -module "this" { - source = "cloudposse/label/null" - version = "0.25.0" # requires Terraform >= 0.13.0 - - enabled = var.enabled - namespace = var.namespace - tenant = var.tenant - environment = var.environment - stage = var.stage - name = var.name - delimiter = var.delimiter - attributes = var.attributes - tags = var.tags - additional_tag_map = var.additional_tag_map - label_order = var.label_order - regex_replace_chars = var.regex_replace_chars - id_length_limit = var.id_length_limit - label_key_case = var.label_key_case - label_value_case = var.label_value_case - descriptor_formats = var.descriptor_formats - labels_as_tags = var.labels_as_tags - - context = var.context -} - -# Copy contents of cloudposse/terraform-null-label/variables.tf here - -variable "context" { - type = any - default = { - enabled = true - namespace = null - tenant = null - environment = null - stage = null - name = null - delimiter = null - attributes = [] - tags = {} - additional_tag_map = {} - regex_replace_chars = null - label_order = [] - id_length_limit = null - label_key_case = null - label_value_case = null - descriptor_formats = {} - # Note: we have to use [] instead of null for unset lists due to - # https://github.com/hashicorp/terraform/issues/28137 - # which was not fixed until Terraform 1.0.0, - # but we want the default to be all the labels in `label_order` - # and we want users to be able to prevent all tag generation - # by setting `labels_as_tags` to `[]`, so we need - # a different sentinel to indicate "default" - labels_as_tags = ["unset"] - } - description = <<-EOT - 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. - EOT - - validation { - condition = lookup(var.context, "label_key_case", null) == null ? true : contains(["lower", "title", "upper"], var.context["label_key_case"]) - error_message = "Allowed values: `lower`, `title`, `upper`." - } - - validation { - condition = lookup(var.context, "label_value_case", null) == null ? true : contains(["lower", "title", "upper", "none"], var.context["label_value_case"]) - error_message = "Allowed values: `lower`, `title`, `upper`, `none`." - } -} - -variable "enabled" { - type = bool - default = null - description = "Set to false to prevent the module from creating any resources" -} - -variable "namespace" { - type = string - default = null - description = "ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique" -} - -variable "tenant" { - type = string - default = null - description = "ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for" -} - -variable "environment" { - type = string - default = null - description = "ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT'" -} - -variable "stage" { - type = string - default = null - description = "ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release'" -} - -variable "name" { - type = string - default = null - description = <<-EOT - 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. - EOT -} - -variable "delimiter" { - type = string - default = null - description = <<-EOT - Delimiter to be used between ID elements. - Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. - EOT -} - -variable "attributes" { - type = list(string) - default = [] - description = <<-EOT - 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. - EOT -} - -variable "labels_as_tags" { - type = set(string) - default = ["default"] - description = <<-EOT - 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. - EOT -} - -variable "tags" { - type = map(string) - default = {} - description = <<-EOT - Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`). - Neither the tag keys nor the tag values will be modified by this module. - EOT -} - -variable "additional_tag_map" { - type = map(string) - default = {} - description = <<-EOT - 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. - EOT -} - -variable "label_order" { - type = list(string) - default = null - description = <<-EOT - 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. - EOT -} - -variable "regex_replace_chars" { - type = string - default = null - description = <<-EOT - 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. - EOT -} - -variable "id_length_limit" { - type = number - default = null - description = <<-EOT - 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`. - EOT - validation { - condition = var.id_length_limit == null ? true : var.id_length_limit >= 6 || var.id_length_limit == 0 - error_message = "The id_length_limit must be >= 6 if supplied (not null), or 0 for unlimited length." - } -} - -variable "label_key_case" { - type = string - default = null - description = <<-EOT - 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`. - EOT - - validation { - condition = var.label_key_case == null ? true : contains(["lower", "title", "upper"], var.label_key_case) - error_message = "Allowed values: `lower`, `title`, `upper`." - } -} - -variable "label_value_case" { - type = string - default = null - description = <<-EOT - 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`. - EOT - - validation { - condition = var.label_value_case == null ? true : contains(["lower", "title", "upper", "none"], var.label_value_case) - error_message = "Allowed values: `lower`, `title`, `upper`, `none`." - } -} - -variable "descriptor_formats" { - type = any - default = {} - description = <<-EOT - 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). - EOT -} - -#### End of copy of cloudposse/terraform-null-label/variables.tf diff --git a/examples/simple/versions.tf b/examples/simple/versions.tf index 33afd12..0929444 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.90" + version = "~> 0.95" } } } diff --git a/locals.tf b/locals.tf index 9d6b7bb..435b4b6 100644 --- a/locals.tf +++ b/locals.tf @@ -1,16 +1,16 @@ locals { - # Get a name from the descriptor. If not available, use default naming convention. - # Trim and replace function are used to avoid bare delimiters on both ends of the name and situation of adjacent delimiters. - name_from_descriptor = module.database_label.enabled ? trim(replace( - 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 + context_template = lookup(var.context_templates, var.name_scheme.context_template_name, null) - create_default_roles = module.this.enabled && var.create_default_roles + default_role_naming_scheme = { + properties = ["name"] + context_template_name = "snowflake-database-database-role" + extra_values = { + database = var.name + } + } #This needs to be the same as an object in roles variable role_template = { - enabled = true - descriptor_name = "snowflake-database-role" comment = null role_ownership_grant = "SYSADMIN" granted_roles = [] @@ -81,7 +81,7 @@ locals { default_roles = { for role_name, role in local.roles_definition : role_name => role - if contains(keys(local.default_roles_definition), role_name) + if contains(keys(local.default_roles_definition), role_name) && var.create_default_roles } custom_roles = { @@ -96,8 +96,6 @@ locals { ) : role_name => role if role_name != null } - - schemas = var.schemas } module "roles_deep_merge" { diff --git a/main.tf b/main.tf index de92b1a..c8d8c43 100644 --- a/main.tf +++ b/main.tf @@ -1,17 +1,18 @@ -module "database_label" { - source = "cloudposse/label/null" - version = "0.25.0" - context = module.this.context - - delimiter = coalesce(module.this.context.delimiter, "_") - regex_replace_chars = coalesce(module.this.context.regex_replace_chars, "/[^_a-zA-Z0-9]/") - label_value_case = coalesce(module.this.context.label_value_case, "upper") +data "context_label" "this" { + delimiter = local.context_template == null ? var.name_scheme.delimiter : null + properties = local.context_template == null ? var.name_scheme.properties : null + template = local.context_template + + replace_chars_regex = var.name_scheme.replace_chars_regex + + values = merge( + var.name_scheme.extra_values, + { name = var.name } + ) } resource "snowflake_database" "this" { - count = module.this.enabled ? 1 : 0 - - name = local.name_from_descriptor + name = data.context_label.this.rendered is_transient = var.is_transient comment = var.comment @@ -31,20 +32,28 @@ resource "snowflake_database" "this" { 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 + drop_public_schema_on_creation = var.drop_public_schema_on_creation +} +moved { + from = snowflake_database.this[0] + to = snowflake_database.this } module "snowflake_default_role" { for_each = local.default_roles source = "getindata/database-role/snowflake" - version = "1.1.1" - context = module.this.context + version = "2.0.1" + + database_name = snowflake_database.this.name + context_templates = var.context_templates - 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-database-role") + name = each.key + name_scheme = merge( + local.default_role_naming_scheme, + lookup(each.value, "name_scheme", {}) + ) + comment = lookup(each.value, "comment", null) granted_to_roles = lookup(each.value, "granted_to_roles", []) granted_to_database_roles = lookup(each.value, "granted_to_database_roles", []) @@ -52,24 +61,23 @@ module "snowflake_default_role" { 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/database-role/snowflake" - version = "1.1.1" - context = module.this.context + version = "2.0.1" + + database_name = snowflake_database.this.name + context_templates = var.context_templates - 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-database-role") + name = each.key + name_scheme = merge( + local.default_role_naming_scheme, + lookup(each.value, "name_scheme", {}) + ) + comment = lookup(each.value, "comment", null) granted_to_roles = lookup(each.value, "granted_to_roles", []) granted_to_database_roles = lookup(each.value, "granted_to_database_roles", []) @@ -77,27 +85,28 @@ module "snowflake_custom_role" { 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 + for_each = var.schemas source = "getindata/schema/snowflake" - version = "2.0.2" + version = "3.0.0" - context = module.this.context - enabled = module.this.enabled && each.value.enabled - descriptor_name = each.value.descriptor_name + context_templates = var.context_templates + + name = each.key + name_scheme = merge({ + extra_values = { + database = var.name + } }, + lookup(each.value, "name_scheme", {}) + ) - 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) + database = snowflake_database.this.name skip_schema_creation = each.value.skip_schema_creation data_retention_time_in_days = each.value.data_retention_time_in_days @@ -122,11 +131,6 @@ module "snowflake_schema" { roles = each.value.roles create_default_roles = coalesce(each.value.create_default_roles, var.create_default_roles) - - depends_on = [ - module.snowflake_default_role, - module.snowflake_custom_role - ] } resource "snowflake_grant_ownership" "database_ownership" { @@ -136,10 +140,13 @@ resource "snowflake_grant_ownership" "database_ownership" { outbound_privileges = "REVOKE" on { object_type = "DATABASE" - object_name = one(snowflake_database.this[*].name) + object_name = snowflake_database.this.name } + # In order to create all resources before transferring ownership depends_on = [ - module.snowflake_schema + module.snowflake_default_role, + module.snowflake_custom_role, + module.snowflake_schema, ] } diff --git a/outputs.tf b/outputs.tf index b61f1be..705621f 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,91 +1,91 @@ output "name" { description = "Name of the database" - value = one(snowflake_database.this[*].name) + value = snowflake_database.this.name } output "data_retention_time_in_days" { description = "Data retention days for the database" - value = one(snowflake_database.this[*].data_retention_time_in_days) + value = 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) + value = snowflake_database.this.max_data_extension_time_in_days } output "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" - value = one(snowflake_database.this[*].is_transient) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = 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) + value = snowflake_database.this.enable_console_output } output "database_roles" { diff --git a/variables.tf b/variables.tf index f312793..4712cdd 100644 --- a/variables.tf +++ b/variables.tf @@ -1,7 +1,6 @@ -variable "descriptor_name" { - description = "Name of the descriptor used to form a resource name" +variable "name" { + description = "Name of the resource" type = string - default = "snowflake-database" } variable "comment" { @@ -121,8 +120,13 @@ variable "create_default_roles" { variable "roles" { description = "Roles created in the database scope" type = map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-database-role") + name_scheme = optional(object({ + properties = optional(list(string)) + delimiter = optional(string) + context_template_name = optional(string) + replace_chars_regex = optional(string) + extra_labels = optional(map(string)) + })) comment = optional(string) role_ownership_grant = optional(string) granted_roles = optional(list(string)) @@ -157,9 +161,14 @@ variable "roles" { variable "schemas" { description = "Schemas to be created in the database" type = map(object({ - enabled = optional(bool, true) + name_scheme = optional(object({ + properties = optional(list(string)) + delimiter = optional(string) + context_template_name = optional(string) + replace_chars_regex = optional(string) + extra_labels = optional(map(string)) + })) 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) @@ -182,8 +191,13 @@ variable "schemas" { 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") + name_scheme = optional(object({ + properties = optional(list(string)) + delimiter = optional(string) + context_template_name = optional(string) + replace_chars_regex = optional(string) + extra_labels = optional(map(string)) + })) aws_external_id = optional(string) comment = optional(string) copy_options = optional(string) @@ -196,8 +210,13 @@ variable "schemas" { url = optional(string) create_default_roles = optional(bool) roles = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-database-role") + name_scheme = optional(object({ + properties = optional(list(string)) + delimiter = optional(string) + context_template_name = optional(string) + replace_chars_regex = optional(string) + extra_labels = optional(map(string)) + })) with_grant_option = optional(bool) granted_to_roles = optional(list(string)) granted_to_database_roles = optional(list(string)) @@ -207,8 +226,13 @@ variable "schemas" { })), {}) })), {}) roles = optional(map(object({ - enabled = optional(bool, true) - descriptor_name = optional(string, "snowflake-database-role") + name_scheme = optional(object({ + properties = optional(list(string)) + delimiter = optional(string) + context_template_name = optional(string) + replace_chars_regex = optional(string) + extra_labels = optional(map(string)) + })) comment = optional(string) granted_to_roles = optional(list(string)) granted_to_database_roles = optional(list(string)) @@ -236,3 +260,34 @@ variable "database_ownership_grant" { type = string default = null } + +variable "drop_public_schema_on_creation" { + description = "Whether the `PUBLIC` schema should be dropped after the database creation" + type = bool + default = true +} + +variable "name_scheme" { + description = <