]>
Commit | Line | Data |
---|---|---|
e6604cf3 DM |
1 | use anyhow::Error; |
2 | use serde_json::Value; | |
3 | ||
4 | use proxmox::{ | |
5 | api::{ | |
6 | api, | |
7 | cli::*, | |
8 | RpcEnvironment, | |
9 | ApiHandler, | |
10 | }, | |
11 | }; | |
12 | ||
13 | use proxmox_backup::{ | |
14 | api2::{ | |
15 | self, | |
16 | types::{ | |
49c965a4 | 17 | DRIVE_NAME_SCHEMA, |
e6604cf3 DM |
18 | }, |
19 | }, | |
645a044b | 20 | tape::complete_drive_path, |
e6604cf3 DM |
21 | config::drive::{ |
22 | complete_drive_name, | |
23 | complete_changer_name, | |
24 | complete_linux_drive_name, | |
25 | }, | |
26 | }; | |
27 | ||
28 | pub fn drive_commands() -> CommandLineInterface { | |
29 | ||
30 | let cmd_def = CliCommandMap::new() | |
5605ca56 | 31 | .insert("scan", CliCommand::new(&API_METHOD_SCAN_FOR_DRIVES)) |
e6604cf3 DM |
32 | .insert("list", CliCommand::new(&API_METHOD_LIST_DRIVES)) |
33 | .insert("config", | |
34 | CliCommand::new(&API_METHOD_GET_CONFIG) | |
35 | .arg_param(&["name"]) | |
36 | .completion_cb("name", complete_linux_drive_name) | |
37 | ) | |
38 | .insert( | |
39 | "remove", | |
a3c709ef | 40 | CliCommand::new(&api2::config::drive::API_METHOD_DELETE_DRIVE) |
e6604cf3 DM |
41 | .arg_param(&["name"]) |
42 | .completion_cb("name", complete_linux_drive_name) | |
43 | ) | |
44 | .insert( | |
45 | "create", | |
a3c709ef | 46 | CliCommand::new(&api2::config::drive::API_METHOD_CREATE_DRIVE) |
e6604cf3 DM |
47 | .arg_param(&["name"]) |
48 | .completion_cb("name", complete_drive_name) | |
49 | .completion_cb("path", complete_drive_path) | |
50 | .completion_cb("changer", complete_changer_name) | |
51 | ) | |
52 | .insert( | |
53 | "update", | |
a3c709ef | 54 | CliCommand::new(&api2::config::drive::API_METHOD_UPDATE_DRIVE) |
e6604cf3 DM |
55 | .arg_param(&["name"]) |
56 | .completion_cb("name", complete_linux_drive_name) | |
57 | .completion_cb("path", complete_drive_path) | |
58 | .completion_cb("changer", complete_changer_name) | |
59 | ) | |
e6604cf3 DM |
60 | ; |
61 | ||
62 | cmd_def.into() | |
63 | } | |
64 | ||
e6604cf3 DM |
65 | #[api( |
66 | input: { | |
67 | properties: { | |
68 | "output-format": { | |
69 | schema: OUTPUT_FORMAT, | |
70 | optional: true, | |
71 | }, | |
72 | }, | |
73 | }, | |
74 | )] | |
75 | /// List drives | |
76 | fn list_drives( | |
77 | param: Value, | |
78 | rpcenv: &mut dyn RpcEnvironment, | |
79 | ) -> Result<(), Error> { | |
80 | ||
81 | let output_format = get_output_format(¶m); | |
5fdaecf6 | 82 | let info = &api2::tape::drive::API_METHOD_LIST_DRIVES; |
e6604cf3 DM |
83 | let mut data = match info.handler { |
84 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
85 | _ => unreachable!(), | |
86 | }; | |
87 | ||
88 | let options = default_table_format_options() | |
89 | .column(ColumnConfig::new("name")) | |
90 | .column(ColumnConfig::new("path")) | |
91 | .column(ColumnConfig::new("changer")) | |
92 | .column(ColumnConfig::new("vendor")) | |
93 | .column(ColumnConfig::new("model")) | |
94 | .column(ColumnConfig::new("serial")) | |
95 | ; | |
96 | ||
b2362a12 | 97 | format_and_print_result_full(&mut data, &info.returns, &output_format, &options); |
e6604cf3 DM |
98 | |
99 | Ok(()) | |
100 | } | |
101 | ||
102 | #[api( | |
103 | input: { | |
104 | properties: { | |
105 | "output-format": { | |
106 | schema: OUTPUT_FORMAT, | |
107 | optional: true, | |
108 | }, | |
109 | }, | |
110 | } | |
111 | )] | |
112 | /// Scan for drives | |
113 | fn scan_for_drives( | |
114 | param: Value, | |
115 | rpcenv: &mut dyn RpcEnvironment, | |
116 | ) -> Result<(), Error> { | |
117 | ||
118 | let output_format = get_output_format(¶m); | |
119 | let info = &api2::tape::drive::API_METHOD_SCAN_DRIVES; | |
120 | let mut data = match info.handler { | |
121 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
122 | _ => unreachable!(), | |
123 | }; | |
124 | ||
125 | let options = default_table_format_options() | |
126 | .column(ColumnConfig::new("path")) | |
127 | .column(ColumnConfig::new("vendor")) | |
128 | .column(ColumnConfig::new("model")) | |
129 | .column(ColumnConfig::new("serial")) | |
130 | ; | |
131 | ||
b2362a12 | 132 | format_and_print_result_full(&mut data, &info.returns, &output_format, &options); |
e6604cf3 DM |
133 | |
134 | Ok(()) | |
135 | } | |
136 | ||
137 | ||
138 | #[api( | |
139 | input: { | |
140 | properties: { | |
141 | "output-format": { | |
142 | schema: OUTPUT_FORMAT, | |
143 | optional: true, | |
144 | }, | |
145 | name: { | |
49c965a4 | 146 | schema: DRIVE_NAME_SCHEMA, |
e6604cf3 DM |
147 | }, |
148 | }, | |
149 | }, | |
150 | )] | |
151 | /// Get pool configuration | |
152 | fn get_config( | |
153 | param: Value, | |
154 | rpcenv: &mut dyn RpcEnvironment, | |
155 | ) -> Result<(), Error> { | |
156 | ||
157 | let output_format = get_output_format(¶m); | |
158 | let info = &api2::config::drive::API_METHOD_GET_CONFIG; | |
159 | let mut data = match info.handler { | |
160 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
161 | _ => unreachable!(), | |
162 | }; | |
163 | ||
164 | let options = default_table_format_options() | |
165 | .column(ColumnConfig::new("name")) | |
166 | .column(ColumnConfig::new("path")) | |
167 | .column(ColumnConfig::new("changer")) | |
f70d8091 | 168 | .column(ColumnConfig::new("changer-drive-id")) |
e6604cf3 DM |
169 | ; |
170 | ||
b2362a12 | 171 | format_and_print_result_full(&mut data, &info.returns, &output_format, &options); |
e6604cf3 DM |
172 | |
173 | Ok(()) | |
174 | } |