|
| 1 | +package dbt_cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +type PostgresCredential struct { |
| 11 | + ID *int `json:"id"` |
| 12 | + Account_Id int `json:"account_id"` |
| 13 | + Project_Id int `json:"project_id"` |
| 14 | + Type string `json:"type"` |
| 15 | + State int `json:"state"` |
| 16 | + Threads int `json:"threads"` |
| 17 | + Username string `json:"username"` |
| 18 | + Default_Schema string `json:"default_schema"` |
| 19 | + Target_Name string `json:"target_name"` |
| 20 | + Password string `json:"password,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type PostgresCredentialListResponse struct { |
| 24 | + Data []PostgresCredential `json:"data"` |
| 25 | + Status ResponseStatus `json:"status"` |
| 26 | +} |
| 27 | + |
| 28 | +type PostgresCredentialResponse struct { |
| 29 | + Data PostgresCredential `json:"data"` |
| 30 | + Status ResponseStatus `json:"status"` |
| 31 | +} |
| 32 | + |
| 33 | +// GetPostgresCredential retrieves a specific Postgres credential by its ID |
| 34 | +func (c *Client) GetPostgresCredential(projectId int, credentialId int) (*PostgresCredential, error) { |
| 35 | + req, err := http.NewRequest("GET", fmt.Sprintf("%s/v3/accounts/%d/projects/%d/credentials/", c.HostURL, c.AccountID, projectId), nil) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + body, err := c.doRequest(req) |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + PostgresCredentialListResponse := PostgresCredentialListResponse{} |
| 46 | + err = json.Unmarshal(body, &PostgresCredentialListResponse) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + for i, credential := range PostgresCredentialListResponse.Data { |
| 52 | + if *credential.ID == credentialId { |
| 53 | + return &PostgresCredentialListResponse.Data[i], nil |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return nil, fmt.Errorf("did not find credential ID %d in project ID %d", credentialId, projectId) |
| 58 | +} |
| 59 | + |
| 60 | +// CreatePostgresCredential creates a new Postgres credential |
| 61 | +func (c *Client) CreatePostgresCredential(projectId int, isActive bool, type_ string, defaultSchema string, targetName string, username string, password string, numThreads int) (*PostgresCredential, error) { |
| 62 | + newPostgresCredential := PostgresCredential{ |
| 63 | + Account_Id: c.AccountID, |
| 64 | + Project_Id: projectId, |
| 65 | + Type: type_, |
| 66 | + State: STATE_ACTIVE, // TODO: make variable |
| 67 | + Threads: numThreads, |
| 68 | + Username: username, |
| 69 | + Default_Schema: defaultSchema, |
| 70 | + Target_Name: targetName, |
| 71 | + Password: password, |
| 72 | + } |
| 73 | + newPostgresCredentialData, err := json.Marshal(newPostgresCredential) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + req, err := http.NewRequest("POST", fmt.Sprintf("%s/v3/accounts/%d/projects/%d/credentials/", c.HostURL, c.AccountID, projectId), strings.NewReader(string(newPostgresCredentialData))) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + body, err := c.doRequest(req) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + postgresCredentialResponse := PostgresCredentialResponse{} |
| 89 | + err = json.Unmarshal(body, &postgresCredentialResponse) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + |
| 94 | + return &postgresCredentialResponse.Data, nil |
| 95 | +} |
| 96 | + |
| 97 | +// UpdatePostgresCredential updates an existing Postgres credential |
| 98 | +func (c *Client) UpdatePostgresCredential(projectId int, credentialId int, postgresCredential PostgresCredential) (*PostgresCredential, error) { |
| 99 | + postgresCredentialData, err := json.Marshal(postgresCredential) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + req, err := http.NewRequest("POST", fmt.Sprintf("%s/v3/accounts/%d/projects/%d/credentials/%d/", c.HostURL, c.AccountID, projectId, credentialId), strings.NewReader(string(postgresCredentialData))) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + |
| 109 | + body, err := c.doRequest(req) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + postgresCredentialResponse := PostgresCredentialResponse{} |
| 115 | + err = json.Unmarshal(body, &postgresCredentialResponse) |
| 116 | + if err != nil { |
| 117 | + return nil, err |
| 118 | + } |
| 119 | + |
| 120 | + return &postgresCredentialResponse.Data, nil |
| 121 | +} |
| 122 | + |
| 123 | +// DeletePostgresCredential deletes a Postgres credential by its ID |
| 124 | +func (c *Client) DeletePostgresCredential(credentialId, projectId string) (string, error) { |
| 125 | + req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/v3/accounts/%d/projects/%s/credentials/%s/", c.HostURL, c.AccountID, projectId, credentialId), nil) |
| 126 | + if err != nil { |
| 127 | + return "", err |
| 128 | + } |
| 129 | + |
| 130 | + _, err = c.doRequest(req) |
| 131 | + if err != nil { |
| 132 | + return "", err |
| 133 | + } |
| 134 | + |
| 135 | + return "", err |
| 136 | +} |
0 commit comments