|
1 | 1 | import os |
2 | | -from ruamel.yaml import YAML |
3 | 2 | import sys |
4 | 3 |
|
5 | 4 | from loguru import logger |
@@ -87,7 +86,6 @@ def build_change_set(config): |
87 | 86 |
|
88 | 87 | # Replicate the env vars from the YML to dbt Cloud |
89 | 88 | for job in defined_jobs.values(): |
90 | | - |
91 | 89 | if job.identifier in mapping_job_identifier_job_id: # the job already exists |
92 | 90 | job_id = mapping_job_identifier_job_id[job.identifier] |
93 | 91 | all_env_vars_for_job = dbt_cloud.get_env_vars(project_id=job.project_id, job_id=job_id) |
@@ -130,7 +128,6 @@ def build_change_set(config): |
130 | 128 |
|
131 | 129 | # Delete the env vars from dbt Cloud that are not in the yml |
132 | 130 | for job in defined_jobs.values(): |
133 | | - |
134 | 131 | # we only delete env var overwrite if the job already exists |
135 | 132 | if job.identifier in mapping_job_identifier_job_id: |
136 | 133 | job_id = mapping_job_identifier_job_id[job.identifier] |
@@ -270,11 +267,7 @@ def validate(config, online): |
270 | 267 |
|
271 | 268 | # In case deferral jobs are mentioned, check that they exist |
272 | 269 | deferral_envs = set( |
273 | | - [ |
274 | | - job.deferring_environment_id |
275 | | - for job in defined_jobs |
276 | | - if job.deferring_environment_id |
277 | | - ] |
| 270 | + [job.deferring_environment_id for job in defined_jobs if job.deferring_environment_id] |
278 | 271 | ) |
279 | 272 | if deferral_envs: |
280 | 273 | logger.info(f"Checking that Deferring Env IDs are valid") |
@@ -342,5 +335,117 @@ def import_jobs(config, account_id, job_id): |
342 | 335 | export_jobs_yml(cloud_jobs) |
343 | 336 |
|
344 | 337 |
|
| 338 | +@cli.command() |
| 339 | +@click.option("--config", type=click.File("r"), help="The path to your YML jobs config file.") |
| 340 | +@click.option("--account-id", type=int, help="The ID of your dbt Cloud account.") |
| 341 | +@click.option("--dry-run", is_flag=True, help="In dry run mode we don't update dbt Cloud.") |
| 342 | +@click.option( |
| 343 | + "--identifier", |
| 344 | + "-i", |
| 345 | + type=str, |
| 346 | + multiple=True, |
| 347 | + help="[Optional] The identifiers we want to unlink. If not provided, all jobs are unlinked.", |
| 348 | +) |
| 349 | +def unlink(config, account_id, dry_run, identifier): |
| 350 | + """ |
| 351 | + Unlink the YML file to dbt Cloud. |
| 352 | + All relevant jobs get the part [[...]] removed from their name |
| 353 | + """ |
| 354 | + |
| 355 | + # we get the account id either from a parameter (e.g if the config file doesn't exist) or from the config file |
| 356 | + if account_id: |
| 357 | + cloud_account_id = account_id |
| 358 | + elif config: |
| 359 | + defined_jobs = load_job_configuration(config).jobs.values() |
| 360 | + cloud_account_id = list(defined_jobs)[0].account_id |
| 361 | + else: |
| 362 | + raise click.BadParameter("Either --config or --account-id must be provided") |
| 363 | + |
| 364 | + # we get the account id from the config file |
| 365 | + defined_jobs = load_job_configuration(config).jobs.values() |
| 366 | + cloud_account_id = list(defined_jobs)[0].account_id |
| 367 | + |
| 368 | + dbt_cloud = DBTCloud( |
| 369 | + account_id=cloud_account_id, |
| 370 | + api_key=os.environ.get("DBT_API_KEY"), |
| 371 | + base_url=os.environ.get("DBT_BASE_URL", "https://cloud.getdbt.com"), |
| 372 | + ) |
| 373 | + cloud_jobs = dbt_cloud.get_jobs() |
| 374 | + selected_jobs = [job for job in cloud_jobs if job.identifier is not None] |
| 375 | + logger.info(f"Getting the jobs definition from dbt Cloud") |
| 376 | + |
| 377 | + if identifier: |
| 378 | + selected_jobs = [job for job in selected_jobs if job.identifier in identifier] |
| 379 | + |
| 380 | + for cloud_job in selected_jobs: |
| 381 | + current_identifier = cloud_job.identifier |
| 382 | + # by removing the identifier, we unlink the job from the YML file |
| 383 | + cloud_job.identifier = None |
| 384 | + if dry_run: |
| 385 | + logger.info( |
| 386 | + f"Would unlink/rename the job {cloud_job.id}:{cloud_job.name} [[{current_identifier}]]" |
| 387 | + ) |
| 388 | + else: |
| 389 | + logger.info( |
| 390 | + f"Unlinking/Renaming the job {cloud_job.id}:{cloud_job.name} [[{current_identifier}]]" |
| 391 | + ) |
| 392 | + dbt_cloud.update_job(job=cloud_job) |
| 393 | + |
| 394 | + if len(selected_jobs) == 0: |
| 395 | + logger.info(f"No jobs to unlink") |
| 396 | + elif not dry_run: |
| 397 | + logger.success(f"Updated all jobs!") |
| 398 | + |
| 399 | + |
| 400 | +@cli.command() |
| 401 | +@click.option("--config", type=click.File("r"), help="The path to your YML jobs config file.") |
| 402 | +@click.option("--account-id", type=int, help="The ID of your dbt Cloud account.") |
| 403 | +@click.option( |
| 404 | + "--job-id", |
| 405 | + "-j", |
| 406 | + type=int, |
| 407 | + multiple=True, |
| 408 | + help="[Optional] The ID of the job to deactivate.", |
| 409 | +) |
| 410 | +def deactivate_jobs(config, account_id, job_id): |
| 411 | + """ |
| 412 | + Deactivate jobs triggers in dbt Cloud (schedule and CI/CI triggers) |
| 413 | + """ |
| 414 | + |
| 415 | + # we get the account id either from a parameter (e.g if the config file doesn't exist) or from the config file |
| 416 | + if account_id: |
| 417 | + cloud_account_id = account_id |
| 418 | + elif config: |
| 419 | + defined_jobs = load_job_configuration(config).jobs.values() |
| 420 | + cloud_account_id = list(defined_jobs)[0].account_id |
| 421 | + else: |
| 422 | + raise click.BadParameter("Either --config or --account-id must be provided") |
| 423 | + |
| 424 | + dbt_cloud = DBTCloud( |
| 425 | + account_id=cloud_account_id, |
| 426 | + api_key=os.environ.get("DBT_API_KEY"), |
| 427 | + base_url=os.environ.get("DBT_BASE_URL", "https://cloud.getdbt.com"), |
| 428 | + ) |
| 429 | + cloud_jobs = dbt_cloud.get_jobs() |
| 430 | + |
| 431 | + selected_cloud_jobs = [job for job in cloud_jobs if job.id in job_id] |
| 432 | + |
| 433 | + for cloud_job in selected_cloud_jobs: |
| 434 | + if ( |
| 435 | + cloud_job.triggers.git_provider_webhook |
| 436 | + or cloud_job.triggers.github_webhook |
| 437 | + or cloud_job.triggers.schedule |
| 438 | + ): |
| 439 | + logger.info(f"Deactivating the job {cloud_job.id}:{cloud_job.name}") |
| 440 | + cloud_job.triggers.github_webhook = False |
| 441 | + cloud_job.triggers.git_provider_webhook = False |
| 442 | + cloud_job.triggers.schedule = False |
| 443 | + dbt_cloud.update_job(job=cloud_job) |
| 444 | + else: |
| 445 | + logger.info(f"The job {cloud_job.id}:{cloud_job.name} is already deactivated") |
| 446 | + |
| 447 | + logger.success(f"Deactivated all jobs!") |
| 448 | + |
| 449 | + |
345 | 450 | if __name__ == "__main__": |
346 | 451 | cli() |
0 commit comments