Skip to content

Commit e7214e0

Browse files
committed
apply and unapply tags
1 parent a0a7dc9 commit e7214e0

File tree

2 files changed

+150
-1
lines changed

2 files changed

+150
-1
lines changed

src/sempy_labs/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
)
2323
from sempy_labs._tags import (
2424
list_tags,
25+
apply_tags,
26+
unapply_tags,
2527
)
2628
from sempy_labs._semantic_models import (
2729
get_semantic_model_refresh_schedule,
@@ -579,4 +581,6 @@
579581
"list_variable_libraries",
580582
"delete_variable_library",
581583
"create_vpax",
584+
"apply_tags",
585+
"unapply_tags",
582586
]

src/sempy_labs/_tags.py

Lines changed: 146 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22
_base_api,
33
_create_dataframe,
44
_update_dataframe_datatypes,
5+
resolve_item_name_and_id,
6+
resolve_workspace_name_and_id,
7+
_is_valid_uuid,
58
)
69
import pandas as pd
10+
from typing import Optional, List
11+
from uuid import UUID
12+
import sempy_labs._icons as icons
713

814

915
def list_tags() -> pd.DataFrame:
1016
"""
1117
Shows a list of all the tenant's tags.
1218
13-
This is a wrapper function for the following API: `Items - List Tags <https://learn.microsoft.com/rest/api/fabric/core/tags/list-tags>`_.
19+
This is a wrapper function for the following API: `Tags - List Tags <https://learn.microsoft.com/rest/api/fabric/core/tags/list-tags>`_.
1420
1521
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
1622
@@ -47,3 +53,142 @@ def list_tags() -> pd.DataFrame:
4753
_update_dataframe_datatypes(dataframe=df, column_map=columns)
4854

4955
return df
56+
57+
58+
def resolve_tags(tags: str | List[str]) -> List[str]:
59+
"""
60+
Resolves the tags to a list of strings.
61+
62+
Parameters
63+
----------
64+
tags : str | List[str]
65+
The tags to resolve.
66+
67+
Returns
68+
-------
69+
List[str]
70+
A list of resolved tags.
71+
"""
72+
73+
if isinstance(tags, str):
74+
tags = [tags]
75+
76+
if all(_is_valid_uuid(tag) for tag in tags):
77+
return tags
78+
79+
df = list_tags()
80+
81+
tag_list = []
82+
for tag in tags:
83+
if _is_valid_uuid(tag):
84+
tag_list.append(tag)
85+
else:
86+
df_filt = df[df["Tag Name"] == tag]
87+
if df_filt.empty:
88+
raise ValueError(f"Tag '{tag}' not found in the tenant's tags.")
89+
tag_id = df_filt["Tag Id"].iloc[0]
90+
tag_list.append(tag_id)
91+
92+
return tag_list
93+
94+
95+
def apply_tags(
96+
item: str | UUID,
97+
type: str,
98+
tags: str | UUID | List[str | UUID],
99+
workspace: Optional[str | UUID] = None,
100+
):
101+
"""
102+
Shows a list of all the tenant's tags.
103+
104+
This is a wrapper function for the following API: `Tags - Apply Tags <https://learn.microsoft.com/rest/api/fabric/core/tags/apply-tags>`_.
105+
106+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
107+
108+
Parameters
109+
----------
110+
item : str | uuid.UUID
111+
The name or ID of the item to apply tags to.
112+
type : str
113+
The type of the item to apply tags to. For example: "Lakehouse".
114+
tags : str | uuid.UUID | List[str | uuid.UUID]
115+
The name or ID of the tag(s) to apply to the item.
116+
workspace : str | uuid.UUID, default=None
117+
The workspace name or ID.
118+
Defaults to None which resolves to the workspace of the attached lakehouse
119+
or if no lakehouse attached, resolves to the workspace of the notebook.
120+
"""
121+
122+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
123+
(item_name, item_id) = resolve_item_name_and_id(item, type, workspace_id)
124+
125+
if isinstance(tags, str):
126+
tags = [tags]
127+
128+
tag_list = resolve_tags(tags)
129+
130+
payload = {
131+
"tags": tag_list,
132+
}
133+
134+
_base_api(
135+
request=f"/v1/workspaces/{workspace_id}/items/{item_id}/applyTags",
136+
client="fabric_sp",
137+
method="post",
138+
payload=payload,
139+
)
140+
141+
print(
142+
f"{icons.green_dot} Tags {tags} applied to the '{item_name}' {type.lower()} within the '{workspace_name}' workspace"
143+
)
144+
145+
146+
def unapply_tags(
147+
item: str | UUID,
148+
type: str,
149+
tags: str | UUID | List[str | UUID],
150+
workspace: Optional[str | UUID] = None,
151+
):
152+
"""
153+
Shows a list of all the tenant's tags.
154+
155+
This is a wrapper function for the following API: `Tags - Unapply Tags <https://learn.microsoft.com/rest/api/fabric/core/tags/unapply-tags>`_.
156+
157+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
158+
159+
Parameters
160+
----------
161+
item : str | uuid.UUID
162+
The name or ID of the item to apply tags to.
163+
type : str
164+
The type of the item to apply tags to. For example: "Lakehouse".
165+
tags : str | uuid.UUID | List[str | uuid.UUID]
166+
The name or ID of the tag(s) to apply to the item.
167+
workspace : str | uuid.UUID, default=None
168+
The workspace name or ID.
169+
Defaults to None which resolves to the workspace of the attached lakehouse
170+
or if no lakehouse attached, resolves to the workspace of the notebook.
171+
"""
172+
173+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
174+
(item_name, item_id) = resolve_item_name_and_id(item, type, workspace_id)
175+
176+
if isinstance(tags, str):
177+
tags = [tags]
178+
179+
tag_list = resolve_tags(tags)
180+
181+
payload = {
182+
"tags": tag_list,
183+
}
184+
185+
_base_api(
186+
request=f"/v1/workspaces/{workspace_id}/items/{item_id}/unapplyTags",
187+
client="fabric_sp",
188+
method="post",
189+
payload=payload,
190+
)
191+
192+
print(
193+
f"{icons.green_dot} Tags {tags} applied to the '{item_name}' {type.lower()} within the '{workspace_name}' workspace"
194+
)

0 commit comments

Comments
 (0)