-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy path_data_pipelines.py
222 lines (174 loc) · 6.99 KB
/
_data_pipelines.py
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
import sempy.fabric as fabric
import pandas as pd
import sempy_labs._icons as icons
from typing import Optional
from sempy_labs._helper_functions import (
resolve_workspace_name_and_id,
lro,
pagination,
_decode_b64,
)
from sempy.fabric.exceptions import FabricHTTPException
def list_data_pipelines(workspace: Optional[str] = None) -> pd.DataFrame:
"""
Shows the data pipelines within a workspace.
This is a wrapper function for the following API: `Items - List Data Pipelines <https://learn.microsoft.com/rest/api/fabric/datapipeline/items/list-data-pipelines>`_.
Parameters
----------
workspace : str, default=None
The Fabric workspace name.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
Returns
-------
pandas.DataFrame
A pandas dataframe showing the data pipelines within a workspace.
"""
df = pd.DataFrame(columns=["Data Pipeline Name", "Data Pipeline ID", "Description"])
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
client = fabric.FabricRestClient()
response = client.get(f"/v1/workspaces/{workspace_id}/dataPipelines")
if response.status_code != 200:
raise FabricHTTPException(response)
responses = pagination(client, response)
for r in responses:
for v in r.get("value", []):
new_data = {
"Data Pipeline Name": v.get("displayName"),
"Data Pipeline ID": v.get("id"),
"Description": v.get("description"),
}
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
return df
def create_data_pipeline(
name: str, description: Optional[str] = None, workspace: Optional[str] = None
):
"""
Creates a Fabric data pipeline.
This is a wrapper function for the following API: `Items - Create Data Pipeline <https://learn.microsoft.com/rest/api/fabric/datapipeline/items/create-data-pipeline>`_.
Parameters
----------
name: str
Name of the data pipeline.
description : str, default=None
A description of the environment.
workspace : str, default=None
The Fabric workspace name.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
"""
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
request_body = {"displayName": name}
if description:
request_body["description"] = description
client = fabric.FabricRestClient()
response = client.post(
f"/v1/workspaces/{workspace_id}/dataPipelines", json=request_body
)
lro(client, response, status_codes=[201, 202])
print(
f"{icons.green_dot} The '{name}' data pipeline has been created within the '{workspace}' workspace."
)
def delete_data_pipeline(name: str, workspace: Optional[str] = None):
"""
Deletes a Fabric data pipeline.
This is a wrapper function for the following API: `Items - Delete Data Pipeline <https://learn.microsoft.com/rest/api/fabric/datapipeline/items/delete-data-pipeline>`_.
Parameters
----------
name: str
Name of the data pipeline.
workspace : str, default=None
The Fabric workspace name.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
"""
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
item_id = fabric.resolve_item_id(
item_name=name, type="DataPipeline", workspace=workspace
)
client = fabric.FabricRestClient()
response = client.delete(f"/v1/workspaces/{workspace_id}/dataPipelines/{item_id}")
if response.status_code != 200:
raise FabricHTTPException(response)
print(
f"{icons.green_dot} The '{name}' data pipeline within the '{workspace}' workspace has been deleted."
)
def get_data_pipeline_definition(
name: str, workspace: Optional[str] = None, decode: bool = True
) -> dict | pd.DataFrame:
"""
Obtains the definition of a data pipeline.
Parameters
----------
name : str
The name of the data pipeline.
workspace : str, default=None
The Fabric workspace name.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
decode : bool, default=True
decode : bool, default=True
If True, decodes the data pipeline definition file into .json format.
If False, obtains the data pipeline definition file a pandas DataFrame format.
Returns
-------
dict | pandas.DataFrame
A pandas dataframe showing the data pipelines within a workspace.
"""
workspace = fabric.resolve_workspace_name(workspace)
workspace_id = fabric.resolve_workspace_id(workspace)
item_id = fabric.resolve_item_id(
item_name=name, type="DataPipeline", workspace=workspace
)
client = fabric.FabricRestClient()
response = client.post(
f"/v1/workspaces/{workspace_id}/dataPipelines/{item_id}/getDefinition"
)
result = lro(client, response).json()
df = pd.json_normalize(result["definition"]["parts"])
if not decode:
return df
content = df[df["path"] == "pipeline-content.json"]
payload = content["payload"].iloc[0]
return _decode_b64(payload)
def update_data_pipeline_definition(
name: str, pipeline_content: dict, workspace: Optional[str] = None
):
"""
Updates an existing data pipeline with a new definition.
Parameters
----------
name : str
The name of the data pipeline.
pipeline_content : dict
The data pipeline content (not in Base64 format).
workspace : str, default=None
The name of the workspace.
Defaults to None which resolves to the workspace of the attached lakehouse
or if no lakehouse attached, resolves to the workspace of the notebook.
"""
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
client = fabric.FabricRestClient()
pipeline_payload = base64.b64encode(json.dumps(pipeline_content).encode('utf-8')).decode('utf-8')
pipeline_id = fabric.resolve_item_id(
item_name=name, type="DataPipeline", workspace=workspace
)
request_body = {
"definition": {
"parts": [
{
"path": "pipeline-content.json",
"payload": pipeline_payload,
"payloadType": "InlineBase64"
}
]
}
}
response = client.post(
f"v1/workspaces/{workspace_id}/items/{pipeline_id}/updateDefinition",
json=request_body,
)
lro(client, response, return_status_code=True)
print(
f"{icons.green_dot} The '{name}' pipeline was updated within the '{workspace}' workspace."
)