Skip to content

Commit 132c1a4

Browse files
committed
snowflake_database
1 parent de586bd commit 132c1a4

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/sempy_labs/_utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,6 @@
7373
"Reflex": "reflexes",
7474
"GraphModel": "GraphModels",
7575
"GraphQuerySet": "GraphQuerySets",
76+
"OperationsAgent": "OperationsAgents",
77+
"SnowflakeDatabase": "snowflakeDatabases",
7678
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from ._items import (
2+
list_snowflake_databases,
3+
delete_snowflake_database,
4+
)
5+
6+
7+
__all__ = [
8+
"list_snowflake_databases",
9+
"delete_snowflake_database",
10+
]
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from sempy_labs._helper_functions import (
2+
_base_api,
3+
_create_dataframe,
4+
_update_dataframe_datatypes,
5+
delete_item,
6+
resolve_workspace_id,
7+
)
8+
import pandas as pd
9+
from typing import Optional
10+
from uuid import UUID
11+
from sempy._utils._log import log
12+
13+
14+
@log
15+
def list_snowflake_databases(workspace: Optional[str | UUID] = None) -> pd.DataFrame:
16+
"""
17+
Shows the snowflake databases within a workspace.
18+
19+
This is a wrapper function for the following API: `Items - List Snowflake Databases <https://learn.microsoft.com/rest/api/fabric/snowflakedatabase/items/list-snowflake-databases>`_.
20+
21+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
22+
23+
Parameters
24+
----------
25+
workspace : str | uuid.UUID, default=None
26+
The Fabric workspace name or ID.
27+
Defaults to None which resolves to the workspace of the attached lakehouse
28+
or if no lakehouse attached, resolves to the workspace of the notebook.
29+
30+
Returns
31+
-------
32+
pandas.DataFrame
33+
A pandas dataframe showing the snowflake databases within a workspace.
34+
"""
35+
36+
columns = {
37+
"Snowflake Database Display Name": "string",
38+
"Snowflake Database Id": "string",
39+
"Description": "string",
40+
"Snowflake Database Name": "string",
41+
"OneLake Tables Path": "string",
42+
"Snowflake Volume Path": "string",
43+
"SQL Endpoint Connection String": "string",
44+
"SQL Endpoint Id": "string",
45+
"SQL Endpoint Provisioning Status": "string",
46+
"Default Schema": "string",
47+
}
48+
df = _create_dataframe(columns=columns)
49+
50+
workspace_id = resolve_workspace_id(workspace)
51+
52+
responses = _base_api(
53+
request=f"/v1/workspaces/{workspace_id}/snowflakeDatabases",
54+
uses_pagination=True,
55+
client="fabric_sp",
56+
)
57+
58+
rows = []
59+
for r in responses:
60+
for v in r.get("value", []):
61+
prop = v.get("properties", {})
62+
63+
rows.append(
64+
{
65+
"Snowflake Database Display Name": v.get("displayName"),
66+
"Snowflake Database Id": v.get("id"),
67+
"Description": v.get("description"),
68+
"Snowflake Database Name": prop.get("snowflakeDatabaseName"),
69+
"OneLake Tables Path": prop.get("onelakeTablesPath"),
70+
"Snowflake Volume Path": prop.get("snowflakeVolumePath"),
71+
"SQL Endpoint Connection String": prop.get(
72+
"sqlEndpointProperties", {}
73+
).get("connectionString"),
74+
"SQL Endpoint Id": prop.get("sqlEndpointProperties", {}).get("id"),
75+
"SQL Endpoint Provisioning Status": prop.get(
76+
"sqlEndpointProperties", {}
77+
).get("provisioningStatus"),
78+
"Default Schema": prop.get("defaultSchema"),
79+
}
80+
)
81+
82+
if rows:
83+
df = pd.DataFrame(rows, columns=list(columns.keys()))
84+
_update_dataframe_datatypes(dataframe=df, column_map=columns)
85+
86+
return df
87+
88+
89+
@log
90+
def delete_snowflake_database(
91+
snowflake_database: str | UUID, workspace: Optional[str | UUID] = None
92+
):
93+
"""
94+
Deletes a Fabric snowflake database.
95+
96+
This is a wrapper function for the following API: `Items - Delete Snowflake Database <https://learn.microsoft.com/rest/api/fabric/snowflakedatabase/items/delete-snowflake-database>`_.
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+
101+
delete_item(
102+
item=snowflake_database,
103+
type="SnowflakeDatabase",
104+
workspace=workspace,
105+
)

0 commit comments

Comments
 (0)