|
| 1 | +import pandas as pd |
| 2 | +from typing import Optional |
| 3 | +from sempy_labs._helper_functions import ( |
| 4 | + resolve_item_name_and_id, |
| 5 | + resolve_workspace_id, |
| 6 | + _base_api, |
| 7 | + _create_dataframe, |
| 8 | + delete_item, |
| 9 | + resolve_item_id, |
| 10 | + resolve_workspace_name_and_id, |
| 11 | +) |
| 12 | +from uuid import UUID |
| 13 | +from sempy._utils._log import log |
| 14 | +import sempy_labs._icons as icons |
| 15 | + |
| 16 | + |
| 17 | +@log |
| 18 | +def list_apache_airflow_job_files( |
| 19 | + apache_airflow_job: str | UUID, |
| 20 | + workspace: Optional[str | UUID] = None, |
| 21 | + root_path: Optional[str] = None, |
| 22 | +) -> pd.DataFrame: |
| 23 | + """ |
| 24 | + Shows a list of Apache Airflow job files from the specified Apache Airflow job. |
| 25 | +
|
| 26 | + This is a wrapper function for the following API: `Files - List Apache Airflow Job Files <https://learn.microsoft.com/rest/api/fabric/apacheairflowjob/files/list-apache-airflow-job-files>`_. |
| 27 | +
|
| 28 | + Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples). |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + apache_airflow_job : str | uuid.UUID |
| 33 | + The Apache Airflow job name or ID. |
| 34 | + workspace : str | uuid.UUID, default=None |
| 35 | + The Fabric workspace name or ID. |
| 36 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 37 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 38 | + root_path : str, default=None |
| 39 | + The folder path to list. If not provided, the root directory is used. |
| 40 | +
|
| 41 | + Returns |
| 42 | + ------- |
| 43 | + pandas.DataFrame |
| 44 | + A pandas dataframe showing a list of Apache Airflow job files from the specified Apache Airflow job. |
| 45 | + """ |
| 46 | + |
| 47 | + columns = { |
| 48 | + "File Path": "string", |
| 49 | + "Size In Bytes": "int", |
| 50 | + } |
| 51 | + |
| 52 | + df = _create_dataframe(columns=columns) |
| 53 | + |
| 54 | + workspace_id = resolve_workspace_id(workspace) |
| 55 | + |
| 56 | + apache_airflow_job_id = resolve_item_id( |
| 57 | + item=apache_airflow_job, |
| 58 | + type="ApacheAirflowJob", |
| 59 | + workspace=workspace_id, |
| 60 | + ) |
| 61 | + |
| 62 | + url = f"/v1/workspaces/{workspace_id}/ApacheAirflowJobs/{apache_airflow_job_id}/files?beta=True" |
| 63 | + if root_path: |
| 64 | + url += f"&rootPath={root_path}" |
| 65 | + responses = _base_api( |
| 66 | + request=url, |
| 67 | + client="fabric_sp", |
| 68 | + uses_pagination=True, |
| 69 | + ) |
| 70 | + |
| 71 | + rows = [] |
| 72 | + for r in responses: |
| 73 | + for v in r.get("value", []): |
| 74 | + rows.append( |
| 75 | + { |
| 76 | + "File Path": v.get("filePath"), |
| 77 | + "Size In Bytes": v.get("sizeInBytes"), |
| 78 | + } |
| 79 | + ) |
| 80 | + |
| 81 | + if rows: |
| 82 | + df = pd.DataFrame(rows, columns=list(columns.keys())) |
| 83 | + |
| 84 | + return df |
| 85 | + |
| 86 | + |
| 87 | +@log |
| 88 | +def delete_apache_airflow_job_file( |
| 89 | + apache_airflow_job: str | UUID, |
| 90 | + file_path: str, |
| 91 | + workspace: Optional[str | UUID] = None, |
| 92 | +): |
| 93 | + """ |
| 94 | + Deletes a file from a Fabric Apache Airflow Job. |
| 95 | +
|
| 96 | + This is a wrapper function for the following API: `Files - Delete Apache Airflow Job File <https://learn.microsoft.com/rest/api/fabric/apacheairflowjob/files/delete-apache-airflow-job-file>`_. |
| 97 | +
|
| 98 | + Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples). |
| 99 | +
|
| 100 | + Parameters |
| 101 | + ---------- |
| 102 | + apache_airflow_job : str | uuid.UUID |
| 103 | + The Apache Airflow job name or ID. |
| 104 | + file_path : str |
| 105 | + The file path relative to the Apache Airflow job root. It must begin with either 'dags/' or 'plugins/' (for example, dags/example_dag.py). |
| 106 | + workspace : str | uuid.UUID, default=None |
| 107 | + The Fabric workspace name or ID. |
| 108 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 109 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 110 | + """ |
| 111 | + |
| 112 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 113 | + (item_name, item_id) = resolve_item_name_and_id( |
| 114 | + item=apache_airflow_job, |
| 115 | + type="ApacheAirflowJob", |
| 116 | + workspace=workspace_id, |
| 117 | + ) |
| 118 | + |
| 119 | + _base_api( |
| 120 | + request=f"/v1/workspaces/{workspace_id}/ApacheAirflowJobs/{item_id}/files/{file_path}?beta=True", |
| 121 | + client="fabric_sp", |
| 122 | + method="delete", |
| 123 | + ) |
| 124 | + |
| 125 | + print( |
| 126 | + f"{icons.green_dot} The '{file_path}' file has been deleted from the '{item_name}' Apache Airflow Job within the '{workspace_name}' workspace." |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +@log |
| 131 | +def create_or_update_apache_airflow_job_file( |
| 132 | + apache_airflow_job: str | UUID, |
| 133 | + file_path: str, |
| 134 | + file_content: bytes, |
| 135 | + workspace: Optional[str | UUID] = None, |
| 136 | +): |
| 137 | + """ |
| 138 | + Creates or updates an Apache Airflow job file. |
| 139 | +
|
| 140 | + This is a wrapper function for the following API: `Files - Create Or Update Apache Airflow Job File <https://learn.microsoft.com/rest/api/fabric/apacheairflowjob/files/create-or-update-apache-airflow-job-file>`_. |
| 141 | +
|
| 142 | + Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples). |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + apache_airflow_job : str | uuid.UUID |
| 147 | + The Apache Airflow job name or ID. |
| 148 | + file_path : str |
| 149 | + The file path relative to the Apache Airflow job root. It must begin with either 'dags/' or 'plugins/' (for example, dags/example_dag.py). |
| 150 | + file_content : bytes |
| 151 | + The file content. Text files must be UTF-8 encoded. |
| 152 | + workspace : str | uuid.UUID, default=None |
| 153 | + The Fabric workspace name or ID. |
| 154 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 155 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 156 | + """ |
| 157 | + |
| 158 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 159 | + (item_name, item_id) = resolve_item_name_and_id( |
| 160 | + item=apache_airflow_job, |
| 161 | + type="ApacheAirflowJob", |
| 162 | + workspace=workspace_id, |
| 163 | + ) |
| 164 | + |
| 165 | + _base_api( |
| 166 | + request=f"/v1/workspaces/{workspace_id}/ApacheAirflowJobs/{item_id}/files/{file_path}?beta=True", |
| 167 | + client="fabric_sp", |
| 168 | + method="put", |
| 169 | + payload=file_content, |
| 170 | + ) |
| 171 | + |
| 172 | + print( |
| 173 | + f"{icons.green_dot} The '{file_path}' file has been created/updated in the '{item_name}' Apache Airflow Job within the '{workspace_name}' workspace." |
| 174 | + ) |
0 commit comments