|
| 1 | +from typing import Literal, List |
| 2 | +from uuid import UUID |
| 3 | +import sempy_labs._icons as icons |
| 4 | +from sempy_labs.admin._basic_functions import ( |
| 5 | + _resolve_workspace_name_and_id, |
| 6 | +) |
| 7 | +from sempy_labs.admin._items import ( |
| 8 | + list_items, |
| 9 | +) |
| 10 | +from sempy_labs._helper_functions import ( |
| 11 | + _is_valid_uuid, |
| 12 | + _base_api, |
| 13 | +) |
| 14 | +from sempy._utils._log import log |
| 15 | + |
| 16 | + |
| 17 | +@log |
| 18 | +def bulk_set_labels( |
| 19 | + items: List[dict], |
| 20 | + label_id: UUID, |
| 21 | + assignment_method: Literal["Standard", "Priviledged"] = "Standard", |
| 22 | +): |
| 23 | + """ |
| 24 | + Sets sensitivity labels on Fabric items. |
| 25 | +
|
| 26 | + Note: Please use the sempy_labs.graph.resolve_sensitivity_label_id function to retrieve label IDs. |
| 27 | +
|
| 28 | + This is a wrapper function for the following API: `Labels - Bulk Set Labels <https://learn.microsoft.com/rest/api/fabric/admin/labels/bulk-set-labels>`_. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + items : List[dict] |
| 33 | + A list of dictionaries containing the item details. |
| 34 | +
|
| 35 | + Example 1: |
| 36 | + items = [ |
| 37 | + { |
| 38 | + "id": "fe472f5e-636e-4c10-a1c6-7e9edc0b542a", |
| 39 | + "type": "Dashboard" |
| 40 | + }, |
| 41 | + { |
| 42 | + "id": "fe472f5e-636e-4c10-a1c6-7e9edc0b542c", |
| 43 | + "type": "Report" |
| 44 | + }, |
| 45 | + { |
| 46 | + "id": "fe472f5e-636e-4c10-a1c6-7e9edc0b542e", |
| 47 | + "type": "SemanticModel" |
| 48 | + }, |
| 49 | + ] |
| 50 | +
|
| 51 | + Example 2: |
| 52 | + items = [ |
| 53 | + { |
| 54 | + "id": "Dashboard 1", |
| 55 | + "type": "Dashboard", |
| 56 | + "workspace": "Sales Workspace" |
| 57 | + }, |
| 58 | + { |
| 59 | + "id": "Sales Report", |
| 60 | + "type": "Report", |
| 61 | + "workspace": "Sales Workspace" |
| 62 | + }, |
| 63 | + { |
| 64 | + "id": "KPI Model", |
| 65 | + "type": "SemanticModel", |
| 66 | + "workspace": "Workspace 2" |
| 67 | + }, |
| 68 | + ] |
| 69 | +
|
| 70 | + label_id : uuid.UUID |
| 71 | + The label ID, which must be in the user's label policy. |
| 72 | + assignment_method : Literal["Standard", "Priviledged"], default="Standard" |
| 73 | + Specifies whether the assigned label was set by an automated process or manually. Additional tenant setting property types may be added over time. |
| 74 | + """ |
| 75 | + |
| 76 | + if assignment_method not in ["Standard", "Priviledged"]: |
| 77 | + raise ValueError("assignment_method must be either 'Standard' or 'Priviledged'") |
| 78 | + |
| 79 | + payload = {"items": []} |
| 80 | + df = list_items() |
| 81 | + |
| 82 | + for i in items: |
| 83 | + item = i.get("item") |
| 84 | + type = i.get("type") |
| 85 | + workspace = i.get("workspace") |
| 86 | + if _is_valid_uuid(item): |
| 87 | + payload["items"].append( |
| 88 | + { |
| 89 | + "id": item, |
| 90 | + "type": type, |
| 91 | + } |
| 92 | + ) |
| 93 | + else: |
| 94 | + workspace_id = _resolve_workspace_name_and_id(workspace)[1] |
| 95 | + df_filtered = df[ |
| 96 | + (df["Item Name"] == item) |
| 97 | + & (df["Type"] == type) |
| 98 | + & (df["Workspace Id"] == workspace_id) |
| 99 | + ] |
| 100 | + if df_filtered.empty: |
| 101 | + raise ValueError( |
| 102 | + f"The item '{item}' of type '{type}' does not exist in workspace '{workspace}'." |
| 103 | + ) |
| 104 | + else: |
| 105 | + payload["items"].append( |
| 106 | + { |
| 107 | + "id": df_filtered["Item Id"].iloc[0], |
| 108 | + "type": type, |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + payload["labelId"] = label_id |
| 113 | + payload["assignmentMethod"] = assignment_method |
| 114 | + |
| 115 | + _base_api(request="/v1/admin/items/bulkSetLabels", method="post", payload=payload) |
| 116 | + |
| 117 | + print( |
| 118 | + f"{icons.green_dot} Labels have been successfully set on the specified items." |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +@log |
| 123 | +def bulk_remove_labels( |
| 124 | + items: List[dict], |
| 125 | +): |
| 126 | + """ |
| 127 | + Removes sensitivity labels from Fabric items. |
| 128 | +
|
| 129 | + This is a wrapper function for the following API: `Labels - Bulk Remove Labels <https://learn.microsoft.com/rest/api/fabric/admin/labels/bulk-remove-labels>`_. |
| 130 | +
|
| 131 | + Parameters |
| 132 | + ---------- |
| 133 | + items : List[dict] |
| 134 | + A list of dictionaries containing the item details. |
| 135 | +
|
| 136 | + Example 1: |
| 137 | + items = [ |
| 138 | + { |
| 139 | + "id": "fe472f5e-636e-4c10-a1c6-7e9edc0b542a", |
| 140 | + "type": "Dashboard" |
| 141 | + }, |
| 142 | + { |
| 143 | + "id": "fe472f5e-636e-4c10-a1c6-7e9edc0b542c", |
| 144 | + "type": "Report" |
| 145 | + }, |
| 146 | + { |
| 147 | + "id": "fe472f5e-636e-4c10-a1c6-7e9edc0b542e", |
| 148 | + "type": "SemanticModel" |
| 149 | + }, |
| 150 | + ] |
| 151 | +
|
| 152 | + Example 2: |
| 153 | + items = [ |
| 154 | + { |
| 155 | + "id": "Dashboard 1", |
| 156 | + "type": "Dashboard", |
| 157 | + "workspace": "Sales Workspace" |
| 158 | + }, |
| 159 | + { |
| 160 | + "id": "Sales Report", |
| 161 | + "type": "Report", |
| 162 | + "workspace": "Sales Workspace" |
| 163 | + }, |
| 164 | + { |
| 165 | + "id": "KPI Model", |
| 166 | + "type": "SemanticModel", |
| 167 | + "workspace": "Workspace 2" |
| 168 | + }, |
| 169 | + ] |
| 170 | + """ |
| 171 | + |
| 172 | + payload = {"items": []} |
| 173 | + df = list_items() |
| 174 | + |
| 175 | + for i in items: |
| 176 | + item = i.get("item") |
| 177 | + type = i.get("type") |
| 178 | + workspace = i.get("workspace") |
| 179 | + if _is_valid_uuid(item): |
| 180 | + payload["items"].append( |
| 181 | + { |
| 182 | + "id": item, |
| 183 | + "type": type, |
| 184 | + } |
| 185 | + ) |
| 186 | + else: |
| 187 | + workspace_id = _resolve_workspace_name_and_id(workspace)[1] |
| 188 | + df_filtered = df[ |
| 189 | + (df["Item Name"] == item) |
| 190 | + & (df["Type"] == type) |
| 191 | + & (df["Workspace Id"] == workspace_id) |
| 192 | + ] |
| 193 | + if df_filtered.empty: |
| 194 | + raise ValueError( |
| 195 | + f"The item '{item}' of type '{type}' does not exist in workspace '{workspace}'." |
| 196 | + ) |
| 197 | + else: |
| 198 | + payload["items"].append( |
| 199 | + { |
| 200 | + "id": df_filtered["Item Id"].iloc[0], |
| 201 | + "type": type, |
| 202 | + } |
| 203 | + ) |
| 204 | + |
| 205 | + _base_api( |
| 206 | + request="/v1/admin/items/bulkRemoveLabels", method="post", payload=payload |
| 207 | + ) |
| 208 | + |
| 209 | + print( |
| 210 | + f"{icons.green_dot} Labels have been successfully removed from the specified items." |
| 211 | + ) |
0 commit comments