]> git.proxmox.com Git - proxmox-backup.git/blob - src/bin/proxmox_tape/drive.rs
f8831aec9bc8e45eedf08a92bcb7eac53105bcb8
[proxmox-backup.git] / src / bin / proxmox_tape / drive.rs
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::{
17 DRIVE_NAME_SCHEMA,
18 },
19 },
20 tape::complete_drive_path,
21 config::drive::{
22 complete_drive_name,
23 complete_changer_name,
24 complete_lto_drive_name,
25 },
26 };
27
28 pub fn drive_commands() -> CommandLineInterface {
29
30 let cmd_def = CliCommandMap::new()
31 .insert("scan", CliCommand::new(&API_METHOD_SCAN_FOR_DRIVES))
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_lto_drive_name)
37 )
38 .insert(
39 "remove",
40 CliCommand::new(&api2::config::drive::API_METHOD_DELETE_DRIVE)
41 .arg_param(&["name"])
42 .completion_cb("name", complete_lto_drive_name)
43 )
44 .insert(
45 "create",
46 CliCommand::new(&api2::config::drive::API_METHOD_CREATE_DRIVE)
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",
54 CliCommand::new(&api2::config::drive::API_METHOD_UPDATE_DRIVE)
55 .arg_param(&["name"])
56 .completion_cb("name", complete_lto_drive_name)
57 .completion_cb("path", complete_drive_path)
58 .completion_cb("changer", complete_changer_name)
59 )
60 ;
61
62 cmd_def.into()
63 }
64
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(&param);
82 let info = &api2::tape::drive::API_METHOD_LIST_DRIVES;
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
97 format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
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(&param);
119 let info = &api2::tape::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
132 format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
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: {
146 schema: DRIVE_NAME_SCHEMA,
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(&param);
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"))
168 .column(ColumnConfig::new("changer-drivenum"))
169 ;
170
171 format_and_print_result_full(&mut data, &info.returns, &output_format, &options);
172
173 Ok(())
174 }