-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy path__init__.py
More file actions
440 lines (349 loc) · 15.9 KB
/
__init__.py
File metadata and controls
440 lines (349 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import os
import re
import requests
from beartype.typing import Any, Dict, List, Optional
from importlib_metadata import version
from loguru import logger
from urllib3.exceptions import InsecureRequestWarning
from dbt_jobs_as_code.schemas.custom_environment_variable import (
CustomEnvironmentVariable,
CustomEnvironmentVariablePayload,
)
from dbt_jobs_as_code.schemas.job import JobDefinition, JobMissingFields
if os.getenv("DBT_JOB_ID", "") == "":
VERSION = f"v{version('dbt-jobs-as-code')}"
else:
VERSION = "dev"
class DBTCloudException(Exception):
pass
class DBTCloudParamsException(Exception):
pass
class DBTCloud:
"""A minimalistic API client for fetching dbt Cloud data."""
def __init__(
self,
account_id: int,
api_key: Optional[str],
base_url: str = "https://cloud.getdbt.com",
disable_ssl_verification: bool = False,
use_desc_for_id: bool = False,
) -> None:
self.account_id = account_id
self._api_key = api_key
self._use_desc_for_id = use_desc_for_id
self._environment_variable_cache: Dict[
int, Dict[str, CustomEnvironmentVariablePayload]
] = {}
self.base_url = base_url.rstrip("/")
self._headers = {
"Authorization": f"Bearer {self._api_key}",
"Content-Type": "application/json",
"User-Agent": f"dbt-jobs-as-code/{VERSION}",
}
self._verify = not disable_ssl_verification
if not self._verify:
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning) # type: ignore
logger.warning(
"SSL verification is disabled. This is not recommended unless you absolutely need this config."
)
self._session = requests.Session()
def _clear_env_var_cache(self, job_definition_id: Optional[int]) -> None:
"""Clear out any cached environment variables for a given job."""
if job_definition_id in self._environment_variable_cache:
del self._environment_variable_cache[job_definition_id]
@staticmethod
def _pre_process_job_data(data: dict) -> dict:
"""Move [[identifier]] from description back to name for internal processing."""
description = data.get("description", "")
if not description:
return data
identifier_info = JobDefinition._extract_identifier_from_description(description)
if not identifier_info.identifier:
return data
data = dict(data) # shallow copy to avoid mutating caller's dict
raw_id = identifier_info.raw_identifier
# Strip " [[raw_id]]" or "[[raw_id]]" from description (first occurrence only)
data["description"] = re.sub(
r" ?\[\[" + re.escape(raw_id) + r"\]\]",
"",
description,
count=1,
)
# Move identifier to name (where JobDefinition.__init__ expects it)
data["name"] = f"{data['name']} [[{raw_id}]]"
return data
def _check_for_creds(self):
"""Confirm the presence of credentials"""
if not self._api_key:
raise DBTCloudParamsException("An API key is required to get dbt Cloud jobs.")
if not self.account_id:
raise DBTCloudParamsException("An account_id is required to get dbt Cloud jobs.")
def build_mapping_job_identifier_job_id(
self, cloud_jobs: Optional[List[JobDefinition]] = None
):
if cloud_jobs is None:
# TODO, we should filter things here at least if we call it often
cloud_jobs = self.get_jobs()
mapping_job_identifier_job_id = {}
for job in cloud_jobs:
if job.identifier is not None:
mapping_job_identifier_job_id[job.identifier] = job.id
return mapping_job_identifier_job_id
def update_job(self, job: JobDefinition) -> JobDefinition:
"""Update an existing dbt Cloud job using a new JobDefinition"""
logger.debug("Updating {job_name}. {job}", job_name=job.name, job=job)
response = self._session.post( # Yes, it's actually a POST. Ew.
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job.id}/",
headers=self._headers,
data=job.to_payload(use_desc_for_id=self._use_desc_for_id),
verify=self._verify,
)
if response.status_code >= 400:
logger.error(response.json())
raise DBTCloudException(f"Error updating job {job.name}")
else:
logger.success("Job updated successfully.")
raw_data = response.json()["data"]
if self._use_desc_for_id:
raw_data = DBTCloud._pre_process_job_data(raw_data)
return JobDefinition(**raw_data)
return JobDefinition(**raw_data, identifier=job.identifier)
def create_job(self, job: JobDefinition) -> Optional[JobDefinition]:
"""Create a dbt Cloud Job using a JobDefinition"""
logger.debug("Creating {job_name}. {job}", job_name=job.name, job=job)
response = self._session.post(
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/",
headers=self._headers,
data=job.to_payload(use_desc_for_id=self._use_desc_for_id),
verify=self._verify,
)
if response.status_code >= 400:
logger.error(response.json())
raise DBTCloudException(f"Error creating job {job.name}")
return None
else:
logger.success("Job created successfully.")
raw_data = response.json()["data"]
if self._use_desc_for_id:
raw_data = DBTCloud._pre_process_job_data(raw_data)
return JobDefinition(**raw_data)
return JobDefinition(**raw_data, identifier=job.identifier)
def delete_job(self, job: JobDefinition) -> None:
"""Delete a dbt Cloud job."""
logger.debug("Deleting {job_name}. {job}", job_name=job.name, job=job)
response = self._session.delete(
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job.id}/",
headers=self._headers,
verify=self._verify,
)
if response.status_code >= 400:
logger.error(response.json())
raise DBTCloudException(f"Error deleting job {job.name}")
else:
logger.success("Job deleted successfully.")
def get_job(self, job_id: int) -> JobDefinition:
"""Generate a Job based on a dbt Cloud job."""
self._check_for_creds()
response = self._session.get(
url=(f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job_id}/"),
headers=self._headers,
verify=self._verify,
)
if response.status_code > 200:
logger.error(f"Issue getting the job {job_id}")
raise DBTCloudException(f"Error getting the job {job_id}")
raw_data = response.json()["data"]
if self._use_desc_for_id:
raw_data = DBTCloud._pre_process_job_data(raw_data)
return JobDefinition(**raw_data)
def get_job_missing_fields(self, job_id: int) -> Optional[JobMissingFields]:
"""Generate a Job based on a dbt Cloud job."""
self._check_for_creds()
response = self._session.get(
url=(f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/{job_id}/"),
headers=self._headers,
verify=self._verify,
)
if response.status_code > 200:
logger.error(f"Issue getting the job {job_id}")
raise DBTCloudException(f"Error getting the job {job_id}")
return JobMissingFields(**response.json()["data"])
def get_jobs(
self,
project_ids: Optional[List[int]] = None,
environment_ids: Optional[List[int]] = None,
) -> List[JobDefinition]:
"""Return a list of Jobs for all the dbt Cloud jobs in an environment."""
self._check_for_creds()
project_ids = project_ids or []
environment_ids = environment_ids or []
jobs: List[dict] = []
if len(environment_ids) > 1:
for env_id in environment_ids:
jobs.extend(self._fetch_jobs(project_ids, env_id))
elif len(environment_ids) == 1:
jobs = self._fetch_jobs(project_ids, environment_ids[0])
else:
jobs = self._fetch_jobs(project_ids, None)
if self._use_desc_for_id:
jobs = [DBTCloud._pre_process_job_data(job) for job in jobs]
return [JobDefinition(**job) for job in jobs]
def _fetch_jobs(self, project_ids: List[int], environment_id: Optional[int]) -> List[dict]:
offset = 0
jobs: List[dict] = []
while True:
parameters = self._build_parameters(project_ids, environment_id, offset)
job_data = self._make_request(parameters)
if not job_data:
return []
jobs.extend(job_data["data"])
if (
job_data["extra"]["filters"]["limit"] + job_data["extra"]["filters"]["offset"]
>= job_data["extra"]["pagination"]["total_count"]
):
break
offset += job_data["extra"]["filters"]["limit"]
return jobs
def _build_parameters(
self, project_ids: List[int], environment_id: Optional[int], offset
) -> dict[str, Any]:
parameters = {"offset": offset}
if len(project_ids) == 1:
parameters["project_id"] = project_ids[0]
elif len(project_ids) > 1:
project_id_str = [str(i) for i in project_ids]
parameters["project_id__in"] = f"[{','.join(project_id_str)}]"
if environment_id is not None:
parameters["environment_id"] = environment_id
logger.debug(f"Request parameters {parameters}")
return parameters
def _make_request(self, parameters: dict[str, Any]):
response = self._session.get(
url=f"{self.base_url}/api/v2/accounts/{self.account_id}/jobs/",
params=parameters,
headers=self._headers,
verify=self._verify,
)
if response.status_code >= 400:
error_data = response.json()
logger.error(error_data)
if response.status_code == 401:
raise DBTCloudException(
"401 Unauthorized -- Check your API key and dbt Cloud parameters"
)
raise DBTCloudException(f"Error fetching jobs (HTTP {response.status_code})")
return response.json()
def get_env_vars(
self, project_id: int, job_id: int
) -> Dict[str, CustomEnvironmentVariablePayload]:
"""Get the existing env vars job overwrite in dbt Cloud."""
if job_id in self._environment_variable_cache:
return self._environment_variable_cache[job_id]
self._check_for_creds()
response = self._session.get(
url=(
f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{project_id}/environment-variables/job/?job_definition_id={job_id}"
),
headers=self._headers,
verify=self._verify,
)
variables = {
name: CustomEnvironmentVariablePayload(
id=variable_data.get("job", {}).get("id"),
name=name,
value=variable_data.get("job", {}).get("value"),
job_definition_id=job_id,
project_id=project_id,
account_id=self.account_id,
)
for name, variable_data in response.json()["data"].items()
}
self._environment_variable_cache[job_id] = variables
return variables
def create_env_var(
self, env_var: CustomEnvironmentVariablePayload
) -> CustomEnvironmentVariablePayload:
"""Create a new Custom Environment Variable in dbt Cloud."""
response = self._session.post(
f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{env_var.project_id}/environment-variables/",
headers=self._headers,
data=env_var.model_dump_json(),
verify=self._verify,
)
logger.debug(response.json())
if response.status_code >= 400:
logger.error(response.json())
raise DBTCloudException(f"Error creating the env var {env_var.name}")
# If the new environment variables has a job_definition_id, then clear the EnvVar cache.
self._clear_env_var_cache(job_definition_id=env_var.job_definition_id)
return response.json()["data"]
def update_env_var(
self,
custom_env_var: CustomEnvironmentVariable,
project_id: int,
job_id: Optional[int],
env_var_id: Optional[int],
yml_job_identifier: Optional[str] = None,
) -> Optional[CustomEnvironmentVariablePayload]:
"""Update env vars job overwrite in dbt Cloud."""
self._check_for_creds()
# handle the case where the job was not created when we queued the function call
if yml_job_identifier and not job_id:
# TODO - we shouldn't have to call the API so many times
mapping_job_identifier_job_id = self.build_mapping_job_identifier_job_id()
job_id = mapping_job_identifier_job_id[yml_job_identifier]
custom_env_var.job_definition_id = job_id
# the endpoint is different for updating an overwrite vs creating one
if env_var_id:
url = f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{project_id}/environment-variables/{env_var_id}/"
else:
url = f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{project_id}/environment-variables/"
payload = CustomEnvironmentVariablePayload(
account_id=self.account_id,
project_id=project_id,
id=env_var_id,
**custom_env_var.model_dump(),
)
response = self._session.post(
url=url, headers=self._headers, data=payload.model_dump_json(), verify=self._verify
)
if response.status_code >= 400:
logger.error(response.json())
raise DBTCloudException(f"Error updating the env var {custom_env_var.name}")
self._clear_env_var_cache(job_definition_id=payload.job_definition_id)
logger.success(f"Updated the env_var {custom_env_var.name} for job {job_id}")
return CustomEnvironmentVariablePayload(**(response.json()["data"]))
def delete_env_var(self, project_id: int, env_var_id: int) -> None:
"""Delete env_var job overwrite in dbt Cloud."""
logger.debug(f"Deleting env var id {env_var_id}")
response = self._session.delete(
url=f"{self.base_url}/api/v3/accounts/{self.account_id}/projects/{project_id}/environment-variables/{env_var_id}/",
headers=self._headers,
verify=self._verify,
)
if response.status_code >= 400:
logger.error(response.json())
raise DBTCloudException(f"Error deleting the env var {env_var_id}")
logger.success("Env Var Job Overwrite deleted successfully.")
def _fetch_environment(self, url) -> List[dict]:
response = self._session.get(
url=url,
headers=self._headers,
verify=self._verify,
)
if response.status_code >= 400:
logger.error(response.json())
logger.error(f"Does the Account ID {self.account_id} exist?")
return []
return response.json()["data"]
def get_environments(self, project_ids: List[int]) -> List[dict]:
"""Return a list of Environments for all the dbt Cloud jobs in an account"""
self._check_for_creds()
all_envs = []
for project_id in project_ids:
url = (
f"{self.base_url}/api/v3/accounts/"
f"{self.account_id}/environments/?project_id={project_id}"
)
all_envs.extend(self._fetch_environment(url))
return all_envs