Skip to content

Commit c8e3444

Browse files
authored
Merge pull request #4 from getindata/feature/initial_commit
feat: Initial commit of Snowflake Shared Database module
2 parents dc177e6 + 5363024 commit c8e3444

24 files changed

+622
-151
lines changed

README.md

Lines changed: 59 additions & 40 deletions
Large diffs are not rendered by default.

examples/complete/.env.dist

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
SNOWFLAKE_USER=
2+
SNOWFLAKE_ACCOUNT=
3+
SNOWFLAKE_ROLE="ACCOUNTADMIN"
4+
5+
# Login using RSA key
6+
# SNOWFLAKE_PRIVATE_KEY_PATH=
7+
# SNOWFLAKE_AUTHENTICATOR="JWT"
8+
9+
# Login using password
10+
# SNOWFLAKE_PASSWORD=

examples/complete/README.md

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,71 @@
11
# Complete Example
22

33
```terraform
4-
module "terraform_module_template" {
5-
source = "../../"
6-
context = module.this.context
7-
8-
example_var = "This is a example value."
9-
sub_resource = {
10-
example_var = "This is a example value of sub resource."
4+
module "snowflake_shared_database" {
5+
source = "../.."
6+
7+
name = "SHARED_DATABASE"
8+
context = module.this.context
9+
from_share = var.from_share
10+
11+
descriptor_name = "snowflake-database"
12+
comment = "Sample shared Database"
13+
replace_invalid_characters = true
14+
default_ddl_collation = "UTF8"
15+
log_level = "INFO"
16+
trace_level = "ON_EVENT"
17+
18+
suspend_task_after_num_failures = 1
19+
task_auto_retry_attempts = 1
20+
user_task_managed_initial_warehouse_size = "X-Small"
21+
user_task_minimum_trigger_interval_in_seconds = 300
22+
user_task_timeout_ms = 200
23+
quoted_identifiers_ignore_case = true
24+
enable_console_output = true
25+
26+
create_default_roles = true
27+
28+
roles = {
29+
readonly = {
30+
comment = "Read-only role"
31+
granted_roles = [resource.snowflake_account_role.this.name]
32+
granted_to_users = [resource.snowflake_user.this.name]
33+
}
1134
}
1235
}
36+
1337
```
1438

1539
## Usage
40+
41+
1. Configure private sharing on producer Snowflake Account (remember that both producer and consumer accounts have to be deployed on the same Cloud Provider for. ex. AWS and in the same region for. ex. `eu-west-1`)
42+
43+
This step can be done manually using Snowsight UI or by running [create_share.sql](./create-share.sql) script on producer Snowflake account.
44+
45+
When using the script, please remember to properly define consumer account details in the last line:
46+
47+
```sql
48+
ALTER SHARE sample_share ADD ACCOUNT="<orgname.accountname>";
1649
```
50+
51+
2. With share configured in step 1., run terraform on consumer account using below commands
52+
53+
```shell
1754
terraform init
1855
terraform plan -var-file fixtures.tfvars -out tfplan
1956
terraform apply tfplan
2057
```
2158

59+
**Please remember to pass share details (from step 1.) to `from_share` variable.**
60+
61+
```shell
62+
$ terraform plan
63+
var.from_share
64+
A fully qualified path to a share from which the database will be created. A fully qualified path follows the format of `<organization_name>.<account_name>.<share_name>`
65+
66+
Enter a value: <orgname.accountname.sharename>
67+
```
68+
2269
<!-- BEGIN_TF_DOCS -->
2370

2471

