]>
Commit | Line | Data |
---|---|---|
380bd7df DM |
1 | use anyhow::Error; |
2 | use serde_json::Value; | |
3 | ||
6ef1b649 WB |
4 | use proxmox_router::{cli::*, ApiHandler, RpcEnvironment}; |
5 | use proxmox_schema::api; | |
380bd7df | 6 | |
01a08021 | 7 | use pbs_client::view_task_result; |
e7d4be9d | 8 | use pbs_api_types::{DataStoreConfig, DATASTORE_SCHEMA}; |
2b7f8dd5 | 9 | |
e7d4be9d | 10 | use proxmox_backup::api2; |
01a08021 | 11 | use proxmox_backup::client_helpers::connect_to_localhost; |
380bd7df DM |
12 | |
13 | #[api( | |
14 | input: { | |
15 | properties: { | |
16 | "output-format": { | |
17 | schema: OUTPUT_FORMAT, | |
18 | optional: true, | |
19 | }, | |
20 | } | |
21 | } | |
22 | )] | |
23 | /// Datastore list. | |
24 | fn list_datastores(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
25 | ||
26 | let output_format = get_output_format(¶m); | |
27 | ||
28 | let info = &api2::config::datastore::API_METHOD_LIST_DATASTORES; | |
29 | let mut data = match info.handler { | |
30 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
31 | _ => unreachable!(), | |
32 | }; | |
33 | ||
34 | let options = default_table_format_options() | |
35 | .column(ColumnConfig::new("name")) | |
36 | .column(ColumnConfig::new("path")) | |
37 | .column(ColumnConfig::new("comment")); | |
38 | ||
b2362a12 | 39 | format_and_print_result_full(&mut data, &info.returns, &output_format, &options); |
380bd7df DM |
40 | |
41 | Ok(Value::Null) | |
42 | } | |
43 | ||
44 | #[api( | |
45 | input: { | |
46 | properties: { | |
47 | name: { | |
48 | schema: DATASTORE_SCHEMA, | |
49 | }, | |
50 | "output-format": { | |
51 | schema: OUTPUT_FORMAT, | |
52 | optional: true, | |
53 | }, | |
54 | } | |
55 | } | |
56 | )] | |
57 | /// Show datastore configuration | |
58 | fn show_datastore(param: Value, rpcenv: &mut dyn RpcEnvironment) -> Result<Value, Error> { | |
59 | ||
60 | let output_format = get_output_format(¶m); | |
61 | ||
62 | let info = &api2::config::datastore::API_METHOD_READ_DATASTORE; | |
63 | let mut data = match info.handler { | |
64 | ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, | |
65 | _ => unreachable!(), | |
66 | }; | |
67 | ||
68 | let options = default_table_format_options(); | |
b2362a12 | 69 | format_and_print_result_full(&mut data, &info.returns, &output_format, &options); |
380bd7df DM |
70 | |
71 | Ok(Value::Null) | |
72 | } | |
73 | ||
81c767ef DC |
74 | #[api( |
75 | protected: true, | |
76 | input: { | |
77 | properties: { | |
68e77657 DM |
78 | config: { |
79 | type: DataStoreConfig, | |
80 | flatten: true, | |
81c767ef DC |
81 | }, |
82 | "output-format": { | |
83 | schema: OUTPUT_FORMAT, | |
84 | optional: true, | |
85 | }, | |
86 | }, | |
87 | }, | |
88 | )] | |
89 | /// Create new datastore config. | |
90 | async fn create_datastore(mut param: Value) -> Result<Value, Error> { | |
91 | ||
92 | let output_format = extract_output_format(&mut param); | |
93 | ||
94 | let mut client = connect_to_localhost()?; | |
95 | ||
96 | let result = client.post(&"api2/json/config/datastore", Some(param)).await?; | |
97 | ||
98 | view_task_result(&mut client, result, &output_format).await?; | |
99 | ||
100 | Ok(Value::Null) | |
101 | } | |
102 | ||
380bd7df DM |
103 | pub fn datastore_commands() -> CommandLineInterface { |
104 | ||
105 | let cmd_def = CliCommandMap::new() | |
106 | .insert("list", CliCommand::new(&API_METHOD_LIST_DATASTORES)) | |
107 | .insert("show", | |
108 | CliCommand::new(&API_METHOD_SHOW_DATASTORE) | |
109 | .arg_param(&["name"]) | |
e7d4be9d | 110 | .completion_cb("name", pbs_config::datastore::complete_datastore_name) |
380bd7df DM |
111 | ) |
112 | .insert("create", | |
81c767ef | 113 | CliCommand::new(&API_METHOD_CREATE_DATASTORE) |
380bd7df DM |
114 | .arg_param(&["name", "path"]) |
115 | ) | |
116 | .insert("update", | |
117 | CliCommand::new(&api2::config::datastore::API_METHOD_UPDATE_DATASTORE) | |
118 | .arg_param(&["name"]) | |
e7d4be9d DM |
119 | .completion_cb("name", pbs_config::datastore::complete_datastore_name) |
120 | .completion_cb("gc-schedule", pbs_config::datastore::complete_calendar_event) | |
121 | .completion_cb("prune-schedule", pbs_config::datastore::complete_calendar_event) | |
355c055e | 122 | ) |
380bd7df DM |
123 | .insert("remove", |
124 | CliCommand::new(&api2::config::datastore::API_METHOD_DELETE_DATASTORE) | |
125 | .arg_param(&["name"]) | |
e7d4be9d | 126 | .completion_cb("name", pbs_config::datastore::complete_datastore_name) |
380bd7df DM |
127 | ); |
128 | ||
129 | cmd_def.into() | |
130 | } |