-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy path_kql_databases.py
153 lines (121 loc) · 5.02 KB
/
_kql_databases.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
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,
)
from sempy.fabric.exceptions import FabricHTTPException
from uuid import UUID
def list_kql_databases(workspace: Optional[str] = None) -> pd.DataFrame:
"""
Shows the KQL databases within a workspace.
This is a wrapper function for the following API: `Items - List KQL Databases <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/list-kql-databases>`_.
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 KQL databases within a workspace.
"""
df = pd.DataFrame(
columns=[
"KQL Database Name",
"KQL Database Id",
"Description",
"Parent Eventhouse Item Id",
"Query Service URI",
"Ingestion Service URI",
"Database Type",
]
)
(workspace, workspace_id) = resolve_workspace_name_and_id(workspace)
client = fabric.FabricRestClient()
response = client.get(f"/v1/workspaces/{workspace_id}/kqlDatabases")
if response.status_code != 200:
raise FabricHTTPException(response)
responses = pagination(client, response)
for r in responses:
for v in r.get("value", []):
prop = v.get("properties", {})
new_data = {
"KQL Database Name": v.get("displayName"),
"KQL Database Id": v.get("id"),
"Description": v.get("description"),
"Parent Eventhouse Item Id": prop.get("parentEventhouseItemId"),
"Query Service URI": prop.get("queryServiceUri"),
"Ingestion Service URI": prop.get("ingestionServiceUri"),
"Database Type": prop.get("databaseType"),
}
df = pd.concat([df, pd.DataFrame(new_data, index=[0])], ignore_index=True)
return df
def create_kql_database(
name: str, description: Optional[str] = None, workspace: Optional[str] = None
):
"""
Creates a KQL database.
This is a wrapper function for the following API: `Items - Create KQL Database <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/create-kql-database>`_.
Parameters
----------
name: str
Name of the KQL database.
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}/kqlDatabases", json=request_body
)
lro(client, response, status_codes=[201, 202])
print(
f"{icons.green_dot} The '{name}' KQL database has been created within the '{workspace}' workspace."
)
def delete_kql_database(name: str, workspace: Optional[str] = None):
"""
Deletes a KQL database.
This is a wrapper function for the following API: `Items - Delete KQL Database <https://learn.microsoft.com/rest/api/fabric/kqldatabase/items/delete-kql-database>`_.
Parameters
----------
name: str
Name of the KQL database.
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)
kql_database_id = fabric.resolve_item_id(
item_name=name, type="KQLDatabase", workspace=workspace
)
client = fabric.FabricRestClient()
response = client.delete(
f"/v1/workspaces/{workspace_id}/kqlDatabases/{kql_database_id}"
)
if response.status_code != 200:
raise FabricHTTPException(response)
print(
f"{icons.green_dot} The '{name}' KQL database within the '{workspace}' workspace has been deleted."
)
def _resolve_cluster_uri(workspace: Optional[str | UUID] = None) -> str:
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
dfK = list_kql_databases(workspace=workspace_id)
dfK_filt = dfK[dfK["KQL Database Name"] == "Monitoring KQL database"]
if len(dfK_filt) == 0:
raise ValueError(
f"{icons.red_dot} Workspace monitoring is not set up for the '{workspace_name}' workspace."
)
return dfK_filt["Query Service URI"].iloc[0]