dremio-cli 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dremio_cli/__init__.py +5 -0
- dremio_cli/__main__.py +6 -0
- dremio_cli/cli.py +191 -0
- dremio_cli/client/__init__.py +1 -0
- dremio_cli/client/auth.py +100 -0
- dremio_cli/client/base.py +190 -0
- dremio_cli/client/cloud.py +305 -0
- dremio_cli/client/factory.py +122 -0
- dremio_cli/client/software.py +561 -0
- dremio_cli/commands/__init__.py +1 -0
- dremio_cli/commands/catalog.py +143 -0
- dremio_cli/commands/favorite.py +162 -0
- dremio_cli/commands/folder.py +290 -0
- dremio_cli/commands/grant.py +202 -0
- dremio_cli/commands/history.py +122 -0
- dremio_cli/commands/job.py +321 -0
- dremio_cli/commands/lineage.py +9 -0
- dremio_cli/commands/profile.py +165 -0
- dremio_cli/commands/role.py +319 -0
- dremio_cli/commands/source.py +375 -0
- dremio_cli/commands/space.py +226 -0
- dremio_cli/commands/sql.py +280 -0
- dremio_cli/commands/table.py +155 -0
- dremio_cli/commands/tag.py +145 -0
- dremio_cli/commands/udf.py +9 -0
- dremio_cli/commands/user.py +250 -0
- dremio_cli/commands/view.py +369 -0
- dremio_cli/commands/wiki.py +163 -0
- dremio_cli/config.py +193 -0
- dremio_cli/env.py +175 -0
- dremio_cli/formatters/__init__.py +1 -0
- dremio_cli/formatters/json.py +19 -0
- dremio_cli/formatters/table.py +45 -0
- dremio_cli/formatters/yaml.py +17 -0
- dremio_cli/history.py +198 -0
- dremio_cli/utils/__init__.py +1 -0
- dremio_cli/utils/exceptions.py +35 -0
- dremio_cli/utils/helpers.py +73 -0
- dremio_cli/utils/validators.py +147 -0
- dremio_cli-1.0.0.dist-info/METADATA +261 -0
- dremio_cli-1.0.0.dist-info/RECORD +45 -0
- dremio_cli-1.0.0.dist-info/WHEEL +5 -0
- dremio_cli-1.0.0.dist-info/entry_points.txt +2 -0
- dremio_cli-1.0.0.dist-info/licenses/LICENSE +21 -0
- dremio_cli-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"""Favorite query management commands."""
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from rich.console import Console
|
|
5
|
+
from rich.table import Table
|
|
6
|
+
|
|
7
|
+
from dremio_cli.config import ProfileManager
|
|
8
|
+
from dremio_cli.client.factory import create_client
|
|
9
|
+
from dremio_cli.history import HistoryManager
|
|
10
|
+
from dremio_cli.formatters.json import format_as_json
|
|
11
|
+
from dremio_cli.formatters.yaml import format_as_yaml
|
|
12
|
+
|
|
13
|
+
console = Console()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@click.group()
|
|
17
|
+
def favorite() -> None:
|
|
18
|
+
"""Favorite query management."""
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@favorite.command("add")
|
|
23
|
+
@click.argument("name")
|
|
24
|
+
@click.option("--sql", required=True, help="SQL query")
|
|
25
|
+
@click.option("--description", help="Optional description")
|
|
26
|
+
@click.pass_context
|
|
27
|
+
def add_favorite(ctx, name: str, sql: str, description: str) -> None:
|
|
28
|
+
"""Add a favorite query.
|
|
29
|
+
|
|
30
|
+
Examples:
|
|
31
|
+
dremio favorite add daily_report --sql "SELECT * FROM sales WHERE date = CURRENT_DATE"
|
|
32
|
+
dremio favorite add summary --sql "SELECT COUNT(*) FROM customers" --description "Customer count"
|
|
33
|
+
"""
|
|
34
|
+
try:
|
|
35
|
+
manager = HistoryManager()
|
|
36
|
+
manager.add_favorite(name, sql, description)
|
|
37
|
+
|
|
38
|
+
console.print(f"[green]✓[/green] Favorite '{name}' saved")
|
|
39
|
+
console.print(f" SQL: {sql[:100]}{'...' if len(sql) > 100 else ''}")
|
|
40
|
+
|
|
41
|
+
except Exception as e:
|
|
42
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
43
|
+
if ctx.obj.verbose:
|
|
44
|
+
import traceback
|
|
45
|
+
traceback.print_exc()
|
|
46
|
+
raise click.Abort()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@favorite.command("list")
|
|
50
|
+
@click.pass_context
|
|
51
|
+
def list_favorites(ctx) -> None:
|
|
52
|
+
"""List all favorite queries.
|
|
53
|
+
|
|
54
|
+
Examples:
|
|
55
|
+
dremio favorite list
|
|
56
|
+
"""
|
|
57
|
+
try:
|
|
58
|
+
manager = HistoryManager()
|
|
59
|
+
favorites = manager.get_favorites()
|
|
60
|
+
|
|
61
|
+
output_format = ctx.obj.output_format
|
|
62
|
+
|
|
63
|
+
if output_format == "json":
|
|
64
|
+
console.print(format_as_json(favorites))
|
|
65
|
+
elif output_format == "yaml":
|
|
66
|
+
console.print(format_as_yaml(favorites))
|
|
67
|
+
else:
|
|
68
|
+
if favorites:
|
|
69
|
+
table = Table(title="Favorite Queries")
|
|
70
|
+
table.add_column("Name", style="cyan")
|
|
71
|
+
table.add_column("Query", style="yellow", max_width=60)
|
|
72
|
+
table.add_column("Description", style="green", max_width=30)
|
|
73
|
+
|
|
74
|
+
for fav in favorites:
|
|
75
|
+
query = fav["query"]
|
|
76
|
+
table.add_row(
|
|
77
|
+
fav["name"],
|
|
78
|
+
query[:57] + "..." if len(query) > 60 else query,
|
|
79
|
+
fav["description"] or ""
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
console.print(table)
|
|
83
|
+
else:
|
|
84
|
+
console.print("[yellow]No favorites found[/yellow]")
|
|
85
|
+
|
|
86
|
+
except Exception as e:
|
|
87
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
88
|
+
if ctx.obj.verbose:
|
|
89
|
+
import traceback
|
|
90
|
+
traceback.print_exc()
|
|
91
|
+
raise click.Abort()
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@favorite.command("run")
|
|
95
|
+
@click.argument("name")
|
|
96
|
+
@click.pass_context
|
|
97
|
+
def run_favorite(ctx, name: str) -> None:
|
|
98
|
+
"""Execute a favorite query.
|
|
99
|
+
|
|
100
|
+
Examples:
|
|
101
|
+
dremio favorite run daily_report
|
|
102
|
+
"""
|
|
103
|
+
try:
|
|
104
|
+
manager = HistoryManager()
|
|
105
|
+
favorite = manager.get_favorite(name)
|
|
106
|
+
|
|
107
|
+
if not favorite:
|
|
108
|
+
console.print(f"[red]Favorite '{name}' not found[/red]")
|
|
109
|
+
raise click.Abort()
|
|
110
|
+
|
|
111
|
+
# Get profile
|
|
112
|
+
profile_manager = ProfileManager()
|
|
113
|
+
profile_name = ctx.obj.profile_name
|
|
114
|
+
profile = profile_manager.get_profile(profile_name)
|
|
115
|
+
|
|
116
|
+
if not profile:
|
|
117
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
118
|
+
raise click.Abort()
|
|
119
|
+
|
|
120
|
+
# Create client
|
|
121
|
+
client = create_client(profile)
|
|
122
|
+
|
|
123
|
+
console.print(f"[cyan]Running favorite:[/cyan] {name}")
|
|
124
|
+
console.print(f"[yellow]SQL:[/yellow] {favorite['query']}")
|
|
125
|
+
|
|
126
|
+
# Execute query
|
|
127
|
+
with console.status(f"[bold green]Executing query..."):
|
|
128
|
+
result = client.execute_sql(favorite['query'])
|
|
129
|
+
|
|
130
|
+
job_id = result.get("id")
|
|
131
|
+
console.print(f"[green]✓[/green] Query executed")
|
|
132
|
+
console.print(f" Job ID: {job_id}")
|
|
133
|
+
|
|
134
|
+
except Exception as e:
|
|
135
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
136
|
+
if ctx.obj.verbose:
|
|
137
|
+
import traceback
|
|
138
|
+
traceback.print_exc()
|
|
139
|
+
raise click.Abort()
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@favorite.command("delete")
|
|
143
|
+
@click.argument("name")
|
|
144
|
+
@click.confirmation_option(prompt="Are you sure you want to delete this favorite?")
|
|
145
|
+
@click.pass_context
|
|
146
|
+
def delete_favorite(ctx, name: str) -> None:
|
|
147
|
+
"""Delete a favorite query.
|
|
148
|
+
|
|
149
|
+
Examples:
|
|
150
|
+
dremio favorite delete daily_report
|
|
151
|
+
"""
|
|
152
|
+
try:
|
|
153
|
+
manager = HistoryManager()
|
|
154
|
+
manager.delete_favorite(name)
|
|
155
|
+
console.print(f"[green]✓[/green] Favorite '{name}' deleted")
|
|
156
|
+
|
|
157
|
+
except Exception as e:
|
|
158
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
159
|
+
if ctx.obj.verbose:
|
|
160
|
+
import traceback
|
|
161
|
+
traceback.print_exc()
|
|
162
|
+
raise click.Abort()
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""Folder management commands."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import click
|
|
5
|
+
from rich.console import Console
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from dremio_cli.config import ProfileManager
|
|
9
|
+
from dremio_cli.client.factory import create_client
|
|
10
|
+
from dremio_cli.formatters.table import format_as_table
|
|
11
|
+
from dremio_cli.formatters.json import format_as_json
|
|
12
|
+
from dremio_cli.formatters.yaml import format_as_yaml
|
|
13
|
+
from dremio_cli.utils.validators import validate_path
|
|
14
|
+
|
|
15
|
+
console = Console()
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@click.group()
|
|
19
|
+
def folder() -> None:
|
|
20
|
+
"""Folder management operations."""
|
|
21
|
+
pass
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@folder.command("create")
|
|
25
|
+
@click.option("--path", required=True, help="Folder path as JSON array or slash-separated")
|
|
26
|
+
@click.option("--description", help="Folder description")
|
|
27
|
+
@click.pass_context
|
|
28
|
+
def create_folder(ctx, path: str, description: str) -> None:
|
|
29
|
+
"""Create a new folder.
|
|
30
|
+
|
|
31
|
+
Examples:
|
|
32
|
+
dremio folder create --path "Analytics/Reports"
|
|
33
|
+
dremio folder create --path '["Analytics", "Reports", "2024"]'
|
|
34
|
+
dremio folder create --path "namespace/data" --description "Data folder"
|
|
35
|
+
"""
|
|
36
|
+
try:
|
|
37
|
+
# Get profile
|
|
38
|
+
manager = ProfileManager()
|
|
39
|
+
profile_name = ctx.obj.profile_name
|
|
40
|
+
profile = manager.get_profile(profile_name)
|
|
41
|
+
|
|
42
|
+
if not profile:
|
|
43
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
44
|
+
raise click.Abort()
|
|
45
|
+
|
|
46
|
+
# Create client
|
|
47
|
+
client = create_client(profile)
|
|
48
|
+
|
|
49
|
+
# Parse path
|
|
50
|
+
folder_path = validate_path(path)
|
|
51
|
+
|
|
52
|
+
# Create folder data
|
|
53
|
+
folder_data = {
|
|
54
|
+
"path": folder_path,
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if description:
|
|
58
|
+
folder_data["description"] = description
|
|
59
|
+
|
|
60
|
+
with console.status(f"[bold green]Creating folder '{'/'.join(folder_path)}'..."):
|
|
61
|
+
result = client.create_folder(folder_data)
|
|
62
|
+
|
|
63
|
+
folder_id = result.get("id")
|
|
64
|
+
result_path = result.get("path", [])
|
|
65
|
+
|
|
66
|
+
console.print(f"[green]✓[/green] Folder created successfully")
|
|
67
|
+
console.print(f" ID: {folder_id}")
|
|
68
|
+
console.print(f" Path: {'.'.join(result_path)}")
|
|
69
|
+
|
|
70
|
+
# Display full result if verbose
|
|
71
|
+
if ctx.obj.verbose:
|
|
72
|
+
output_format = ctx.obj.output_format
|
|
73
|
+
if output_format == "json":
|
|
74
|
+
console.print(format_as_json(result))
|
|
75
|
+
elif output_format == "yaml":
|
|
76
|
+
console.print(format_as_yaml(result))
|
|
77
|
+
|
|
78
|
+
except Exception as e:
|
|
79
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
80
|
+
if ctx.obj.verbose:
|
|
81
|
+
import traceback
|
|
82
|
+
traceback.print_exc()
|
|
83
|
+
raise click.Abort()
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
@folder.command("list")
|
|
87
|
+
@click.option("--parent", help="Parent folder/space ID or path")
|
|
88
|
+
@click.pass_context
|
|
89
|
+
def list_folders(ctx, parent: str) -> None:
|
|
90
|
+
"""List folders.
|
|
91
|
+
|
|
92
|
+
Examples:
|
|
93
|
+
dremio folder list
|
|
94
|
+
dremio folder list --parent "Analytics"
|
|
95
|
+
dremio folder list --parent abc-123-def-456
|
|
96
|
+
"""
|
|
97
|
+
try:
|
|
98
|
+
# Get profile
|
|
99
|
+
manager = ProfileManager()
|
|
100
|
+
profile_name = ctx.obj.profile_name
|
|
101
|
+
profile = manager.get_profile(profile_name)
|
|
102
|
+
|
|
103
|
+
if not profile:
|
|
104
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
105
|
+
raise click.Abort()
|
|
106
|
+
|
|
107
|
+
# Create client
|
|
108
|
+
client = create_client(profile)
|
|
109
|
+
|
|
110
|
+
# Get catalog
|
|
111
|
+
with console.status(f"[bold green]Fetching folders..."):
|
|
112
|
+
data = client.get_catalog()
|
|
113
|
+
|
|
114
|
+
items = data.get("data", [])
|
|
115
|
+
|
|
116
|
+
# Filter for folders
|
|
117
|
+
folders = [
|
|
118
|
+
item for item in items
|
|
119
|
+
if item.get("containerType") == "FOLDER"
|
|
120
|
+
]
|
|
121
|
+
|
|
122
|
+
# Filter by parent if specified
|
|
123
|
+
if parent:
|
|
124
|
+
# Try to resolve parent to a path
|
|
125
|
+
try:
|
|
126
|
+
parent_item = client.get_catalog_item(parent)
|
|
127
|
+
parent_path = parent_item.get("path", [])
|
|
128
|
+
except:
|
|
129
|
+
# Assume it's a path
|
|
130
|
+
parent_path = validate_path(parent)
|
|
131
|
+
|
|
132
|
+
# Filter folders that are children of parent
|
|
133
|
+
folders = [
|
|
134
|
+
folder for folder in folders
|
|
135
|
+
if folder.get("path", [])[:len(parent_path)] == parent_path and
|
|
136
|
+
len(folder.get("path", [])) == len(parent_path) + 1
|
|
137
|
+
]
|
|
138
|
+
|
|
139
|
+
# Format output
|
|
140
|
+
output_format = ctx.obj.output_format
|
|
141
|
+
|
|
142
|
+
if output_format == "json":
|
|
143
|
+
console.print(format_as_json(folders))
|
|
144
|
+
elif output_format == "yaml":
|
|
145
|
+
console.print(format_as_yaml(folders))
|
|
146
|
+
else:
|
|
147
|
+
if folders:
|
|
148
|
+
title = f"Folders in {parent}" if parent else "All Folders"
|
|
149
|
+
format_as_table(folders, title=title)
|
|
150
|
+
else:
|
|
151
|
+
console.print("[yellow]No folders found[/yellow]")
|
|
152
|
+
|
|
153
|
+
except Exception as e:
|
|
154
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
155
|
+
if ctx.obj.verbose:
|
|
156
|
+
import traceback
|
|
157
|
+
traceback.print_exc()
|
|
158
|
+
raise click.Abort()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
@folder.command("get")
|
|
162
|
+
@click.argument("folder_id")
|
|
163
|
+
@click.pass_context
|
|
164
|
+
def get_folder(ctx, folder_id: str) -> None:
|
|
165
|
+
"""Get folder by ID.
|
|
166
|
+
|
|
167
|
+
Examples:
|
|
168
|
+
dremio folder get abc-123-def-456
|
|
169
|
+
"""
|
|
170
|
+
try:
|
|
171
|
+
# Get profile
|
|
172
|
+
manager = ProfileManager()
|
|
173
|
+
profile_name = ctx.obj.profile_name
|
|
174
|
+
profile = manager.get_profile(profile_name)
|
|
175
|
+
|
|
176
|
+
if not profile:
|
|
177
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
178
|
+
raise click.Abort()
|
|
179
|
+
|
|
180
|
+
# Create client
|
|
181
|
+
client = create_client(profile)
|
|
182
|
+
|
|
183
|
+
# Get folder
|
|
184
|
+
with console.status(f"[bold green]Fetching folder {folder_id}..."):
|
|
185
|
+
data = client.get_catalog_item(folder_id)
|
|
186
|
+
|
|
187
|
+
# Format output
|
|
188
|
+
output_format = ctx.obj.output_format
|
|
189
|
+
|
|
190
|
+
if output_format == "json":
|
|
191
|
+
console.print(format_as_json(data))
|
|
192
|
+
elif output_format == "yaml":
|
|
193
|
+
console.print(format_as_yaml(data))
|
|
194
|
+
else:
|
|
195
|
+
format_as_table(data, title=f"Folder: {folder_id}")
|
|
196
|
+
|
|
197
|
+
except Exception as e:
|
|
198
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
199
|
+
if ctx.obj.verbose:
|
|
200
|
+
import traceback
|
|
201
|
+
traceback.print_exc()
|
|
202
|
+
raise click.Abort()
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
@folder.command("get-by-path")
|
|
206
|
+
@click.argument("path")
|
|
207
|
+
@click.pass_context
|
|
208
|
+
def get_folder_by_path(ctx, path: str) -> None:
|
|
209
|
+
"""Get folder by path.
|
|
210
|
+
|
|
211
|
+
Examples:
|
|
212
|
+
dremio folder get-by-path "Analytics/Reports"
|
|
213
|
+
dremio folder get-by-path '["Analytics", "Reports", "2024"]'
|
|
214
|
+
"""
|
|
215
|
+
try:
|
|
216
|
+
# Get profile
|
|
217
|
+
manager = ProfileManager()
|
|
218
|
+
profile_name = ctx.obj.profile_name
|
|
219
|
+
profile = manager.get_profile(profile_name)
|
|
220
|
+
|
|
221
|
+
if not profile:
|
|
222
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
223
|
+
raise click.Abort()
|
|
224
|
+
|
|
225
|
+
# Create client
|
|
226
|
+
client = create_client(profile)
|
|
227
|
+
|
|
228
|
+
# Get folder by path
|
|
229
|
+
with console.status(f"[bold green]Fetching folder {path}..."):
|
|
230
|
+
data = client.get_catalog_item_by_path(path)
|
|
231
|
+
|
|
232
|
+
# Format output
|
|
233
|
+
output_format = ctx.obj.output_format
|
|
234
|
+
|
|
235
|
+
if output_format == "json":
|
|
236
|
+
console.print(format_as_json(data))
|
|
237
|
+
elif output_format == "yaml":
|
|
238
|
+
console.print(format_as_yaml(data))
|
|
239
|
+
else:
|
|
240
|
+
format_as_table(data, title=f"Folder: {path}")
|
|
241
|
+
|
|
242
|
+
except Exception as e:
|
|
243
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
244
|
+
if ctx.obj.verbose:
|
|
245
|
+
import traceback
|
|
246
|
+
traceback.print_exc()
|
|
247
|
+
raise click.Abort()
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
@folder.command("delete")
|
|
251
|
+
@click.argument("folder_id")
|
|
252
|
+
@click.option("--tag", help="Version tag for optimistic concurrency control")
|
|
253
|
+
@click.confirmation_option(prompt="Are you sure you want to delete this folder?")
|
|
254
|
+
@click.pass_context
|
|
255
|
+
def delete_folder(ctx, folder_id: str, tag: str) -> None:
|
|
256
|
+
"""Delete a folder.
|
|
257
|
+
|
|
258
|
+
Examples:
|
|
259
|
+
dremio folder delete abc-123-def-456
|
|
260
|
+
"""
|
|
261
|
+
try:
|
|
262
|
+
# Get profile
|
|
263
|
+
manager = ProfileManager()
|
|
264
|
+
profile_name = ctx.obj.profile_name
|
|
265
|
+
profile = manager.get_profile(profile_name)
|
|
266
|
+
|
|
267
|
+
if not profile:
|
|
268
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
269
|
+
raise click.Abort()
|
|
270
|
+
|
|
271
|
+
# Create client
|
|
272
|
+
client = create_client(profile)
|
|
273
|
+
|
|
274
|
+
# Get tag if not provided
|
|
275
|
+
if not tag:
|
|
276
|
+
folder = client.get_catalog_item(folder_id)
|
|
277
|
+
tag = folder.get("tag")
|
|
278
|
+
|
|
279
|
+
# Delete folder
|
|
280
|
+
with console.status(f"[bold yellow]Deleting folder {folder_id}..."):
|
|
281
|
+
client.delete_folder(folder_id, tag)
|
|
282
|
+
|
|
283
|
+
console.print(f"[green]✓[/green] Folder deleted successfully")
|
|
284
|
+
|
|
285
|
+
except Exception as e:
|
|
286
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
287
|
+
if ctx.obj.verbose:
|
|
288
|
+
import traceback
|
|
289
|
+
traceback.print_exc()
|
|
290
|
+
raise click.Abort()
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"""Grant and privilege management commands."""
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import click
|
|
5
|
+
from rich.console import Console
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from dremio_cli.config import ProfileManager
|
|
9
|
+
from dremio_cli.client.factory import create_client
|
|
10
|
+
from dremio_cli.formatters.table import format_as_table
|
|
11
|
+
from dremio_cli.formatters.json import format_as_json
|
|
12
|
+
from dremio_cli.formatters.yaml import format_as_yaml
|
|
13
|
+
|
|
14
|
+
console = Console()
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@click.group()
|
|
18
|
+
def grant() -> None:
|
|
19
|
+
"""Grant and privilege management operations."""
|
|
20
|
+
pass
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@grant.command("list")
|
|
24
|
+
@click.argument("catalog_id")
|
|
25
|
+
@click.pass_context
|
|
26
|
+
def list_grants(ctx, catalog_id: str) -> None:
|
|
27
|
+
"""List grants for a catalog object.
|
|
28
|
+
|
|
29
|
+
Examples:
|
|
30
|
+
dremio grant list abc-123-def-456
|
|
31
|
+
"""
|
|
32
|
+
try:
|
|
33
|
+
# Get profile
|
|
34
|
+
manager = ProfileManager()
|
|
35
|
+
profile_name = ctx.obj.profile_name
|
|
36
|
+
profile = manager.get_profile(profile_name)
|
|
37
|
+
|
|
38
|
+
if not profile:
|
|
39
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
40
|
+
raise click.Abort()
|
|
41
|
+
|
|
42
|
+
# Create client
|
|
43
|
+
client = create_client(profile)
|
|
44
|
+
|
|
45
|
+
# Get grants
|
|
46
|
+
with console.status(f"[bold green]Fetching grants for {catalog_id}..."):
|
|
47
|
+
data = client.list_grants(catalog_id)
|
|
48
|
+
|
|
49
|
+
# Format output
|
|
50
|
+
output_format = ctx.obj.output_format
|
|
51
|
+
|
|
52
|
+
if output_format == "json":
|
|
53
|
+
console.print(format_as_json(data))
|
|
54
|
+
elif output_format == "yaml":
|
|
55
|
+
console.print(format_as_yaml(data))
|
|
56
|
+
else:
|
|
57
|
+
grants = data.get("grants", [])
|
|
58
|
+
if grants:
|
|
59
|
+
format_as_table(grants, title=f"Grants for {catalog_id}")
|
|
60
|
+
else:
|
|
61
|
+
console.print("[yellow]No grants found[/yellow]")
|
|
62
|
+
|
|
63
|
+
except Exception as e:
|
|
64
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
65
|
+
if ctx.obj.verbose:
|
|
66
|
+
import traceback
|
|
67
|
+
traceback.print_exc()
|
|
68
|
+
raise click.Abort()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@grant.command("add")
|
|
72
|
+
@click.argument("catalog_id")
|
|
73
|
+
@click.option("--grantee-type", required=True, type=click.Choice(["USER", "ROLE"]), help="Grantee type")
|
|
74
|
+
@click.option("--grantee-id", required=True, help="User or role ID")
|
|
75
|
+
@click.option("--privileges", required=True, help="Comma-separated privileges (e.g., SELECT,ALTER,VIEW)")
|
|
76
|
+
@click.pass_context
|
|
77
|
+
def add_grant(ctx, catalog_id: str, grantee_type: str, grantee_id: str, privileges: str) -> None:
|
|
78
|
+
"""Add a grant to a catalog object.
|
|
79
|
+
|
|
80
|
+
Examples:
|
|
81
|
+
dremio grant add abc-123 --grantee-type USER --grantee-id user-456 --privileges SELECT,VIEW
|
|
82
|
+
dremio grant add abc-123 --grantee-type ROLE --grantee-id role-789 --privileges SELECT,ALTER,MODIFY
|
|
83
|
+
"""
|
|
84
|
+
try:
|
|
85
|
+
# Get profile
|
|
86
|
+
manager = ProfileManager()
|
|
87
|
+
profile_name = ctx.obj.profile_name
|
|
88
|
+
profile = manager.get_profile(profile_name)
|
|
89
|
+
|
|
90
|
+
if not profile:
|
|
91
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
92
|
+
raise click.Abort()
|
|
93
|
+
|
|
94
|
+
# Create client
|
|
95
|
+
client = create_client(profile)
|
|
96
|
+
|
|
97
|
+
# Parse privileges
|
|
98
|
+
privilege_list = [p.strip() for p in privileges.split(",")]
|
|
99
|
+
|
|
100
|
+
# Create grant data
|
|
101
|
+
grant_data = {
|
|
102
|
+
"granteeType": grantee_type,
|
|
103
|
+
"granteeId": grantee_id,
|
|
104
|
+
"privileges": privilege_list
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
# Add grant
|
|
108
|
+
with console.status(f"[bold green]Adding grant..."):
|
|
109
|
+
client.add_grant(catalog_id, grant_data)
|
|
110
|
+
|
|
111
|
+
console.print(f"[green]✓[/green] Grant added successfully")
|
|
112
|
+
console.print(f" Grantee: {grantee_type} {grantee_id}")
|
|
113
|
+
console.print(f" Privileges: {', '.join(privilege_list)}")
|
|
114
|
+
|
|
115
|
+
except Exception as e:
|
|
116
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
117
|
+
if ctx.obj.verbose:
|
|
118
|
+
import traceback
|
|
119
|
+
traceback.print_exc()
|
|
120
|
+
raise click.Abort()
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@grant.command("remove")
|
|
124
|
+
@click.argument("catalog_id")
|
|
125
|
+
@click.option("--grantee-type", required=True, type=click.Choice(["USER", "ROLE"]), help="Grantee type")
|
|
126
|
+
@click.option("--grantee-id", required=True, help="User or role ID")
|
|
127
|
+
@click.confirmation_option(prompt="Are you sure you want to remove this grant?")
|
|
128
|
+
@click.pass_context
|
|
129
|
+
def remove_grant(ctx, catalog_id: str, grantee_type: str, grantee_id: str) -> None:
|
|
130
|
+
"""Remove a grant from a catalog object.
|
|
131
|
+
|
|
132
|
+
Examples:
|
|
133
|
+
dremio grant remove abc-123 --grantee-type USER --grantee-id user-456
|
|
134
|
+
"""
|
|
135
|
+
try:
|
|
136
|
+
# Get profile
|
|
137
|
+
manager = ProfileManager()
|
|
138
|
+
profile_name = ctx.obj.profile_name
|
|
139
|
+
profile = manager.get_profile(profile_name)
|
|
140
|
+
|
|
141
|
+
if not profile:
|
|
142
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
143
|
+
raise click.Abort()
|
|
144
|
+
|
|
145
|
+
# Create client
|
|
146
|
+
client = create_client(profile)
|
|
147
|
+
|
|
148
|
+
# Remove grant
|
|
149
|
+
with console.status(f"[bold yellow]Removing grant..."):
|
|
150
|
+
client.remove_grant(catalog_id, grantee_type, grantee_id)
|
|
151
|
+
|
|
152
|
+
console.print(f"[green]✓[/green] Grant removed successfully")
|
|
153
|
+
|
|
154
|
+
except Exception as e:
|
|
155
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
156
|
+
if ctx.obj.verbose:
|
|
157
|
+
import traceback
|
|
158
|
+
traceback.print_exc()
|
|
159
|
+
raise click.Abort()
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
@grant.command("set")
|
|
163
|
+
@click.argument("catalog_id")
|
|
164
|
+
@click.option("--from-file", "grant_file", type=click.Path(exists=True), required=True, help="JSON file with grants")
|
|
165
|
+
@click.pass_context
|
|
166
|
+
def set_grants(ctx, catalog_id: str, grant_file: str) -> None:
|
|
167
|
+
"""Set all grants for a catalog object (replaces existing).
|
|
168
|
+
|
|
169
|
+
Examples:
|
|
170
|
+
dremio grant set abc-123 --from-file grants.json
|
|
171
|
+
"""
|
|
172
|
+
try:
|
|
173
|
+
# Get profile
|
|
174
|
+
manager = ProfileManager()
|
|
175
|
+
profile_name = ctx.obj.profile_name
|
|
176
|
+
profile = manager.get_profile(profile_name)
|
|
177
|
+
|
|
178
|
+
if not profile:
|
|
179
|
+
console.print(f"[red]Profile '{profile_name}' not found[/red]")
|
|
180
|
+
raise click.Abort()
|
|
181
|
+
|
|
182
|
+
# Load grants
|
|
183
|
+
with open(grant_file, 'r') as f:
|
|
184
|
+
grants_data = json.load(f)
|
|
185
|
+
|
|
186
|
+
# Create client
|
|
187
|
+
client = create_client(profile)
|
|
188
|
+
|
|
189
|
+
# Set grants
|
|
190
|
+
with console.status(f"[bold green]Setting grants..."):
|
|
191
|
+
client.set_grants(catalog_id, grants_data)
|
|
192
|
+
|
|
193
|
+
grant_count = len(grants_data.get("grants", []))
|
|
194
|
+
console.print(f"[green]✓[/green] Grants set successfully")
|
|
195
|
+
console.print(f" Total grants: {grant_count}")
|
|
196
|
+
|
|
197
|
+
except Exception as e:
|
|
198
|
+
console.print(f"[red]Error: {e}[/red]")
|
|
199
|
+
if ctx.obj.verbose:
|
|
200
|
+
import traceback
|
|
201
|
+
traceback.print_exc()
|
|
202
|
+
raise click.Abort()
|