Skip to content

Commit d92beac

Browse files
author
Gary James
committed
Feature - Environments and Snowflake credentials
1 parent c0d0320 commit d92beac

17 files changed

Lines changed: 482 additions & 77 deletions

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.46
1+
0.0.69
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_environment Data Source - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_environment (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **dbt_version** (String) Version number of dbt to use in this environment
21+
- **name** (String) Environment name
22+
- **project_id** (Number) Project ID to create the environment in
23+
- **type** (String) The type of environment (must be either development or deployment)
24+
25+
### Optional
26+
27+
- **credential_id** (Number) Credential ID to create the environment with
28+
- **custom_branch** (String) Which custom branch to use in this environment
29+
- **id** (String) The ID of this resource.
30+
- **is_active** (Boolean) Whether the environment is active
31+
- **use_custom_branch** (Boolean) Whether to use a custom git branch in this environment
32+
33+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_snowflake_credential Data Source - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_snowflake_credential (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **auth_type** (String) The type of Snowflake credential ('password' only currently supported in Terraform)
21+
- **num_threads** (Number) Number of threads to use
22+
- **password** (String, Sensitive) Password for Snowflake
23+
- **project_id** (Number) Project ID to create the Snowflake credential in
24+
- **schema** (String) Default schema name
25+
- **user** (String) Username for Snowflake
26+
27+
### Optional
28+
29+
- **id** (String) The ID of this resource.
30+
- **is_active** (Boolean) Whether the Snowflake credential is active
31+
32+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_environment Resource - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_environment (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **dbt_version** (String) Version number of dbt to use in this environment
21+
- **name** (String) Environment name
22+
- **project_id** (Number) Project ID to create the environment in
23+
- **type** (String) The type of environment (must be either development or deployment)
24+
25+
### Optional
26+
27+
- **credential_id** (Number) Credential ID to create the environment with
28+
- **custom_branch** (String) Which custom branch to use in this environment
29+
- **id** (String) The ID of this resource.
30+
- **is_active** (Boolean) Whether the environment is active
31+
- **use_custom_branch** (Boolean) Whether to use a custom git branch in this environment
32+
33+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "dbt_cloud_snowflake_credential Resource - terraform-provider-dbt-cloud"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# dbt_cloud_snowflake_credential (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- **auth_type** (String) The type of Snowflake credential ('password' only currently supported in Terraform)
21+
- **num_threads** (Number) Number of threads to use
22+
- **password** (String, Sensitive) Password for Snowflake
23+
- **project_id** (Number) Project ID to create the Snowflake credential in
24+
- **schema** (String) Default schema name
25+
- **user** (String) Username for Snowflake
26+
27+
### Optional
28+
29+
- **id** (String) The ID of this resource.
30+
- **is_active** (Boolean) Whether the Snowflake credential is active
31+
32+
### Read-Only
33+
34+
- **credential_id** (Number) The system Snowflake credential ID
35+
36+

pkg/data_sources/environment.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package data_sources
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
var environmentSchema = map[string]*schema.Schema{
13+
"is_active": &schema.Schema{
14+
Type: schema.TypeBool,
15+
Optional: true,
16+
Default: true,
17+
Description: "Whether the environment is active",
18+
},
19+
"project_id": &schema.Schema{
20+
Type: schema.TypeInt,
21+
Required: true,
22+
Description: "Project ID to create the environment in",
23+
},
24+
"credential_id": &schema.Schema{
25+
Type: schema.TypeInt,
26+
Optional: true,
27+
Default: nil,
28+
Description: "Credential ID to create the environment with",
29+
},
30+
"name": &schema.Schema{
31+
Type: schema.TypeString,
32+
Required: true,
33+
Description: "Environment name",
34+
},
35+
"dbt_version": &schema.Schema{
36+
Type: schema.TypeString,
37+
Required: true,
38+
Description: "Version number of dbt to use in this environment",
39+
},
40+
"type": &schema.Schema{
41+
Type: schema.TypeString,
42+
Required: true,
43+
Description: "The type of environment (must be either development or deployment)",
44+
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
45+
type_ := val.(string)
46+
switch type_ {
47+
case
48+
"development",
49+
"deployment":
50+
return
51+
}
52+
errs = append(errs, fmt.Errorf("%q must be either development or deployment, got: %q", key, type_))
53+
return
54+
},
55+
},
56+
"use_custom_branch": &schema.Schema{
57+
Type: schema.TypeBool,
58+
Optional: true,
59+
Default: false,
60+
Description: "Whether to use a custom git branch in this environment",
61+
},
62+
"custom_branch": &schema.Schema{
63+
Type: schema.TypeString,
64+
Optional: true,
65+
Default: "",
66+
Description: "Which custom branch to use in this environment",
67+
},
68+
}
69+
70+
func DatasourceEnvironment() *schema.Resource {
71+
return &schema.Resource{
72+
ReadContext: datasourceEnvironmentRead,
73+
Schema: environmentSchema,
74+
}
75+
}
76+
77+
func datasourceEnvironmentRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
78+
c := m.(*dbt_cloud.Client)
79+
80+
var diags diag.Diagnostics
81+
82+
environmentID := d.Get("environment_id").(int)
83+
projectID := d.Get("project_id").(int)
84+
85+
environment, err := c.GetEnvironment(projectID, environmentID)
86+
if err != nil {
87+
return diag.FromErr(err)
88+
}
89+
90+
if err := d.Set("is_active", environment.State == dbt_cloud.STATE_ACTIVE); err != nil {
91+
return diag.FromErr(err)
92+
}
93+
if err := d.Set("project_id", environment.Project_Id); err != nil {
94+
return diag.FromErr(err)
95+
}
96+
if err := d.Set("credential_id", environment.Credential_Id); err != nil {
97+
return diag.FromErr(err)
98+
}
99+
if err := d.Set("name", environment.Name); err != nil {
100+
return diag.FromErr(err)
101+
}
102+
if err := d.Set("dbt_version", environment.Dbt_Version); err != nil {
103+
return diag.FromErr(err)
104+
}
105+
if err := d.Set("type", environment.Type); err != nil {
106+
return diag.FromErr(err)
107+
}
108+
if err := d.Set("use_custom_branch", environment.Use_Custom_Branch); err != nil {
109+
return diag.FromErr(err)
110+
}
111+
if err := d.Set("custom_branch", environment.Custom_Branch); err != nil {
112+
return diag.FromErr(err)
113+
}
114+
115+
d.SetId(fmt.Sprintf("%d%s%d", environment.Project_Id, dbt_cloud.ID_DELIMITER, *environment.ID))
116+
117+
return diags
118+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package data_sources
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/gthesheep/terraform-provider-dbt-cloud/pkg/dbt_cloud"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
var snowflakeCredentialSchema = map[string]*schema.Schema{
13+
"is_active": &schema.Schema{
14+
Type: schema.TypeBool,
15+
Optional: true,
16+
Default: true,
17+
Description: "Whether the Snowflake credential is active",
18+
},
19+
"project_id": &schema.Schema{
20+
Type: schema.TypeInt,
21+
Required: true,
22+
Description: "Project ID to create the Snowflake credential in",
23+
},
24+
"auth_type": &schema.Schema{
25+
Type: schema.TypeString,
26+
Required: true,
27+
Description: "The type of Snowflake credential ('password' only currently supported in Terraform)",
28+
ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) {
29+
type_ := val.(string)
30+
switch type_ {
31+
case
32+
"password":
33+
return
34+
}
35+
errs = append(errs, fmt.Errorf("%q must be password, got: %q", key, type_))
36+
return
37+
},
38+
},
39+
"schema": &schema.Schema{
40+
Type: schema.TypeString,
41+
Required: true,
42+
Description: "Default schema name",
43+
},
44+
"user": &schema.Schema{
45+
Type: schema.TypeString,
46+
Required: true,
47+
Description: "Username for Snowflake",
48+
},
49+
"password": &schema.Schema{
50+
Type: schema.TypeString,
51+
Required: true,
52+
Sensitive: true,
53+
Description: "Password for Snowflake",
54+
},
55+
"num_threads": &schema.Schema{
56+
Type: schema.TypeInt,
57+
Required: true,
58+
Description: "Number of threads to use",
59+
},
60+
// TODO: add private_key and private_key_passphrase
61+
}
62+
63+
func DatasourceSnowflakeCredential() *schema.Resource {
64+
return &schema.Resource{
65+
ReadContext: snowflakeCredentialRead,
66+
Schema: snowflakeCredentialSchema,
67+
}
68+
}
69+
70+
func snowflakeCredentialRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
71+
c := m.(*dbt_cloud.Client)
72+
73+
var diags diag.Diagnostics
74+
75+
credentialID := d.Get("credential_id").(int)
76+
projectID := d.Get("project_id").(int)
77+
78+
snowflakeCredential, err := c.GetSnowflakeCredential(projectID, credentialID)
79+
if err != nil {
80+
return diag.FromErr(err)
81+
}
82+
83+
if err := d.Set("is_active", snowflakeCredential.State == dbt_cloud.STATE_ACTIVE); err != nil {
84+
return diag.FromErr(err)
85+
}
86+
if err := d.Set("project_id", snowflakeCredential.Project_Id); err != nil {
87+
return diag.FromErr(err)
88+
}
89+
if err := d.Set("auth_type", snowflakeCredential.Auth_Type); err != nil {
90+
return diag.FromErr(err)
91+
}
92+
if err := d.Set("schema", snowflakeCredential.Schema); err != nil {
93+
return diag.FromErr(err)
94+
}
95+
if err := d.Set("user", snowflakeCredential.User); err != nil {
96+
return diag.FromErr(err)
97+
}
98+
if err := d.Set("password", snowflakeCredential.Password); err != nil {
99+
return diag.FromErr(err)
100+
}
101+
if err := d.Set("num_threads", snowflakeCredential.Threads); err != nil {
102+
return diag.FromErr(err)
103+
}
104+
105+
d.SetId(fmt.Sprintf("%d%s%d", snowflakeCredential.Project_Id, dbt_cloud.ID_DELIMITER, *snowflakeCredential.ID))
106+
107+
return diags
108+
}

pkg/dbt_cloud/common.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package dbt_cloud
2+
3+
const (
4+
STATE_ACTIVE = 1
5+
STATE_DELETED = 2
6+
ID_DELIMITER = ":"
7+
)

0 commit comments

Comments
 (0)