]>
Commit | Line | Data |
---|---|---|
09989d99 | 1 | use anyhow::Error; |
a8a20e92 | 2 | use ::serde::{Deserialize, Serialize}; |
09989d99 | 3 | |
6ef1b649 WB |
4 | use proxmox_router::{Permission, Router, RpcEnvironment}; |
5 | use proxmox_schema::api; | |
09989d99 | 6 | |
8cc3760e DM |
7 | use pbs_api_types::{NODE_SCHEMA, PRIV_SYS_AUDIT, PRIV_SYS_MODIFY}; |
8 | ||
440472cb | 9 | use crate::api2::node::apt::update_apt_proxy_config; |
2732c474 | 10 | use crate::config::node::{NodeConfig, NodeConfigUpdater}; |
09989d99 WB |
11 | |
12 | pub const ROUTER: Router = Router::new() | |
13 | .get(&API_METHOD_GET_NODE_CONFIG) | |
14 | .put(&API_METHOD_UPDATE_NODE_CONFIG); | |
15 | ||
16 | #[api( | |
17 | input: { | |
18 | properties: { | |
19 | node: { schema: NODE_SCHEMA }, | |
20 | }, | |
21 | }, | |
22 | access: { | |
2732c474 DM |
23 | permission: &Permission::Privilege(&["system"], PRIV_SYS_AUDIT, false), |
24 | }, | |
25 | returns: { | |
26 | type: NodeConfig, | |
09989d99 WB |
27 | }, |
28 | )] | |
2732c474 DM |
29 | /// Get the node configuration |
30 | pub fn get_node_config(mut rpcenv: &mut dyn RpcEnvironment) -> Result<NodeConfig, Error> { | |
09989d99 WB |
31 | let (config, digest) = crate::config::node::config()?; |
32 | rpcenv["digest"] = proxmox::tools::digest_to_hex(&digest).into(); | |
2732c474 | 33 | Ok(config) |
09989d99 WB |
34 | } |
35 | ||
a8a20e92 DM |
36 | #[api()] |
37 | #[derive(Serialize, Deserialize)] | |
38 | #[serde(rename_all="kebab-case")] | |
39 | #[allow(non_camel_case_types)] | |
40 | /// Deletable property name | |
41 | pub enum DeletableProperty { | |
42 | /// Delete the acme property. | |
43 | acme, | |
44 | /// Delete the acmedomain0 property. | |
45 | acmedomain0, | |
46 | /// Delete the acmedomain1 property. | |
47 | acmedomain1, | |
48 | /// Delete the acmedomain2 property. | |
49 | acmedomain2, | |
50 | /// Delete the acmedomain3 property. | |
51 | acmedomain3, | |
52 | /// Delete the acmedomain4 property. | |
53 | acmedomain4, | |
54 | /// Delete the http-proxy property. | |
55 | http_proxy, | |
56 | } | |
57 | ||
09989d99 WB |
58 | #[api( |
59 | input: { | |
60 | properties: { | |
61 | node: { schema: NODE_SCHEMA }, | |
62 | digest: { | |
63 | description: "Digest to protect against concurrent updates", | |
64 | optional: true, | |
65 | }, | |
a8a20e92 | 66 | update: { |
09989d99 WB |
67 | type: NodeConfigUpdater, |
68 | flatten: true, | |
69 | }, | |
70 | delete: { | |
a8a20e92 DM |
71 | description: "List of properties to delete.", |
72 | type: Array, | |
09989d99 | 73 | optional: true, |
a8a20e92 DM |
74 | items: { |
75 | type: DeletableProperty, | |
76 | } | |
09989d99 WB |
77 | }, |
78 | }, | |
79 | }, | |
80 | access: { | |
81 | permission: &Permission::Privilege(&["system"], PRIV_SYS_MODIFY, false), | |
82 | }, | |
83 | protected: true, | |
84 | )] | |
2732c474 | 85 | /// Update the node configuration |
09989d99 | 86 | pub fn update_node_config( |
a8a20e92 DM |
87 | // node: String, // not used |
88 | update: NodeConfigUpdater, | |
89 | delete: Option<Vec<DeletableProperty>>, | |
09989d99 WB |
90 | digest: Option<String>, |
91 | ) -> Result<(), Error> { | |
92 | let _lock = crate::config::node::lock()?; | |
93 | let (mut config, expected_digest) = crate::config::node::config()?; | |
94 | if let Some(digest) = digest { | |
95 | // FIXME: GUI doesn't handle our non-inlined digest part here properly... | |
96 | if !digest.is_empty() { | |
97 | let digest = proxmox::tools::hex_to_digest(&digest)?; | |
98 | crate::tools::detect_modified_configuration_file(&digest, &expected_digest)?; | |
99 | } | |
100 | } | |
101 | ||
a8a20e92 DM |
102 | if let Some(delete) = delete { |
103 | for delete_prop in delete { | |
104 | match delete_prop { | |
105 | DeletableProperty::acme => { config.acme = None; }, | |
106 | DeletableProperty::acmedomain0 => { config.acmedomain0 = None; }, | |
107 | DeletableProperty::acmedomain1 => { config.acmedomain1 = None; }, | |
108 | DeletableProperty::acmedomain2 => { config.acmedomain2 = None; }, | |
109 | DeletableProperty::acmedomain3 => { config.acmedomain3 = None; }, | |
110 | DeletableProperty::acmedomain4 => { config.acmedomain4 = None; }, | |
111 | DeletableProperty::http_proxy => { config.http_proxy = None; }, | |
112 | } | |
113 | } | |
114 | } | |
09989d99 | 115 | |
a8a20e92 DM |
116 | if update.acme.is_some() { config.acme = update.acme; } |
117 | if update.acmedomain0.is_some() { config.acmedomain0 = update.acmedomain0; } | |
118 | if update.acmedomain1.is_some() { config.acmedomain1 = update.acmedomain1; } | |
119 | if update.acmedomain2.is_some() { config.acmedomain2 = update.acmedomain2; } | |
120 | if update.acmedomain3.is_some() { config.acmedomain3 = update.acmedomain3; } | |
121 | if update.acmedomain4.is_some() { config.acmedomain4 = update.acmedomain4; } | |
122 | if update.http_proxy.is_some() { config.http_proxy = update.http_proxy; } | |
09989d99 | 123 | |
440472cb DM |
124 | crate::config::node::save_config(&config)?; |
125 | ||
126 | update_apt_proxy_config(config.http_proxy().as_ref())?; | |
127 | ||
128 | Ok(()) | |
09989d99 | 129 | } |