aidanmelen / terraform-provider-snowsql

Terraform SnowSQL provider
https://registry.terraform.io/providers/aidanmelen/snowsql/latest
Other
21 stars 10 forks source link

V1.2.0 read lifecycle #68

Closed aidanmelen closed 1 year ago

aidanmelen commented 1 year ago

The snowsql_exec resource allows you to execute arbitrary Snowflake SQL queries from Terraform, and use the results in your infrastructure management. When using the read statements in the resource, the result(s) of the SQL query/queries will be available in the read_results attribute as a sensitive raw JSON string.

To output the query results in a non-sensitive format to the console, you can use the nonsensitive function to mark the read_results value as non-sensitive before decoding it with the jsondecode function.

resource "snowsql_exec" "role" {
  name = "my_role"

  create {
    statements = "CREATE ROLE IF NOT EXISTS my_role;"
  }

  read {
    statements = "SHOW ROLES LIKE 'my_role';"
  }

  delete {
    statements = "DROP ROLE IF EXISTS my_role;"
  }
}

output "read_results" {
  description = "The SnowSQL query result from the read statements."
  value       = jsondecode(nonsensitive(snowsql_exec.role.read_results))
}

This will output the JSON formatted results like this:

Outputs:

read_results = [
  {
    "assigned_to_users" = "0"
    "comment" = ""
    "created_on" = "2023-02-16T18:47:48.756-08:00"
    "granted_roles" = "0"
    "granted_to_roles" = "0"
    "is_current" = "N"
    "is_default" = "N"
    "is_inherited" = "N"
    "name" = "BASIC_CIVIL_HALIBUT"
    "owner" = "ACCOUNTADMIN"
  },
]