Skip to content

Commit c0d0320

Browse files
Sean McIntyreGary James
authored andcommitted
Add dbt_cloud_snowflake_credential resource
1 parent 49b5135 commit c0d0320

5 files changed

Lines changed: 367 additions & 4 deletions

File tree

pkg/dbt_cloud/environment.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type Environment struct {
2929
State int `json:"state"`
3030
Account_Id int `json:"account_id"`
3131
Project_Id int `json:"project_id"`
32-
Credentials_Id *int `json:"credentials_id"`
32+
Credential_Id *int `json:"credentials_id"`
3333
Name string `json:"name"`
3434
Dbt_Version string `json:"dbt_version"`
3535
Type string `json:"type"`
@@ -63,7 +63,7 @@ func (c *Client) GetEnvironment(projectId int, environmentId int) (*Environment,
6363
return nil, fmt.Errorf("did not find environment ID %d in project ID %d", environmentId, projectId)
6464
}
6565

66-
func (c *Client) CreateEnvironment(isActive bool, projectId int, name string, dbtVersion string, type_ string, useCustomBranch bool, customBranch string) (*Environment, error) {
66+
func (c *Client) CreateEnvironment(isActive bool, projectId int, name string, dbtVersion string, type_ string, useCustomBranch bool, customBranch string, credentialId int) (*Environment, error) {
6767
state := 1
6868
if !isActive {
6969
state = 2
@@ -76,7 +76,7 @@ func (c *Client) CreateEnvironment(isActive bool, projectId int, name string, db
7676
Name: name,
7777
Dbt_Version: dbtVersion,
7878
Type: type_,
79-
Credentials_Id: nil,
79+
Credential_Id: &credentialId,
8080
Use_Custom_Branch: useCustomBranch,
8181
Custom_Branch: customBranch,
8282
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package dbt_cloud
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"strings"
8+
)
9+
10+
type snowflakeCredentialResponseStatus struct {
11+
Code int `json:"code"`
12+
Is_Success bool `json:"is_success"`
13+
User_Message string `json:"user_message"`
14+
Developer_Message string `json:"developer_message"`
15+
}
16+
17+
type SnowflakeCredentialListResponse struct {
18+
Data []SnowflakeCredential `json:"data"`
19+
Status snowflakeCredentialResponseStatus `json:"status"`
20+
}
21+
22+
type SnowflakeCredentialResponse struct {
23+
Data SnowflakeCredential `json:"data"`
24+
Status snowflakeCredentialResponseStatus `json:"status"`
25+
}
26+
27+
type SnowflakeCredential struct {
28+
ID *int `json:"id"`
29+
Account_Id int `json:"account_id"`
30+
Project_Id int `json:"project_id"`
31+
Type string `json:"type"`
32+
State int `json:"state"`
33+
Threads int `json:"threads"`
34+
User string `json:"user"`
35+
Password *string `json:"password"`
36+
Auth_Type string `json:"auth_type"`
37+
Schema string `json:"schema"`
38+
}
39+
40+
func (c *Client) GetSnowflakeCredential(projectId int, credentialId int) (*SnowflakeCredential, error) {
41+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/v3/accounts/%d/projects/%d/credentials/", HostURL, c.AccountID, projectId), nil)
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
body, err := c.doRequest(req)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
snowflakeCredentialListResponse := SnowflakeCredentialListResponse{}
52+
err = json.Unmarshal(body, &snowflakeCredentialListResponse)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
for i, credential := range snowflakeCredentialListResponse.Data {
58+
if *credential.ID == credentialId {
59+
return &snowflakeCredentialListResponse.Data[i], nil
60+
}
61+
}
62+
63+
return nil, fmt.Errorf("did not find credential ID %d in project ID %d", credentialId, projectId)
64+
}
65+
66+
func (c *Client) CreateSnowflakeCredential(projectId int, type_ string, isActive bool, schema string, user string, password string, authType string) (*SnowflakeCredential, error) {
67+
newSnowflakeCredential := SnowflakeCredential{
68+
Account_Id: c.AccountID,
69+
Project_Id: projectId,
70+
Type: type_,
71+
State: 1, // TODO: make variable
72+
Schema: schema,
73+
User: user,
74+
Password: &password,
75+
Auth_Type: authType,
76+
Threads: 1, // TODO: make variable
77+
}
78+
newSnowflakeCredentialData, err := json.Marshal(newSnowflakeCredential)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v3/accounts/%d/projects/%d/credentials/", HostURL, c.AccountID, projectId), strings.NewReader(string(newSnowflakeCredentialData)))
84+
if err != nil {
85+
return nil, err
86+
}
87+
88+
body, err := c.doRequest(req)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
snowflakeCredentialResponse := SnowflakeCredentialResponse{}
94+
err = json.Unmarshal(body, &snowflakeCredentialResponse)
95+
if err != nil {
96+
return nil, err
97+
}
98+
99+
return &snowflakeCredentialResponse.Data, nil
100+
}
101+
102+
func (c *Client) UpdateSnowflakeCredential(projectId int, credentialId int, snowflakeCredential SnowflakeCredential) (*SnowflakeCredential, error) {
103+
snowflakeCredentialData, err := json.Marshal(snowflakeCredential)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v3/accounts/%d/projects/%d/credentials/%d", HostURL, c.AccountID, projectId, credentialId), strings.NewReader(string(snowflakeCredentialData)))
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
body, err := c.doRequest(req)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
snowflakeCredentialResponse := SnowflakeCredentialResponse{}
119+
err = json.Unmarshal(body, &snowflakeCredentialResponse)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
return &snowflakeCredentialResponse.Data, nil
125+
}

pkg/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func Provider() *schema.Provider {
3535
"dbt_cloud_job": resources.ResourceJob(),
3636
"dbt_cloud_project": resources.ResourceProject(),
3737
"dbt_cloud_environment": resources.ResourceEnvironment(),
38+
"dbt_cloud_snowflake_credential": resources.ResourceSnowflakeCredential(),
3839
},
3940
ConfigureContextFunc: providerConfigure,
4041
}

pkg/resources/environment.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ func ResourceEnvironment() *schema.Resource {
3434
Required: true,
3535
Description: "Project ID to create the environment in",
3636
},
37+
"credential_id": &schema.Schema{
38+
Type: schema.TypeInt,
39+
Optional: true,
40+
Default: nil,
41+
Description: "Credential ID to create the environment with",
42+
},
3743
"name": &schema.Schema{
3844
Type: schema.TypeString,
3945
Required: true,
@@ -88,13 +94,14 @@ func resourceEnvironmentCreate(ctx context.Context, d *schema.ResourceData, m in
8894

8995
isActive := d.Get("is_active").(bool)
9096
projectId := d.Get("project_id").(int)
97+
credentialId := d.Get("credential_id").(int)
9198
name := d.Get("name").(string)
9299
dbtVersion := d.Get("dbt_version").(string)
93100
type_ := d.Get("type").(string)
94101
useCustomBranch := d.Get("use_custom_branch").(bool)
95102
customBranch := d.Get("custom_branch").(string)
96103

97-
environment, err := c.CreateEnvironment(isActive, projectId, name, dbtVersion, type_, useCustomBranch, customBranch)
104+
environment, err := c.CreateEnvironment(isActive, projectId, name, dbtVersion, type_, useCustomBranch, customBranch, credentialId)
98105
if err != nil {
99106
return diag.FromErr(err)
100107
}

0 commit comments

Comments
 (0)