Skip to content

Commit f8e5dc5

Browse files
committed
warehouse_snapshot
1 parent 8210e97 commit f8e5dc5

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from ._items import (
2+
list_warehouse_snapshots,
3+
delete_warehouse_snapshot,
4+
create_warehouse_snapshot,
5+
)
6+
7+
__all__ = [
8+
"list_warehouse_snapshots",
9+
"delete_warehouse_snapshot",
10+
"create_warehouse_snapshot",
11+
]
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
from sempy_labs._helper_functions import (
2+
resolve_workspace_id,
3+
_base_api,
4+
_create_dataframe,
5+
_update_dataframe_datatypes,
6+
resolve_item_id,
7+
delete_item,
8+
resolve_workspace_name_and_id,
9+
)
10+
import pandas as pd
11+
from typing import Optional
12+
from uuid import UUID
13+
from sempy._utils._log import log
14+
from datetime import datetime
15+
import sempy_labs._icons as icons
16+
17+
18+
@log
19+
def list_warehouse_snapshots(workspace: Optional[str | UUID] = None) -> pd.DataFrame:
20+
"""
21+
Lists all warehouse snapshots for a given warehouse.
22+
23+
This is a wrapper function for the following API: `Items - List Warehouse Snapshots <https://learn.microsoft.com/rest/api/fabric/warehousesnapshot/items/list-warehouse-snapshots>`_.
24+
25+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
26+
27+
Parameters
28+
----------
29+
workspace : str | uuid.UUID, default=None
30+
The Fabric workspace name or ID.
31+
Defaults to None which resolves to the workspace of the attached lakehouse
32+
or if no lakehouse attached, resolves to the workspace of the notebook.
33+
34+
Returns
35+
-------
36+
pandas.DataFrame
37+
A pandas DataFrame containing the list of warehouse snapshots within the workspace.
38+
"""
39+
40+
workspace_id = resolve_workspace_id(workspace)
41+
42+
columns = {
43+
"Warehouse Snapshot Id": "string",
44+
"Warehouse Snapshot Name": "string",
45+
"Description": "string",
46+
"Connection String": "string",
47+
"Parent Warehouse Id": "string",
48+
"Snapshot DateTime": "datetime",
49+
}
50+
51+
df = _create_dataframe(columns=columns)
52+
53+
responses = _base_api(
54+
request=f"/v1/workspaces/{workspace_id}/warehousesnapshots",
55+
client="fabric_sp",
56+
uses_pagination=True,
57+
)
58+
59+
rows = []
60+
for r in responses:
61+
for v in r.get("value", []):
62+
rows.append(
63+
{
64+
"Warehouse Snapshot Id": v.get("id"),
65+
"Warehouse Snapshot Name": v.get("displayName"),
66+
"Description": v.get("properties", {}).get("description"),
67+
"Connection String": v.get("properties", {}).get(
68+
"connectionString"
69+
),
70+
"Parent Warehouse Id": v.get("properties", {}).get(
71+
"parentWarehouseId"
72+
),
73+
"Snapshot DateTime": v.get("properties", {}).get(
74+
"snapshotDateTime"
75+
),
76+
}
77+
)
78+
79+
if rows:
80+
df = pd.DataFrame(rows, columns=list(columns.keys()))
81+
_update_dataframe_datatypes(dataframe=df, column_map=columns)
82+
83+
return df
84+
85+
86+
@log
87+
def delete_warehouse_snapshot(
88+
warehouse_snapshot: str | UUID,
89+
workspace: Optional[str | UUID] = None,
90+
):
91+
"""
92+
Deletes a warehouse snapshot in the Fabric workspace.
93+
94+
This is a wrapper function for the following API: `Items - Delete Warehouse Snapshot <https://learn.microsoft.com/rest/api/fabric/warehousesnapshot/items/delete-warehouse-snapshot>`_.
95+
96+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
97+
"""
98+
delete_item(item=warehouse_snapshot, type="WarehouseSnapshot", workspace=workspace)
99+
100+
101+
@log
102+
def create_warehouse_snapshot(
103+
name: str,
104+
parent_warehouse: str | UUID,
105+
parent_warehouse_workspace: Optional[str | UUID] = None,
106+
warehouse_snapshot_workspace: Optional[str | UUID] = None,
107+
description: Optional[str] = None,
108+
snapshot_datetime: Optional[datetime] = None,
109+
):
110+
"""
111+
Creates a Warehouse snapshot in the specified workspace.
112+
113+
This is a wrapper function for the following API: `Items - Create Warehouse Snapshot <https://learn.microsoft.com/rest/api/fabric/warehousesnapshot/items/create-warehouse-snapshot>`_.
114+
115+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
116+
117+
Parameters
118+
----------
119+
name : str
120+
The name of the warehouse snapshot to create.
121+
parent_warehouse : str | uuid.UUID
122+
The name or ID of the parent warehouse to snapshot.
123+
parent_warehouse_workspace : str | uuid.UUID, default=None
124+
The workspace name or ID where the parent warehouse is located.
125+
Defaults to None which resolves to the workspace of the attached lakehouse
126+
or if no lakehouse attached, resolves to the workspace of the notebook.
127+
warehouse_snapshot_workspace : str | uuid.UUID, default=None
128+
The workspace name or ID where the warehouse snapshot will be created.
129+
Defaults to None which resolves to the workspace of the attached lakehouse
130+
or if no lakehouse attached, resolves to the workspace of the notebook.
131+
description : str, default=None
132+
A description for the warehouse snapshot.
133+
snapshot_datetime : datetime, default=None
134+
The datetime for the snapshot. If not provided, the current datetime will be used.
135+
Example: "2024-10-15T13:00:00Z"
136+
"""
137+
parent_warehouse_workspace_id = resolve_workspace_id(parent_warehouse_workspace)
138+
(warehouse_snapshot_workspace_name, warehouse_snapshot_workspace_id) = (
139+
resolve_workspace_name_and_id(warehouse_snapshot_workspace)
140+
)
141+
parent_warehouse_id = resolve_item_id(
142+
item=parent_warehouse, type="Warehouse", workspace=parent_warehouse_workspace_id
143+
)
144+
145+
payload = {
146+
"displayName": name,
147+
"creationPayload": {
148+
"parentWarehouseId": parent_warehouse_id,
149+
},
150+
}
151+
if description:
152+
payload["description"] = description
153+
if snapshot_datetime:
154+
payload["creationPayload"]["snapshotDateTime"] = snapshot_datetime
155+
156+
_base_api(
157+
request=f"/v1/workspaces/{warehouse_snapshot_workspace_id}/warehousesnapshots",
158+
client="fabric_sp",
159+
method="post",
160+
payload=payload,
161+
status_codes=[201, 202],
162+
lro_return_status_code=True,
163+
)
164+
165+
print(
166+
f"{icons.green_dot} The warehouse snapshot '{name}' has been created in the workspace '{warehouse_snapshot_workspace_name}'."
167+
)

0 commit comments

Comments
 (0)