|
| 1 | +import sempy.fabric as fabric |
| 2 | +import requests |
| 3 | +import pandas as pd |
| 4 | +from typing import Optional, Union |
| 5 | +from uuid import UUID |
| 6 | +from sempy.fabric.exceptions import FabricHTTPException |
| 7 | +from sempy._utils._log import log |
| 8 | + |
| 9 | + |
| 10 | +@log |
| 11 | +def list_item_labels(workspace: Optional[Union[str, UUID]] = None) -> pd.DataFrame: |
| 12 | + """ |
| 13 | + List all items within a workspace and shows their sensitivity labels. |
| 14 | +
|
| 15 | + NOTE: This function uses an internal API and is subject to change/break without notice. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + workspace : str | uuid.UUID, default=None |
| 20 | + The Fabric workspace name or ID. |
| 21 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 22 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 23 | + Returns |
| 24 | + ------- |
| 25 | + pandas.DataFrame |
| 26 | + A pandas dataframe showing a list of all items within a workspace and their sensitivity labels. |
| 27 | + """ |
| 28 | + |
| 29 | + import notebookutils |
| 30 | + |
| 31 | + token = notebookutils.credentials.getToken("pbi") |
| 32 | + headers = {"Authorization": f"Bearer {token}"} |
| 33 | + |
| 34 | + # Item types handled in special payload fields |
| 35 | + grouped_types = { |
| 36 | + "dashboards": "Dashboard", |
| 37 | + "reports": "Report", |
| 38 | + "models": "SemanticModel", |
| 39 | + "dataflows": "Dataflow", |
| 40 | + "datamarts": "Datamart", |
| 41 | + } |
| 42 | + |
| 43 | + # All other item types go into 'artifacts' |
| 44 | + fabric_items = [ |
| 45 | + "Datamart", |
| 46 | + "Lakehouse", |
| 47 | + "Eventhouse", |
| 48 | + "Environment", |
| 49 | + "KQLDatabase", |
| 50 | + "KQLQueryset", |
| 51 | + "KQLDashboard", |
| 52 | + "DataPipeline", |
| 53 | + "Notebook", |
| 54 | + "SparkJobDefinition", |
| 55 | + "MLExperiment", |
| 56 | + "MLModel", |
| 57 | + "Warehouse", |
| 58 | + "Eventstream", |
| 59 | + "SQLEndpoint", |
| 60 | + "MirroredWarehouse", |
| 61 | + "MirroredDatabase", |
| 62 | + "Reflex", |
| 63 | + "GraphQLApi", |
| 64 | + "MountedDataFactory", |
| 65 | + "SQLDatabase", |
| 66 | + "CopyJob", |
| 67 | + "VariableLibrary", |
| 68 | + "Dataflow", |
| 69 | + "ApacheAirflowJob", |
| 70 | + "WarehouseSnapshot", |
| 71 | + "DigitalTwinBuilder", |
| 72 | + "DigitalTwinBuilderFlow", |
| 73 | + "MirroredAzureDatabricksCatalog", |
| 74 | + "DataAgent", |
| 75 | + "UserDataFunction", |
| 76 | + ] |
| 77 | + |
| 78 | + dfI = fabric.list_items(workspace=workspace) |
| 79 | + |
| 80 | + payload = { |
| 81 | + key: [{"artifactId": i} for i in dfI[dfI["Type"] == value]["Id"].tolist()] |
| 82 | + for key, value in grouped_types.items() |
| 83 | + } |
| 84 | + |
| 85 | + # Add generic artifact types |
| 86 | + artifact_ids = dfI[dfI["Type"].isin(fabric_items)]["Id"].tolist() |
| 87 | + if artifact_ids: |
| 88 | + payload["artifacts"] = [{"artifactId": i} for i in artifact_ids] |
| 89 | + |
| 90 | + client = fabric.PowerBIRestClient() |
| 91 | + response = client.get("/v1.0/myorg/capacities") |
| 92 | + if response.status_code != 200: |
| 93 | + raise FabricHTTPException("Failed to retrieve URL prefix.") |
| 94 | + context = response.json().get("@odata.context") |
| 95 | + prefix = context.split("/v1.0")[0] |
| 96 | + |
| 97 | + response = requests.post( |
| 98 | + f"{prefix}/metadata/informationProtection/artifacts", |
| 99 | + json=payload, |
| 100 | + headers=headers, |
| 101 | + ) |
| 102 | + if response.status_code != 200: |
| 103 | + raise FabricHTTPException(f"Failed to retrieve labels: {response.text}") |
| 104 | + result = response.json() |
| 105 | + |
| 106 | + label_keys = [ |
| 107 | + "artifactInformationProtections", |
| 108 | + "datasetInformationProtections", |
| 109 | + "reportInformationProtections", |
| 110 | + "dashboardInformationProtections", |
| 111 | + ] |
| 112 | + |
| 113 | + rows = [ |
| 114 | + { |
| 115 | + "Id": item.get("artifactObjectId"), |
| 116 | + "Label Id": item.get("labelId"), |
| 117 | + "Label Name": item.get("name"), |
| 118 | + "Parent Label Name": item.get("parent", {}).get("name"), |
| 119 | + "Label Description": item.get("tooltip"), |
| 120 | + } |
| 121 | + for key in label_keys |
| 122 | + for item in result.get(key, []) |
| 123 | + ] |
| 124 | + |
| 125 | + df_labels = pd.DataFrame(rows) |
| 126 | + return dfI.merge(df_labels, on="Id", how="left") |
0 commit comments