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

Create statements dinamically using list methods #48

Closed
wants to merge 1 commit into from

Conversation

lucasp0r
Copy link

@lucasp0r lucasp0r commented Aug 17, 2022

Changelog

  • Simplify thousands of grants to run.
  • Example Terraform execution

Note: @aidanmelen The idea is to keep in a list of execution if you have a large environment in my case, I have had 1k grants to run. So that's why I'm using this method. It's working really well :). Thx for your support and help in this provider creation.

@aidanmelen
Copy link
Owner

Thank you for you input. I think it is really cool what you have accomplished with the for loop here! However, I am going to close this in favor of the new update statements feature for in-place updates. Please see this discussion for more information.

Hopefully this new change with simplify your snowsql grant statements in terraform! I was thinking you could experiment with something like this:

locals {
  create_or_update_statements = join(
    "",
    flatten(
      [
        for id,db in snowflake_table_grant.update_db_future : formatlist("GRANT UPDATE, INSERT, DELETE, TRUNCATE ON ALL TABLES IN DATABASE ${db.database_name} TO ROLE %s;", db.roles)
      ]
    )
  )
  update_grant_all_schema = join(
    "",
    flatten(
      [
        for id,db in snowflake_table_grant.update_schema_future : formatlist("GRANT UPDATE, INSERT, DELETE, TRUNCATE ON ALL TABLES IN SCHEMA ${db.database_name}.${db.schema_name} TO ROLE %s;", db.roles)
      ]
    )
  )
}

resource "snowsql_exec" "my_grants" {
  name = "my_grants"

  create {
    statements = local.create_or_update_statements
  }

  update {
    statements = local.create_or_update_statements
  }

  delete {
    statements  = "REVOKE SELECT PRIVILEGES ON ALL MATERIALIZED VIEWS IN DATABASE TEST TO ROLE TEST;"
  }

  lifecycle {
    ignore_changes = [
      create,
    ]
  }
}

@aidanmelen aidanmelen closed this Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants