Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add possibility to set enable_multiple_grants #12

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,15 @@ module "snowflake_role" {
| <a name="input_attributes"></a> [attributes](#input\_attributes) | ID element. Additional attributes (e.g. `workers` or `cluster`) to add to `id`,<br>in the order they appear in the list. New attributes are appended to the<br>end of the list. The elements of the list are joined by the `delimiter`<br>and treated as a single ID element. | `list(string)` | `[]` | no |
| <a name="input_comment"></a> [comment](#input\_comment) | Role description | `string` | `null` | no |
| <a name="input_context"></a> [context](#input\_context) | Single object for setting entire context at once.<br>See description of individual variables for details.<br>Leave string and numeric variables as `null` to use default value.<br>Individual variable settings (non-null) override settings in context object,<br>except for attributes, tags, and additional\_tag\_map, which are merged. | `any` | <pre>{<br> "additional_tag_map": {},<br> "attributes": [],<br> "delimiter": null,<br> "descriptor_formats": {},<br> "enabled": true,<br> "environment": null,<br> "id_length_limit": null,<br> "label_key_case": null,<br> "label_order": [],<br> "label_value_case": null,<br> "labels_as_tags": [<br> "unset"<br> ],<br> "name": null,<br> "namespace": null,<br> "regex_replace_chars": null,<br> "stage": null,<br> "tags": {},<br> "tenant": null<br>}</pre> | no |
| <a name="input_database_grants"></a> [database\_grants](#input\_database\_grants) | Grants on a database level | <pre>list(object({<br> database_name = string<br> privileges = list(string)<br> }))</pre> | `[]` | no |
| <a name="input_database_grants"></a> [database\_grants](#input\_database\_grants) | Grants on a database level | <pre>list(object({<br> database_name = string<br> privileges = list(string)<br> enable_multiple_grants = optional(bool)<br> }))</pre> | `[]` | no |
| <a name="input_delimiter"></a> [delimiter](#input\_delimiter) | Delimiter to be used between ID elements.<br>Defaults to `-` (hyphen). Set to `""` to use no delimiter at all. | `string` | `null` | no |
| <a name="input_descriptor_formats"></a> [descriptor\_formats](#input\_descriptor\_formats) | Describe additional descriptors to be output in the `descriptors` output map.<br>Map of maps. Keys are names of descriptors. Values are maps of the form<br>`{<br> format = string<br> labels = list(string)<br>}`<br>(Type is `any` so the map values can later be enhanced to provide additional options.)<br>`format` is a Terraform format string to be passed to the `format()` function.<br>`labels` is a list of labels, in order, to pass to `format()` function.<br>Label values will be normalized before being passed to `format()` so they will be<br>identical to how they appear in `id`.<br>Default is `{}` (`descriptors` output will be empty). | `any` | `{}` | no |
| <a name="input_descriptor_name"></a> [descriptor\_name](#input\_descriptor\_name) | Name of the descriptor used to form a resource name | `string` | `"snowflake-role"` | no |
| <a name="input_dynamic_table_grants"></a> [dynamic\_table\_grants](#input\_dynamic\_table\_grants) | Grants on a dynamic\_table level | <pre>list(object({<br> database_name = string<br> schema_name = optional(string)<br> dynamic_table_name = optional(string)<br> on_future = optional(bool, false)<br> on_all = optional(bool, false)<br> all_privileges = optional(bool)<br> privileges = optional(list(string), null)<br> }))</pre> | `[]` | no |
| <a name="input_enable_multiple_grants"></a> [enable\_multiple\_grants](#input\_enable\_multiple\_grants) | When this is set to true, multiple grants of the same type can be created for all grants in the role. This will cause Terraform to not revoke grants applied to roles and objects outside Terraform | `bool` | `null` | no |
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
| <a name="input_environment"></a> [environment](#input\_environment) | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | `string` | `null` | no |
| <a name="input_external_table_grants"></a> [external\_table\_grants](#input\_external\_table\_grants) | Grants on a external table level | <pre>list(object({<br> database_name = string<br> schema_name = string<br> external_table_name = optional(string)<br> on_future = optional(bool)<br> on_all = optional(bool)<br> privileges = list(string)<br> }))</pre> | `[]` | no |
| <a name="input_external_table_grants"></a> [external\_table\_grants](#input\_external\_table\_grants) | Grants on a external table level | <pre>list(object({<br> database_name = string<br> schema_name = string<br> external_table_name = optional(string)<br> on_future = optional(bool)<br> on_all = optional(bool)<br> privileges = list(string)<br> enable_multiple_grants = optional(bool)<br> }))</pre> | `[]` | no |
| <a name="input_granted_roles"></a> [granted\_roles](#input\_granted\_roles) | Roles granted to this role | `list(string)` | `[]` | no |
| <a name="input_granted_to_roles"></a> [granted\_to\_roles](#input\_granted\_to\_roles) | Roles which this role is granted to | `list(string)` | `[]` | no |
| <a name="input_granted_to_users"></a> [granted\_to\_users](#input\_granted\_to\_users) | Users which this role is granted to | `list(string)` | `[]` | no |
Expand All @@ -91,12 +92,12 @@ module "snowflake_role" {
| <a name="input_namespace"></a> [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 |
| <a name="input_regex_replace_chars"></a> [regex\_replace\_chars](#input\_regex\_replace\_chars) | Terraform regular expression (regex) string.<br>Characters matching the regex will be removed from the ID elements.<br>If not set, `"/[^a-zA-Z0-9-]/"` is used to remove all characters other than hyphens, letters and digits. | `string` | `null` | no |
| <a name="input_role_ownership_grant"></a> [role\_ownership\_grant](#input\_role\_ownership\_grant) | The name of the role to grant ownership | `string` | `null` | no |
| <a name="input_schema_grants"></a> [schema\_grants](#input\_schema\_grants) | Grants on a schema level | <pre>list(object({<br> database_name = string<br> schema_name = optional(string)<br> privileges = list(string)<br> on_all = optional(bool)<br> on_future = optional(bool)<br> }))</pre> | `[]` | no |
| <a name="input_schema_grants"></a> [schema\_grants](#input\_schema\_grants) | Grants on a schema level | <pre>list(object({<br> database_name = string<br> schema_name = optional(string)<br> privileges = list(string)<br> on_all = optional(bool)<br> on_future = optional(bool)<br> enable_multiple_grants = optional(bool)<br> }))</pre> | `[]` | no |
| <a name="input_stage"></a> [stage](#input\_stage) | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | `string` | `null` | no |
| <a name="input_table_grants"></a> [table\_grants](#input\_table\_grants) | Grants on a table level | <pre>list(object({<br> database_name = string<br> schema_name = string<br> table_name = optional(string)<br> on_future = optional(bool)<br> on_all = optional(bool)<br> privileges = list(string)<br> }))</pre> | `[]` | no |
| <a name="input_table_grants"></a> [table\_grants](#input\_table\_grants) | Grants on a table level | <pre>list(object({<br> database_name = string<br> schema_name = string<br> table_name = optional(string)<br> on_future = optional(bool)<br> on_all = optional(bool)<br> privileges = list(string)<br> enable_multiple_grants = optional(bool)<br> }))</pre> | `[]` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (e.g. `{'BusinessUnit': 'XYZ'}`).<br>Neither the tag keys nor the tag values will be modified by this module. | `map(string)` | `{}` | no |
| <a name="input_tenant"></a> [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 |
| <a name="input_view_grants"></a> [view\_grants](#input\_view\_grants) | Grants on a view level | <pre>list(object({<br> database_name = string<br> schema_name = string<br> view_name = optional(string)<br> on_future = optional(bool)<br> on_all = optional(bool)<br> privileges = list(string)<br> }))</pre> | `[]` | no |
| <a name="input_view_grants"></a> [view\_grants](#input\_view\_grants) | Grants on a view level | <pre>list(object({<br> database_name = string<br> schema_name = string<br> view_name = optional(string)<br> on_future = optional(bool)<br> on_all = optional(bool)<br> privileges = list(string)<br> enable_multiple_grants = optional(bool)<br> }))</pre> | `[]` | no |

## Modules

Expand Down
5 changes: 3 additions & 2 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ module "snowflake_role" {

database_grants = [
{
database_name = "LOGS_DB"
privileges = ["USAGE"]
database_name = "LOGS_DB"
privileges = ["USAGE"]
enable_multiple_grants = true
}
]

Expand Down
55 changes: 30 additions & 25 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,51 +11,56 @@ locals {

database_grants = merge([for database_grant in var.database_grants : {
for privilege in database_grant.privileges : "${database_grant.database_name}/${privilege}" => {
database_name = database_grant.database_name
privilege = privilege
database_name = database_grant.database_name
enable_multiple_grants = database_grant.enable_multiple_grants == null ? var.enable_multiple_grants : database_grant.enable_multiple_grants
privilege = privilege
}
}]...)

schema_grants = merge([for schema_grant in var.schema_grants : {
for privilege in schema_grant.privileges : "${schema_grant.database_name}/${coalesce(schema_grant.schema_name, schema_grant.on_future != null ? "on_future" : "on_all")}/${privilege}" => {
database_name = schema_grant.database_name
schema_name = schema_grant.schema_name
on_future = schema_grant.on_future
on_all = schema_grant.on_all
privilege = privilege
database_name = schema_grant.database_name
schema_name = schema_grant.schema_name
on_future = schema_grant.on_future
on_all = schema_grant.on_all
enable_multiple_grants = schema_grant.enable_multiple_grants == null ? var.enable_multiple_grants : schema_grant.enable_multiple_grants
privilege = privilege
}
}]...)

table_grants = merge([for table_grant in var.table_grants : {
for privilege in table_grant.privileges : "${table_grant.database_name}/${table_grant.schema_name}/${coalesce(table_grant.table_name, table_grant.on_future != null ? "on_future" : "on_all")}/${privilege}" => {
database_name = table_grant.database_name
schema_name = table_grant.schema_name
table_name = table_grant.table_name
on_future = table_grant.on_future
on_all = table_grant.on_all
privilege = privilege
database_name = table_grant.database_name
schema_name = table_grant.schema_name
table_name = table_grant.table_name
on_future = table_grant.on_future
on_all = table_grant.on_all
enable_multiple_grants = table_grant.enable_multiple_grants == null ? var.enable_multiple_grants : table_grant.enable_multiple_grants
privilege = privilege
}
}]...)

external_table_grants = merge([for table_grant in var.external_table_grants : {
for privilege in table_grant.privileges : "${table_grant.database_name}/${table_grant.schema_name}/${coalesce(table_grant.external_table_name, table_grant.on_future != null ? "on_future" : "on_all")}/${privilege}" => {
database_name = table_grant.database_name
schema_name = table_grant.schema_name
external_table_name = table_grant.external_table_name
on_future = table_grant.on_future
on_all = table_grant.on_all
privilege = privilege
database_name = table_grant.database_name
schema_name = table_grant.schema_name
external_table_name = table_grant.external_table_name
on_future = table_grant.on_future
on_all = table_grant.on_all
enable_multiple_grants = table_grant.enable_multiple_grants == null ? var.enable_multiple_grants : table_grant.enable_multiple_grants
privilege = privilege
}
}]...)

view_grants = merge([for view_grant in var.view_grants : {
for privilege in view_grant.privileges : "${view_grant.database_name}/${view_grant.schema_name}/${coalesce(view_grant.view_name, view_grant.on_future != null ? "on_future" : "on_all")}/${privilege}" => {
database_name = view_grant.database_name
schema_name = view_grant.schema_name
view_name = view_grant.view_name
on_future = view_grant.on_future
on_all = view_grant.on_all
privilege = privilege
database_name = view_grant.database_name
schema_name = view_grant.schema_name
view_name = view_grant.view_name
on_future = view_grant.on_future
on_all = view_grant.on_all
enable_multiple_grants = view_grant.enable_multiple_grants == null ? var.enable_multiple_grants : view_grant.enable_multiple_grants
privilege = privilege
}
}]...)

Expand Down
82 changes: 45 additions & 37 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,78 +25,86 @@ resource "snowflake_role_ownership_grant" "this" {
resource "snowflake_role_grants" "granted_roles" {
for_each = toset(module.this.enabled ? local.granted_roles : [])

role_name = each.value
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = var.enable_multiple_grants
role_name = each.value
roles = [one(snowflake_role.this[*].name)]
}

resource "snowflake_role_grants" "granted_to" {
count = module.this.enabled && (length(local.granted_to_roles) > 0 || length(local.granted_to_users) > 0) ? 1 : 0

role_name = one(snowflake_role.this[*].name)
roles = local.granted_to_roles
users = local.granted_to_users
enable_multiple_grants = var.enable_multiple_grants
role_name = one(snowflake_role.this[*].name)
roles = local.granted_to_roles
users = local.granted_to_users
}

resource "snowflake_database_grant" "this" {
for_each = module.this.enabled ? local.database_grants : {}

database_name = each.value.database_name
privilege = each.value.privilege
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = each.value.enable_multiple_grants
database_name = each.value.database_name
privilege = each.value.privilege
roles = [one(snowflake_role.this[*].name)]
}

resource "snowflake_schema_grant" "this" {
for_each = module.this.enabled ? local.schema_grants : {}

database_name = each.value.database_name
schema_name = each.value.schema_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = each.value.enable_multiple_grants
database_name = each.value.database_name
schema_name = each.value.schema_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
}

resource "snowflake_table_grant" "this" {
for_each = module.this.enabled ? local.table_grants : {}

database_name = each.value.database_name
schema_name = each.value.schema_name
table_name = each.value.table_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = each.value.enable_multiple_grants
database_name = each.value.database_name
schema_name = each.value.schema_name
table_name = each.value.table_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
}

resource "snowflake_external_table_grant" "this" {
for_each = module.this.enabled ? local.external_table_grants : {}

database_name = each.value.database_name
schema_name = each.value.schema_name
external_table_name = each.value.external_table_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = each.value.enable_multiple_grants
database_name = each.value.database_name
schema_name = each.value.schema_name
external_table_name = each.value.external_table_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
}

resource "snowflake_view_grant" "this" {
for_each = module.this.enabled ? local.view_grants : {}

database_name = each.value.database_name
schema_name = each.value.schema_name
view_name = each.value.view_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = each.value.enable_multiple_grants
database_name = each.value.database_name
schema_name = each.value.schema_name
view_name = each.value.view_name
privilege = each.value.privilege
on_future = each.value.on_future
on_all = each.value.on_all
roles = [one(snowflake_role.this[*].name)]
}

resource "snowflake_account_grant" "this" {
for_each = toset(module.this.enabled ? var.account_grants : [])

privilege = each.value
roles = [one(snowflake_role.this[*].name)]
enable_multiple_grants = var.enable_multiple_grants
privilege = each.value
roles = [one(snowflake_role.this[*].name)]

with_grant_option = false
}
Expand Down
Loading
Loading