|
| 1 | +package dbt_cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 11 | +) |
| 12 | + |
| 13 | +type TeradataCredentialResponse struct { |
| 14 | + Data TeradataCredentialData `json:"data"` |
| 15 | + Status ResponseStatus `json:"status"` |
| 16 | +} |
| 17 | + |
| 18 | +type TeradataUnencryptedCredentialDetails struct { |
| 19 | + Schema string `json:"schema"` |
| 20 | + TargetName string `json:"target_name"` |
| 21 | + Threads int `json:"threads"` |
| 22 | +} |
| 23 | + |
| 24 | +// TeradataCredentialData represents the data returned by the API for an Teradata credential |
| 25 | +type TeradataCredentialData struct { |
| 26 | + ID *int `json:"id"` |
| 27 | + AccountID int `json:"account_id"` |
| 28 | + Threads int `json:"threads"` |
| 29 | + TargetName string `json:"target_name"` |
| 30 | + AdapterID int `json:"adapter_id"` |
| 31 | + AdapterVersion string `json:"adapter_version,omitempty"` |
| 32 | + UnencryptedCredentialDetails TeradataUnencryptedCredentialDetails `json:"unencrypted_credential_details"` |
| 33 | +} |
| 34 | + |
| 35 | +// TeradataCredentialRequest is used for creating and updating Teradata credentials |
| 36 | +// It doesn't include the UnencryptedCredentialDetails field which is only returned by the API |
| 37 | +type TeradataCredentialRequest struct { |
| 38 | + ID *int `json:"id,omitempty"` |
| 39 | + AccountID int `json:"account_id"` |
| 40 | + ProjectID int `json:"project_id"` |
| 41 | + Type string `json:"type"` |
| 42 | + State int `json:"state"` |
| 43 | + Threads int `json:"threads"` |
| 44 | + TargetName string `json:"target_name"` |
| 45 | + AdapterID int `json:"adapter_id,omitempty"` |
| 46 | + AdapterVersion string `json:"adapter_version,omitempty"` |
| 47 | + CredentialDetails AdapterCredentialDetails `json:"credential_details"` |
| 48 | +} |
| 49 | + |
| 50 | +func (c *Client) GetTeradataCredential( |
| 51 | + projectId int, |
| 52 | + credentialId int, |
| 53 | +) (*TeradataCredentialData, error) { |
| 54 | + req, err := http.NewRequest( |
| 55 | + "GET", |
| 56 | + fmt.Sprintf( |
| 57 | + "%s/v3/accounts/%d/projects/%d/credentials/%d/?include_related=[adapter]", |
| 58 | + c.HostURL, |
| 59 | + c.AccountID, |
| 60 | + projectId, |
| 61 | + credentialId, |
| 62 | + ), |
| 63 | + nil, |
| 64 | + ) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + |
| 69 | + body, err := c.doRequest(req) |
| 70 | + if err != nil { |
| 71 | + return nil, err |
| 72 | + } |
| 73 | + |
| 74 | + credentialResponse := TeradataCredentialResponse{} |
| 75 | + err = json.Unmarshal(body, &credentialResponse) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return &credentialResponse.Data, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (c *Client) CreateTeradataCredential( |
| 84 | + ctx context.Context, |
| 85 | + projectId int, |
| 86 | + username string, |
| 87 | + password string, |
| 88 | + schema string, |
| 89 | + threads int, |
| 90 | +) (*TeradataCredentialData, error) { |
| 91 | + credentialDetails, err := GenerateTeradataCredentialDetails( |
| 92 | + username, |
| 93 | + password, |
| 94 | + schema, |
| 95 | + threads, |
| 96 | + ) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + credential := TeradataCredentialRequest{ |
| 102 | + ID: nil, |
| 103 | + AccountID: c.AccountID, |
| 104 | + ProjectID: projectId, |
| 105 | + Type: "adapter", |
| 106 | + State: STATE_ACTIVE, |
| 107 | + TargetName: DEFAULT_TARGET_NAME, |
| 108 | + Threads: threads, |
| 109 | + CredentialDetails: credentialDetails, |
| 110 | + AdapterVersion: "teradata_v0", |
| 111 | + } |
| 112 | + |
| 113 | + rb, err := json.Marshal(credential) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + tflog.Debug(ctx, fmt.Sprintf("CreateTeradataCredential: %s", string(rb))) |
| 118 | + |
| 119 | + req, err := http.NewRequest( |
| 120 | + "POST", |
| 121 | + fmt.Sprintf( |
| 122 | + "%s/v3/accounts/%d/projects/%d/credentials/", |
| 123 | + c.HostURL, |
| 124 | + c.AccountID, |
| 125 | + projectId, |
| 126 | + ), |
| 127 | + strings.NewReader(string(rb)), |
| 128 | + ) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + body, err := c.doRequest(req) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + |
| 138 | + tflog.Debug(ctx, fmt.Sprintf("CreateTeradataCredentialResponse: %s", string(body))) |
| 139 | + |
| 140 | + credentialResponse := TeradataCredentialResponse{} |
| 141 | + err = json.Unmarshal(body, &credentialResponse) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + return &credentialResponse.Data, nil |
| 147 | +} |
| 148 | + |
| 149 | +func (c *Client) UpdateTeradataCredential( |
| 150 | + projectId int, |
| 151 | + credentialId int, |
| 152 | + teradataCredential TeradataCredentialRequest, |
| 153 | +) (*TeradataCredentialData, error) { |
| 154 | + rb, err := json.Marshal(teradataCredential) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + req, err := http.NewRequest( |
| 160 | + "POST", |
| 161 | + fmt.Sprintf( |
| 162 | + "%s/v3/accounts/%d/projects/%d/credentials/%d/", |
| 163 | + c.HostURL, |
| 164 | + c.AccountID, |
| 165 | + projectId, |
| 166 | + credentialId, |
| 167 | + ), |
| 168 | + strings.NewReader(string(rb)), |
| 169 | + ) |
| 170 | + if err != nil { |
| 171 | + return nil, err |
| 172 | + } |
| 173 | + |
| 174 | + body, err := c.doRequest(req) |
| 175 | + if err != nil { |
| 176 | + return nil, err |
| 177 | + } |
| 178 | + |
| 179 | + credentialResponse := TeradataCredentialResponse{} |
| 180 | + err = json.Unmarshal(body, &credentialResponse) |
| 181 | + if err != nil { |
| 182 | + return nil, err |
| 183 | + } |
| 184 | + |
| 185 | + return &credentialResponse.Data, nil |
| 186 | +} |
| 187 | + |
| 188 | +func GenerateTeradataCredentialDetails( |
| 189 | + username string, |
| 190 | + password string, |
| 191 | + schema string, |
| 192 | + threads int, |
| 193 | +) (AdapterCredentialDetails, error) { |
| 194 | + // Create the credential details structure based on the payload example |
| 195 | + fields := map[string]AdapterCredentialField{ |
| 196 | + "user": { |
| 197 | + Metadata: AdapterCredentialFieldMetadata{ |
| 198 | + Label: "Teradata username", |
| 199 | + Description: "The username", |
| 200 | + Field_Type: "text", |
| 201 | + Encrypt: false, |
| 202 | + Overrideable: false, |
| 203 | + Validation: AdapterCredentialFieldMetadataValidation{Required: true}, |
| 204 | + }, |
| 205 | + Value: username, |
| 206 | + }, |
| 207 | + "password": { |
| 208 | + Metadata: AdapterCredentialFieldMetadata{ |
| 209 | + Label: "Teradata password", |
| 210 | + Description: "User's password", |
| 211 | + Field_Type: "text", |
| 212 | + Encrypt: true, |
| 213 | + Overrideable: false, |
| 214 | + Validation: AdapterCredentialFieldMetadataValidation{Required: true}, |
| 215 | + }, |
| 216 | + Value: password, |
| 217 | + }, |
| 218 | + "schema": { |
| 219 | + Metadata: AdapterCredentialFieldMetadata{ |
| 220 | + Label: "Schema", |
| 221 | + Description: "The schema to build models into", |
| 222 | + Field_Type: "text", |
| 223 | + Encrypt: false, |
| 224 | + Overrideable: false, |
| 225 | + Validation: AdapterCredentialFieldMetadataValidation{Required: true}, |
| 226 | + }, |
| 227 | + Value: schema, |
| 228 | + }, |
| 229 | + "threads": { |
| 230 | + Metadata: AdapterCredentialFieldMetadata{ |
| 231 | + Label: "Threads", |
| 232 | + Description: "The number of threads to use for dbt operations", |
| 233 | + Field_Type: "number", |
| 234 | + Encrypt: false, |
| 235 | + Overrideable: false, |
| 236 | + Validation: AdapterCredentialFieldMetadataValidation{Required: true}, |
| 237 | + }, |
| 238 | + Value: threads, |
| 239 | + }, |
| 240 | + } |
| 241 | + |
| 242 | + return AdapterCredentialDetails{Fields: fields}, nil |
| 243 | +} |
0 commit comments