|
| 1 | +from sempy_labs._helper_functions import ( |
| 2 | + resolve_item_id, |
| 3 | + resolve_workspace_name_and_id, |
| 4 | + _base_api, |
| 5 | + _create_dataframe, |
| 6 | + _update_dataframe_datatypes, |
| 7 | + resolve_workspace_id, |
| 8 | + resolve_item_name_and_id, |
| 9 | +) |
| 10 | +import pandas as pd |
| 11 | +from typing import Optional, List, Literal |
| 12 | +import sempy_labs._icons as icons |
| 13 | +from uuid import UUID |
| 14 | +from sempy._utils._log import log |
| 15 | + |
| 16 | + |
| 17 | +def _get_base_url(item, type, workspace): |
| 18 | + |
| 19 | + workspace_id = resolve_workspace_id(workspace) |
| 20 | + item_id = resolve_item_id(item=item, type=type, workspace=workspace) |
| 21 | + |
| 22 | + type_dict = { |
| 23 | + "Warehouse": "warehouses", |
| 24 | + "SQLEndpoint": "sqlEndpoints", |
| 25 | + } |
| 26 | + type_for_url = type_dict.get(type) |
| 27 | + |
| 28 | + if type in ["SQLEndpoint", "Warehouse"]: |
| 29 | + url = f"/v1/workspaces/{workspace_id}/{type_for_url}/{item_id}" |
| 30 | + else: |
| 31 | + raise ValueError( |
| 32 | + f"{icons.red_dot} The type must be 'Warehouse' or 'SQLEndpoint'." |
| 33 | + ) |
| 34 | + |
| 35 | + return url |
| 36 | + |
| 37 | + |
| 38 | +@log |
| 39 | +def get_sql_audit_settings( |
| 40 | + item: str | UUID, |
| 41 | + type: Literal["Warehouse", "SQLEndpoint"], |
| 42 | + workspace: Optional[str | UUID] = None, |
| 43 | +) -> pd.DataFrame: |
| 44 | + """ |
| 45 | + Shows the SQL audit settings of a Fabric item. |
| 46 | +
|
| 47 | + This is a wrapper function for the following API: `SQL Audit Settings - Get SQL Audit Settings <https://learn.microsoft.com/rest/api/fabric/warehouse/sql-audit-settings/get-sql-audit-settings>`_. |
| 48 | +
|
| 49 | + Service Principal Authentication is supported (see `here <https://learn.microsoft.com/rest/api/fabric/warehouse/sql-audit-settings/get-sql-audit-settings#service-principal-authentication>`_). |
| 50 | +
|
| 51 | + Parameters |
| 52 | + ---------- |
| 53 | + item : str | uuid.UUID |
| 54 | + The name or ID of the item (Warehouse or SQLEndpoint). |
| 55 | + type : Literal['Warehouse', 'SQLEndpoint'] |
| 56 | + The type of the item. Must be 'Warehouse' or 'SQLEndpoint'. |
| 57 | + workspace : str | uuid.UUID, default=None |
| 58 | + The Fabric workspace name or ID. |
| 59 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 60 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 61 | +
|
| 62 | + Returns |
| 63 | + ------- |
| 64 | + pandas.DataFrame |
| 65 | + A pandas dataframe containing the SQL audit settings of the specified warehouse. |
| 66 | + """ |
| 67 | + |
| 68 | + columns = { |
| 69 | + "State": "string", |
| 70 | + "Retention Days": "int", |
| 71 | + "Audit Actions And Group": "list", |
| 72 | + } |
| 73 | + |
| 74 | + df = _create_dataframe(columns=columns) |
| 75 | + |
| 76 | + url = _get_base_url(item=item, type=type, workspace=workspace) |
| 77 | + response = _base_api( |
| 78 | + request=f"{url}/settings/sqlAudit", |
| 79 | + client="fabric_sp", |
| 80 | + ).json() |
| 81 | + |
| 82 | + rows = [] |
| 83 | + rows.append( |
| 84 | + { |
| 85 | + "State": response.get("state"), |
| 86 | + "Retention Days": response.get("retentionDays"), |
| 87 | + "Audit Actions And Group": response.get("auditActionsAndGroups"), |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + if rows: |
| 92 | + df = pd.DataFrame(rows, columns=list(columns.keys())) |
| 93 | + _update_dataframe_datatypes(dataframe=df, column_map=columns) |
| 94 | + |
| 95 | + return df |
| 96 | + |
| 97 | + |
| 98 | +@log |
| 99 | +def update_sql_audit_settings( |
| 100 | + item: str | UUID, |
| 101 | + type: Literal["Warehouse", "SQLEndpoint"], |
| 102 | + workspace: Optional[str | UUID] = None, |
| 103 | + retention_days: Optional[int] = None, |
| 104 | + state: Optional[str] = None, |
| 105 | +): |
| 106 | + """ |
| 107 | + Update settings associated with the item. |
| 108 | +
|
| 109 | + This is a wrapper function for the following API: SQL Audit Settings - Update SQL Audit Settings <https://learn.microsoft.com/rest/api/fabric/warehouse/sql-audit-settings/update-sql-audit-settings>`_. |
| 110 | +
|
| 111 | + Service Principal Authentication is supported (see `here <https://learn.microsoft.com/rest/api/fabric/warehouse/sql-audit-settings/get-sql-audit-settings#service-principal-authentication>`_). |
| 112 | +
|
| 113 | + Parameters |
| 114 | + ---------- |
| 115 | + item : str | uuid.UUID |
| 116 | + The name or ID of the item (Warehouse or SQLEndpoint). |
| 117 | + type : Literal['Warehouse', 'SQLEndpoint'] |
| 118 | + The type of the item. Must be 'Warehouse' or 'SQLEndpoint'. |
| 119 | + workspace : str | uuid.UUID, default=None |
| 120 | + The Fabric workspace name or ID. |
| 121 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 122 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 123 | + """ |
| 124 | + |
| 125 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 126 | + |
| 127 | + payload = {} |
| 128 | + if retention_days is not None: |
| 129 | + if not isinstance(retention_days, int) or retention_days < 0: |
| 130 | + raise ValueError( |
| 131 | + f"{icons.red_dot} retention_days must be a non-negative integer." |
| 132 | + ) |
| 133 | + payload["retentionDays"] = retention_days |
| 134 | + if state is not None: |
| 135 | + state = state.capitalize() |
| 136 | + if state not in ["Enabled", "Disabled"]: |
| 137 | + raise ValueError( |
| 138 | + f"{icons.red_dot} state must be either 'Enabled' or 'Disabled'." |
| 139 | + ) |
| 140 | + payload["state"] = state |
| 141 | + |
| 142 | + if not payload: |
| 143 | + print( |
| 144 | + f"{icons.info} No updates were made as neither retention_days nor state were provided." |
| 145 | + ) |
| 146 | + return |
| 147 | + |
| 148 | + url = _get_base_url(item=item, type=type, workspace=workspace) |
| 149 | + _base_api( |
| 150 | + request=f"{url}/settings/sqlAudit", |
| 151 | + client="fabric_sp", |
| 152 | + method="patch", |
| 153 | + payload=payload, |
| 154 | + ) |
| 155 | + |
| 156 | + print( |
| 157 | + f"{icons.green_dot} The SQL audit settings for the '{item}' {type.lower()} within the '{workspace_name}' workspace have been updated accordingly." |
| 158 | + ) |
| 159 | + |
| 160 | + |
| 161 | +@log |
| 162 | +def set_audit_actions_and_group( |
| 163 | + item: str | UUID, |
| 164 | + type: Literal["Warehouse", "SQLEndpoint"], |
| 165 | + sql_audit_groups: List[str], |
| 166 | + workspace: Optional[str | UUID] = None, |
| 167 | +): |
| 168 | + """ |
| 169 | + Update the audit actions and groups for this item. |
| 170 | +
|
| 171 | + This is a wrapper function for the following API: SQL Audit Settings - Set Audit Actions And Groups <https://learn.microsoft.com/rest/api/fabric/warehouse/sql-audit-settings/set-audit-actions-and-groups>`_. |
| 172 | +
|
| 173 | + Service Principal Authentication is supported (see `here <https://learn.microsoft.com/rest/api/fabric/warehouse/sql-audit-settings/get-sql-audit-settings#service-principal-authentication>`_). |
| 174 | +
|
| 175 | + Parameters |
| 176 | + ---------- |
| 177 | + item : str | uuid.UUID |
| 178 | + The name or ID of the item (Warehouse or SQLEndpoint). |
| 179 | + type : Literal['Warehouse', 'SQLEndpoint'] |
| 180 | + The type of the item. Must be 'Warehouse' or 'SQLEndpoint'. |
| 181 | + workspace : str | uuid.UUID, default=None |
| 182 | + The Fabric workspace name or ID. |
| 183 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 184 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 185 | + """ |
| 186 | + |
| 187 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 188 | + |
| 189 | + if ( |
| 190 | + not sql_audit_groups |
| 191 | + or not isinstance(sql_audit_groups, list) |
| 192 | + or not all(isinstance(item, str) for item in sql_audit_groups) |
| 193 | + ): |
| 194 | + raise ValueError( |
| 195 | + f"{icons.red_dot} sql_audit_groups must be a non-empty list of strings." |
| 196 | + ) |
| 197 | + |
| 198 | + url = _get_base_url(item=item, type=type, workspace=workspace) |
| 199 | + _base_api( |
| 200 | + request=f"{url}/settings/sqlAudit/setAuditActionsAndGroups", |
| 201 | + client="fabric_sp", |
| 202 | + method="post", |
| 203 | + payload=sql_audit_groups, |
| 204 | + ) |
| 205 | + |
| 206 | + print( |
| 207 | + f"{icons.green_dot} The SQL audit actions and groups for the '{item}' {type.lower()} within the '{workspace_name}' workspace have been updated accordingly." |
| 208 | + ) |
0 commit comments