Skip to content

Commit

Permalink
Merge pull request #6 from getindata/feat/add-view-grant
Browse files Browse the repository at this point in the history
feat: Add view grants
  • Loading branch information
Santhin authored Jul 12, 2023
2 parents 8fd6be2 + b760d4b commit ef664d7
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

env:
TERRAFORM_DOCS_VERSION: v0.16.0
TFLINT_VERSION: v0.43.0

jobs:
collectInputs:
Expand Down Expand Up @@ -43,15 +44,15 @@ jobs:
- name: Pre-commit Terraform ${{ steps.minMax.outputs.minVersion }}
# Run only validate pre-commit check on min version supported
if: ${{ matrix.directory != '.' }}
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.3.0
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.6.0
with:
terraform-version: ${{ steps.minMax.outputs.minVersion }}
args: 'terraform-validate --color=always --show-diff-on-failure --files ${{ matrix.directory }}/*'

- name: Pre-commit Terraform ${{ steps.minMax.outputs.minVersion }}
# Run only validate pre-commit check on min version supported
if: ${{ matrix.directory == '.' }}
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.3.0
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.6.0
with:
terraform-version: ${{ steps.minMax.outputs.minVersion }}
args: 'terraform-validate --color=always --show-diff-on-failure --files $(ls *.tf)'
Expand All @@ -75,7 +76,8 @@ jobs:
- run: terraform init

- name: Pre-commit Terraform ${{ steps.minMax.outputs.maxVersion }}
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.3.0
uses: clowdhaus/terraform-composite-actions/pre-commit@v1.6.0
with:
terraform-version: ${{ steps.minMax.outputs.maxVersion }}
terraform-docs-version: ${{ env.TERRAFORM_DOCS_VERSION }}
tflint-version: ${{ env.TFLINT_VERSION }}
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ module "snowflake_role" {
| <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_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, false)<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> privileges = list(string)<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 @@ -92,9 +92,10 @@ module "snowflake_role" {
| <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 = string<br> privileges = list(string)<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, false)<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> privileges = list(string)<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> privileges = list(string)<br> }))</pre> | `[]` | no |

## Modules

Expand Down Expand Up @@ -135,6 +136,7 @@ module "snowflake_role" {
| [snowflake_role_ownership_grant.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/role_ownership_grant) | resource |
| [snowflake_schema_grant.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/schema_grant) | resource |
| [snowflake_table_grant.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/table_grant) | resource |
| [snowflake_view_grant.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/view_grant) | resource |
<!-- END_TF_DOCS -->

## CONTRIBUTING
Expand Down
10 changes: 10 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ locals {
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, "on_future")}/${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
privilege = privilege
}
}]...)
}
11 changes: 11 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,17 @@ resource "snowflake_external_table_grant" "this" {
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
roles = [one(snowflake_role.this[*].name)]
}

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

Expand Down
16 changes: 14 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ variable "table_grants" {
database_name = string
schema_name = string
table_name = optional(string)
on_future = optional(bool, false)
on_future = optional(bool)
privileges = list(string)
}))
default = []
Expand All @@ -71,12 +71,24 @@ variable "external_table_grants" {
database_name = string
schema_name = string
external_table_name = optional(string)
on_future = optional(bool, false)
on_future = optional(bool)
privileges = list(string)
}))
default = []
}

variable "view_grants" {
description = "Grants on a view level"
type = list(object({
database_name = string
schema_name = string
view_name = optional(string)
on_future = optional(bool)
privileges = list(string)
}))
default = []
}

variable "descriptor_name" {
description = "Name of the descriptor used to form a resource name"
type = string
Expand Down

0 comments on commit ef664d7

Please sign in to comment.