|
| 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 | +} |
0 commit comments