@@ -35,6 +82,7 @@ terraform apply tfplan
3582
| <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 |
3683
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `null` | no |
3784
| <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 |
85+
| <a name="input_from_share"></a> [from\_share](#input\_from\_share) | A fully qualified path to a share from which the database will be created. A fully qualified path follows the format of `<organization_name>.<account_name>.<share_name>` | `string` | n/a | yes |
3886
| <a name="input_id_length_limit"></a> [id\_length\_limit](#input\_id\_length\_limit) | Limit `id` to this many characters (minimum 6).<br>Set to `0` for unlimited length.<br>Set to `null` for keep the existing setting, which defaults to `0`.<br>Does not affect `id_full`. | `number` | `null` | no |
3987
| <a name="input_label_key_case"></a> [label\_key\_case](#input\_label\_key\_case) | Controls the letter case of the `tags` keys (label names) for tags generated by this module.<br>Does not affect keys of tags passed in via the `tags` input.<br>Possible values: `lower`, `title`, `upper`.<br>Default value: `title`. | `string` | `null` | no |
4088
| <a name="input_label_order"></a> [label\_order](#input\_label\_order) | The order in which the labels (ID elements) appear in the `id`.<br>Defaults to ["namespace", "environment", "stage", "name", "attributes"].<br>You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | `list(string)` | `null` | no |
@@ -51,7 +99,7 @@ terraform apply tfplan
5199

52100
| Name | Source | Version |
53101
|------|--------|---------|
54-
| <a name="module_terraform_module_template"></a> [terraform\_module\_template](#module\_terraform\_module\_template) | ../../ | n/a |
102+
| <a name="module_snowflake_shared_database"></a> [snowflake\_shared\_database](#module\_snowflake\_shared\_database) | ../.. | n/a |
55103
| <a name="module_this"></a> [this](#module\_this) | cloudposse/label/null | 0.25.0 |
56104

57105
## Outputs
@@ -62,16 +110,21 @@ terraform apply tfplan
62110

63111
## Providers
64112

65-
No providers.
113+
| Name | Version |
114+
|------|---------|
115+
| <a name="provider_snowflake"></a> [snowflake](#provider\_snowflake) | >= 0.95 |
66116

67117
## Requirements
68118

69119
| Name | Version |
70120
|------|---------|
71-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
72-
| <a name="requirement_null"></a> [null](#requirement\_null) | 3.1.1 |
121+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3 |
122+
| <a name="requirement_snowflake"></a> [snowflake](#requirement\_snowflake) | >= 0.95 |
73123

74124
## Resources
75125

76-
No resources.
126+
| Name | Type |
127+
|------|------|
128+
| [snowflake_account_role.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/account_role) | resource |
129+
| [snowflake_user.this](https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/user) | resource |
77130
<!-- END_TF_DOCS -->

examples/complete/create-share.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
CREATE DATABASE sample_shared_db;
2+
3+
CREATE SCHEMA sample_shared_db.shared_schema;
4+
5+
CREATE TABLE sample_shared_db.shared_schema.sample_table (
6+
ID NUMBER,
7+
VALUE TEXT
8+
);
9+
10+
INSERT INTO sample_shared_db.shared_schema.sample_table
11+
VALUES
12+
(1, 'TEST VALUE'),
13+
(2, 'ANOTHER TEST VALUE');
14+
15+
CREATE SHARE sample_share COMMENT = 'Test Share';
16+
17+
GRANT USAGE ON DATABASE sample_shared_db TO SHARE sample_share;
18+
19+
GRANT USAGE ON SCHEMA sample_shared_db.shared_schema TO SHARE sample_share;
20+
21+
GRANT SELECT ON ALL TABLES IN SCHEMA sample_shared_db.shared_schema TO SHARE sample_share;
22+
23+
ALTER SHARE sample_share ADD ACCOUNT="<orgname.accountname>";

examples/complete/fixtures.tfvars

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
descriptor_formats = {
2-
3-
}
1+
namespace = "gid"
2+
#stage = "example"
3+
#environment = "dev"
44

5-
tags = {
6-
Terraform = "True"
5+
descriptor_formats = {
6+
snowflake-role = {
7+
labels = ["attributes", "name"]
8+
format = "%v_%v"
9+
}
10+
snowflake-database = {
11+
labels = ["environment", "stage", "name", "attributes"]
12+
format = "%v_%v_%v_%v"
13+
}
714
}

examples/complete/main.tf

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
1-
module "terraform_module_template" {
2-
source = "../../"
3-
context = module.this.context
1+
resource "snowflake_user" "this" {
2+
name = "SAMPLE_USER"
3+
}
4+
5+
resource "snowflake_account_role" "this" {
6+
name = "SAMPLE_ROLE"
7+
}
8+
9+
module "snowflake_shared_database" {
10+
source = "../.."
11+
12+
name = "SHARED_DATABASE"
13+
context = module.this.context
14+
from_share = var.from_share
15+
16+
descriptor_name = "snowflake-database"
17+
comment = "Sample shared Database"
18+
replace_invalid_characters = true
19+
default_ddl_collation = "UTF8"
20+
log_level = "INFO"
21+
trace_level = "ON_EVENT"
22+
23+
suspend_task_after_num_failures = 1
24+
task_auto_retry_attempts = 1
25+
user_task_managed_initial_warehouse_size = "X-Small"
26+
user_task_minimum_trigger_interval_in_seconds = 300
27+
user_task_timeout_ms = 200
28+
quoted_identifiers_ignore_case = true
29+
enable_console_output = true
30+
31+
create_default_roles = true
432

5-
example_var = "This is a example value."
6-
sub_resource = {
7-
example_var = "This is a example value of sub resource."
33+
roles = {
34+
readonly = {
35+
comment = "Read-only role"
36+
granted_roles = [resource.snowflake_account_role.this.name]
37+
granted_to_users = [resource.snowflake_user.this.name]
38+
}
839
}
940
}

examples/complete/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
output "example_output" {
22
description = "Example output of the module"
3-
value = module.terraform_module_template
3+
value = module.snowflake_shared_database
44
}

examples/complete/providers.tf

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
provider "null" {
2-
# Configuration options
3-
}
1+
provider "snowflake" {}

examples/complete/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "from_share" {
2+
description = "A fully qualified path to a share from which the database will be created. A fully qualified path follows the format of `<organization_name>.<account_name>.<share_name>`"
3+
type = string
4+
}

examples/complete/versions.tf

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
terraform {
2-
required_version = ">= 1.3.0"
3-
2+
required_version = ">= 1.3"
43
required_providers {
5-
null = {
6-
source = "hashicorp/null"
7-
version = "3.1.1"
4+
snowflake = {
5+
source = "Snowflake-Labs/snowflake"
6+
version = ">= 0.95"
87
}
98
}
109
}

0 commit comments

Comments
 (0)