|
| 1 | +package dbt_cloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type AzureADApplication struct { |
| 12 | + ID *int64 `json:"id,omitempty"` |
| 13 | + AccountID int64 `json:"account_id,omitempty"` |
| 14 | + OrganizationName string `json:"organization_name"` |
| 15 | + ClientID *string `json:"client_id,omitempty"` |
| 16 | + ClientSecret *string `json:"client_secret,omitempty"` |
| 17 | + TenantID *string `json:"tenant_id,omitempty"` |
| 18 | + AzureServiceAuthenticationMethod string `json:"azure_service_authentication_method,omitempty"` |
| 19 | + OAuthRedirectURIDomain *string `json:"oauth_redirect_uri_domain,omitempty"` |
| 20 | + CreatedAt *string `json:"created_at,omitempty"` |
| 21 | + UpdatedAt *string `json:"updated_at,omitempty"` |
| 22 | +} |
| 23 | + |
| 24 | +type AzureADApplicationResponse struct { |
| 25 | + Data AzureADApplication `json:"data"` |
| 26 | + Status ResponseStatus `json:"status"` |
| 27 | +} |
| 28 | + |
| 29 | +type AzureADApplicationListResponse struct { |
| 30 | + Data []AzureADApplication `json:"data"` |
| 31 | + Status ResponseStatus `json:"status"` |
| 32 | +} |
| 33 | + |
| 34 | +// GetAzureADApplicationForAccount fetches the single Azure AD application for |
| 35 | +// this account via the list endpoint. Returns nil if none exists yet. |
| 36 | +func (c *Client) GetAzureADApplicationForAccount() (*AzureADApplication, error) { |
| 37 | + req, err := http.NewRequest( |
| 38 | + "GET", |
| 39 | + fmt.Sprintf( |
| 40 | + "%s/v3/accounts/%s/azure-ad-applications/", |
| 41 | + c.HostURL, |
| 42 | + strconv.FormatInt(c.AccountID, 10), |
| 43 | + ), |
| 44 | + nil, |
| 45 | + ) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + body, err := c.doRequestWithRetry(req) |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + listResp := AzureADApplicationListResponse{} |
| 56 | + if err = json.Unmarshal(body, &listResp); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + |
| 60 | + if len(listResp.Data) == 0 { |
| 61 | + return nil, nil |
| 62 | + } |
| 63 | + return &listResp.Data[0], nil |
| 64 | +} |
| 65 | + |
| 66 | +func (c *Client) GetAzureADApplication(applicationID int64) (*AzureADApplication, error) { |
| 67 | + req, err := http.NewRequest( |
| 68 | + "GET", |
| 69 | + fmt.Sprintf( |
| 70 | + "%s/v3/accounts/%s/azure-ad-applications/%d/", |
| 71 | + c.HostURL, |
| 72 | + strconv.FormatInt(c.AccountID, 10), |
| 73 | + applicationID, |
| 74 | + ), |
| 75 | + nil, |
| 76 | + ) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + body, err := c.doRequestWithRetry(req) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + |
| 86 | + resp := AzureADApplicationResponse{} |
| 87 | + if err = json.Unmarshal(body, &resp); err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + return &resp.Data, nil |
| 92 | +} |
| 93 | + |
| 94 | +func (c *Client) CreateAzureADApplication(app AzureADApplication) (*AzureADApplication, error) { |
| 95 | + payload, err := json.Marshal(app) |
| 96 | + if err != nil { |
| 97 | + return nil, err |
| 98 | + } |
| 99 | + |
| 100 | + req, err := http.NewRequest( |
| 101 | + "POST", |
| 102 | + fmt.Sprintf( |
| 103 | + "%s/v3/accounts/%s/azure-ad-applications/", |
| 104 | + c.HostURL, |
| 105 | + strconv.FormatInt(c.AccountID, 10), |
| 106 | + ), |
| 107 | + strings.NewReader(string(payload)), |
| 108 | + ) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + body, err := c.doRequestWithRetry(req) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + resp := AzureADApplicationResponse{} |
| 119 | + if err = json.Unmarshal(body, &resp); err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + return &resp.Data, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (c *Client) UpdateAzureADApplication(applicationID int64, app AzureADApplication) (*AzureADApplication, error) { |
| 127 | + // account_id is passed via the URL path; zero it out to avoid API rejection. |
| 128 | + app.AccountID = 0 |
| 129 | + |
| 130 | + payload, err := json.Marshal(app) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + |
| 135 | + req, err := http.NewRequest( |
| 136 | + "POST", |
| 137 | + fmt.Sprintf( |
| 138 | + "%s/v3/accounts/%s/azure-ad-applications/%d/", |
| 139 | + c.HostURL, |
| 140 | + strconv.FormatInt(c.AccountID, 10), |
| 141 | + applicationID, |
| 142 | + ), |
| 143 | + strings.NewReader(string(payload)), |
| 144 | + ) |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + body, err := c.doRequestWithRetry(req) |
| 150 | + if err != nil { |
| 151 | + return nil, err |
| 152 | + } |
| 153 | + |
| 154 | + resp := AzureADApplicationResponse{} |
| 155 | + if err = json.Unmarshal(body, &resp); err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + return &resp.Data, nil |
| 160 | +} |
| 161 | + |
| 162 | +func (c *Client) DeleteAzureADApplication(applicationID int64) error { |
| 163 | + req, err := http.NewRequest( |
| 164 | + "DELETE", |
| 165 | + fmt.Sprintf( |
| 166 | + "%s/v3/accounts/%s/azure-ad-applications/%d/", |
| 167 | + c.HostURL, |
| 168 | + strconv.FormatInt(c.AccountID, 10), |
| 169 | + applicationID, |
| 170 | + ), |
| 171 | + nil, |
| 172 | + ) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + _, err = c.doRequestWithRetry(req) |
| 178 | + return err |
| 179 | +} |
0 commit comments