|
1 | | -from ._helper_functions import ( |
| 1 | +from sempy_labs._helper_functions import ( |
2 | 2 | resolve_item_id, |
3 | 3 | resolve_workspace_name_and_id, |
4 | 4 | _base_api, |
5 | 5 | _create_dataframe, |
6 | 6 | _update_dataframe_datatypes, |
7 | 7 | delete_item, |
8 | 8 | resolve_workspace_id, |
| 9 | + resolve_item_name_and_id, |
9 | 10 | ) |
10 | 11 | import pandas as pd |
11 | | -from typing import Optional |
| 12 | +from typing import Optional, List |
12 | 13 | import sempy_labs._icons as icons |
13 | 14 | from uuid import UUID |
14 | 15 | from sempy._utils._log import log |
@@ -287,3 +288,172 @@ def get_warehouse_connection_string( |
287 | 288 | response = _base_api(request=url, client="fabric_sp") |
288 | 289 |
|
289 | 290 | return response.json().get("connectionString") |
| 291 | + |
| 292 | + |
| 293 | +@log |
| 294 | +def get_warehouse_sql_audit_settings( |
| 295 | + warehouse: str | UUID, workspace: Optional[str | UUID] = None |
| 296 | +) -> pd.DataFrame: |
| 297 | + """ |
| 298 | + Shows the SQL audit settings of a Fabric warehouse. |
| 299 | +
|
| 300 | + 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>`_. |
| 301 | +
|
| 302 | + 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>`_). |
| 303 | +
|
| 304 | + Parameters |
| 305 | + ---------- |
| 306 | + warehouse : str | uuid.UUID |
| 307 | + Name or ID of the Fabric warehouse. |
| 308 | + workspace : str | uuid.UUID, default=None |
| 309 | + The Fabric workspace name or ID. |
| 310 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 311 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 312 | +
|
| 313 | + Returns |
| 314 | + ------- |
| 315 | + pandas.DataFrame |
| 316 | + A pandas dataframe containing the SQL audit settings of the specified warehouse. |
| 317 | + """ |
| 318 | + workspace_id = resolve_workspace_id(workspace) |
| 319 | + warehouse_id = resolve_item_id( |
| 320 | + item=warehouse, type="Warehouse", workspace=workspace |
| 321 | + ) |
| 322 | + |
| 323 | + columns = { |
| 324 | + "State": "string", |
| 325 | + "Retention Days": "int", |
| 326 | + "Audit Actions And Group": "list", |
| 327 | + } |
| 328 | + |
| 329 | + df = _create_dataframe(columns=columns) |
| 330 | + |
| 331 | + response = _base_api( |
| 332 | + request=f"/v1/workspaces/{workspace_id}/warehouses/{warehouse_id}/settings/sqlAudit", |
| 333 | + client="fabric_sp", |
| 334 | + ).json() |
| 335 | + |
| 336 | + rows = [] |
| 337 | + rows.append( |
| 338 | + { |
| 339 | + "State": response.get("state"), |
| 340 | + "Retention Days": response.get("retentionDays"), |
| 341 | + "Audit Actions And Group": response.get("auditActionsAndGroups"), |
| 342 | + } |
| 343 | + ) |
| 344 | + |
| 345 | + if rows: |
| 346 | + df = pd.DataFrame(rows, columns=list(columns.keys())) |
| 347 | + _update_dataframe_datatypes(dataframe=df, column_map=columns) |
| 348 | + |
| 349 | + return df |
| 350 | + |
| 351 | + |
| 352 | +@log |
| 353 | +def update_warehouse_sql_audit_settings( |
| 354 | + warehouse: str | UUID, |
| 355 | + workspace: Optional[str | UUID] = None, |
| 356 | + retention_days: Optional[int] = None, |
| 357 | + state: Optional[str] = None, |
| 358 | +): |
| 359 | + """ |
| 360 | + Update settings associated with the warehouse. |
| 361 | +
|
| 362 | + 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>`_. |
| 363 | +
|
| 364 | + 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>`_). |
| 365 | +
|
| 366 | + Parameters |
| 367 | + ---------- |
| 368 | + warehouse : str | uuid.UUID |
| 369 | + Name or ID of the Fabric warehouse. |
| 370 | + workspace : str | uuid.UUID, default=None |
| 371 | + The Fabric workspace name or ID. |
| 372 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 373 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 374 | + """ |
| 375 | + |
| 376 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 377 | + (warehouse_name, warehouse_id) = resolve_item_name_and_id( |
| 378 | + item=warehouse, type="Warehouse", workspace=workspace |
| 379 | + ) |
| 380 | + |
| 381 | + payload = {} |
| 382 | + if retention_days is not None: |
| 383 | + if not isinstance(retention_days, int) or retention_days < 0: |
| 384 | + raise ValueError( |
| 385 | + f"{icons.red_dot} retention_days must be a non-negative integer." |
| 386 | + ) |
| 387 | + payload["retentionDays"] = retention_days |
| 388 | + if state is not None: |
| 389 | + state = state.capitalize() |
| 390 | + if state not in ["Enabled", "Disabled"]: |
| 391 | + raise ValueError( |
| 392 | + f"{icons.red_dot} state must be either 'Enabled' or 'Disabled'." |
| 393 | + ) |
| 394 | + payload["state"] = state |
| 395 | + |
| 396 | + if not payload: |
| 397 | + print( |
| 398 | + f"{icons.info} No updates were made as neither retention_days nor state were provided." |
| 399 | + ) |
| 400 | + return |
| 401 | + |
| 402 | + _base_api( |
| 403 | + request=f"/v1/workspaces/{workspace_id}/warehouses/{warehouse_id}/settings/sqlAudit", |
| 404 | + client="fabric_sp", |
| 405 | + method="patch", |
| 406 | + payload=payload, |
| 407 | + ) |
| 408 | + |
| 409 | + print( |
| 410 | + f"{icons.green_dot} The SQL audit settings for the '{warehouse_name}' warehouse within the '{workspace_name}' workspace have been updated accordingly." |
| 411 | + ) |
| 412 | + |
| 413 | + |
| 414 | +@log |
| 415 | +def set_warehouse_audit_actions_and_group( |
| 416 | + warehouse: str | UUID, |
| 417 | + sql_audit_groups: List[str], |
| 418 | + workspace: Optional[str | UUID] = None, |
| 419 | +): |
| 420 | + """ |
| 421 | + Update the audit actions and groups for this warehouse. |
| 422 | +
|
| 423 | + 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>`_. |
| 424 | +
|
| 425 | + 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>`_). |
| 426 | +
|
| 427 | + Parameters |
| 428 | + ---------- |
| 429 | + warehouse : str | uuid.UUID |
| 430 | + Name or ID of the Fabric warehouse. |
| 431 | + workspace : str | uuid.UUID, default=None |
| 432 | + The Fabric workspace name or ID. |
| 433 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 434 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 435 | + """ |
| 436 | + |
| 437 | + (workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace) |
| 438 | + (warehouse_name, warehouse_id) = resolve_item_name_and_id( |
| 439 | + item=warehouse, type="Warehouse", workspace=workspace |
| 440 | + ) |
| 441 | + if ( |
| 442 | + not sql_audit_groups |
| 443 | + or not isinstance(sql_audit_groups, list) |
| 444 | + or not all(isinstance(item, str) for item in sql_audit_groups) |
| 445 | + ): |
| 446 | + raise ValueError( |
| 447 | + f"{icons.red_dot} sql_audit_groups must be a non-empty list of strings." |
| 448 | + ) |
| 449 | + |
| 450 | + _base_api( |
| 451 | + request=f"/v1/workspaces/{workspace_id}/warehouses/{warehouse_id}/settings/sqlAudit/setAuditActionsAndGroups", |
| 452 | + client="fabric_sp", |
| 453 | + method="post", |
| 454 | + payload=sql_audit_groups, |
| 455 | + ) |
| 456 | + |
| 457 | + print( |
| 458 | + f"{icons.green_dot} The SQL audit actions and groups for the '{warehouse_name}' warehouse within the '{workspace_name}' workspace have been updated accordingly." |
| 459 | + ) |
0 commit comments