Coverage for src/commands/list_catalogs.py: 100%
27 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
« prev ^ index » next coverage.py v7.8.0, created at 2025-06-05 22:56 -0700
1"""
2Command for listing Unity Catalog catalogs.
3"""
5from typing import Optional, Any
6from src.clients.databricks import DatabricksAPIClient
7from src.commands.base import CommandResult
8from src.catalogs import list_catalogs as get_catalogs_list
9from src.command_registry import CommandDefinition
10import logging
13def handle_command(
14 client: Optional[DatabricksAPIClient], **kwargs: Any
15) -> CommandResult:
16 """
17 List catalogs in Unity Catalog.
19 Args:
20 client: DatabricksAPIClient instance for API calls
21 **kwargs: Command parameters
22 - include_browse: Whether to include catalogs with selective metadata access (optional)
23 - max_results: Maximum number of catalogs to return (optional)
24 - page_token: Opaque pagination token to go to next page (optional)
26 Returns:
27 CommandResult with list of catalogs if successful
28 """
29 if not client:
30 return CommandResult(
31 False,
32 message="No Databricks client available. Please set up your workspace first.",
33 )
35 # Extract parameters
36 include_browse = kwargs.get("include_browse", False)
37 max_results = kwargs.get("max_results")
38 page_token = kwargs.get("page_token")
40 try:
41 # List catalogs in Unity Catalog
42 result = get_catalogs_list(
43 client=client,
44 include_browse=include_browse,
45 max_results=max_results,
46 page_token=page_token,
47 )
49 catalogs = result.get("catalogs", [])
50 next_page_token = result.get("next_page_token")
52 if not catalogs:
53 return CommandResult(True, message="No catalogs found.")
55 # Format catalog information for display
56 formatted_catalogs = []
57 for catalog in catalogs:
58 formatted_catalog = {
59 "name": catalog.get("name"),
60 "type": catalog.get("type", ""),
61 "comment": catalog.get("comment", ""),
62 "provider": catalog.get("provider", {}).get("name", ""),
63 "created_at": catalog.get("created_at"),
64 "created_by": catalog.get("created_by", ""),
65 "owner": catalog.get("owner", ""),
66 }
67 formatted_catalogs.append(formatted_catalog)
69 return CommandResult(
70 True,
71 data={
72 "catalogs": formatted_catalogs,
73 "total_count": len(formatted_catalogs),
74 "next_page_token": next_page_token,
75 },
76 message=f"Found {len(formatted_catalogs)} catalog(s)."
77 + (
78 f" More catalogs available with page token: {next_page_token}"
79 if next_page_token
80 else ""
81 ),
82 )
83 except Exception as e:
84 logging.error(f"Error listing catalogs: {str(e)}")
85 return CommandResult(
86 False, message=f"Failed to list catalogs: {str(e)}", error=e
87 )
90DEFINITION = CommandDefinition(
91 name="list-catalogs",
92 description="List catalogs in Unity Catalog. Only useful for listing catalogs, not schemas, not tables nor anything else.",
93 handler=handle_command,
94 parameters={
95 "include_browse": {
96 "type": "boolean",
97 "description": "Whether to include catalogs with selective metadata access.",
98 "default": False,
99 },
100 "max_results": {
101 "type": "integer",
102 "description": "Maximum number of catalogs to return.",
103 },
104 "page_token": {
105 "type": "string",
106 "description": "Opaque pagination token to go to next page.",
107 },
108 },
109 required_params=[],
110 tui_aliases=["/catalogs"],
111 needs_api_client=True,
112 visible_to_user=True,
113 visible_to_agent=True,
114 agent_display="full", # Show full catalog list to agents
115 usage_hint="Usage: /list-catalogs [--include_browse true|false] [--max_results <number>] [--page_token <token>]",
116)