Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement API endpoint for DAG deletion #17980

Merged
merged 5 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions airflow/api_connexion/endpoints/dag_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@
from marshmallow import ValidationError

from airflow import DAG
from airflow._vendor.connexion import NoContent
from airflow.api_connexion import security
from airflow.api_connexion.exceptions import BadRequest, NotFound
from airflow.api_connexion.exceptions import AlreadyExists, BadRequest, NotFound
from airflow.api_connexion.parameters import check_limit, format_parameters
from airflow.api_connexion.schemas.dag_schema import (
DAGCollection,
dag_detail_schema,
dag_schema,
dags_collection_schema,
)
from airflow.exceptions import SerializedDagNotFound
from airflow.exceptions import AirflowException, DagNotFound, SerializedDagNotFound
from airflow.models.dag import DagModel
from airflow.security import permissions
from airflow.settings import Session
from airflow.utils.session import provide_session


Expand Down Expand Up @@ -100,3 +102,22 @@ def patch_dag(session, dag_id, update_mask=None):
setattr(dag, 'is_paused', patch_body['is_paused'])
session.commit()
return dag_schema.dump(dag)


@security.requires_access([(permissions.ACTION_CAN_DELETE, permissions.RESOURCE_DAG)])
@provide_session
def delete_dag(dag_id: str, session: Session):
"""Delete the specific DAG."""
# TODO: This function is shared with the /delete endpoint used by the web
# UI, so we're reusing it to simplify maintenance. Refactor the function to
# another place when the experimental/legacy API is removed.
from airflow.api.common.experimental import delete_dag

try:
delete_dag.delete_dag(dag_id, session=session)
except DagNotFound:
raise NotFound(f"Dag with id: '{dag_id}' not found")
except AirflowException:
raise AlreadyExists(detail=f"Task instances of fag with id: '{dag_id}' are still running")
uranusjr marked this conversation as resolved.
Show resolved Hide resolved

uranusjr marked this conversation as resolved.
Show resolved Hide resolved
return NoContent, 204
26 changes: 24 additions & 2 deletions airflow/api_connexion/openapi/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ info:
## AlreadyExists

The request could not be completed due to a conflict with the current state of the target
resource, meaning that the resource already exists
resource, e.g. the resource it tries to create already exists.

## Unknown

Expand Down Expand Up @@ -478,6 +478,28 @@ paths:
'404':
$ref: '#/components/responses/NotFound'

delete:
summary: Delete a DAG
uranusjr marked this conversation as resolved.
Show resolved Hide resolved
description: >
Deletes all metadata related to the DAG, including finished DAG Runs and Tasks.
Logs are not deleted. This action cannot be undone.
x-openapi-router-controller: airflow.api_connexion.endpoints.dag_endpoint
operationId: delete_dag
tags: [DAG]
responses:
'204':
description: Success.
'400':
$ref: '#/components/responses/BadRequest'
'401':
$ref: '#/components/responses/Unauthenticated'
'403':
$ref: '#/components/responses/PermissionDenied'
'404':
$ref: '#/components/responses/NotFound'
'409':
$ref: '#/components/responses/AlreadyExists'

/dags/{dag_id}/clearTaskInstances:
parameters:
- $ref: '#/components/parameters/DAGID'
Expand Down Expand Up @@ -3522,7 +3544,7 @@ components:
$ref: '#/components/schemas/Error'
# 409
'AlreadyExists':
description: The resource that a client tried to create already exists.
description: An existing resource conflicts with the request.
content:
application/json:
schema:
Expand Down
5 changes: 5 additions & 0 deletions airflow/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# to be marked in an ERROR state
"""Exceptions used by Airflow"""
import datetime
import warnings
from typing import Any, Dict, List, NamedTuple, Optional

from airflow.utils.code_utils import prepare_code_snippet
Expand Down Expand Up @@ -139,6 +140,10 @@ class DagRunAlreadyExists(AirflowBadRequest):
class DagFileExists(AirflowBadRequest):
"""Raise when a DAG ID is still in DagBag i.e., DAG file is in DAG folder"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
warnings.warn("DagFileExists is deprecated and will be removed.", DeprecationWarning, stacklevel=2)


class DuplicateTaskIdFound(AirflowException):
"""Raise when a Task with duplicate task_id is defined in the same DAG"""
Expand Down
5 changes: 1 addition & 4 deletions airflow/www/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ def run(self):
def delete(self):
"""Deletes DAG."""
from airflow.api.common.experimental import delete_dag
from airflow.exceptions import DagFileExists, DagNotFound
from airflow.exceptions import DagNotFound

dag_id = request.values.get('dag_id')
origin = get_safe_url(request.values.get('origin'))
Expand All @@ -1566,9 +1566,6 @@ def delete(self):
except DagNotFound:
flash(f"DAG with id {dag_id} not found. Cannot delete", 'error')
return redirect(request.referrer)
except DagFileExists:
flash(f"Dag id {dag_id} is still in DagBag. Remove the DAG file first.", 'error')
return redirect(request.referrer)
except AirflowException:
flash(
f"Cannot delete DAG with id {dag_id} because some task instances of the DAG "
Expand Down