[pve-devel] [PATCH v3 proxmox 06/66] notify: api: add API for sendmail endpoints
Wolfgang Bumiller
w.bumiller at proxmox.com
Tue Jul 18 14:36:20 CEST 2023
On Mon, Jul 17, 2023 at 04:59:51PM +0200, Lukas Wagner wrote:
> Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
> ---
> proxmox-notify/src/api/mod.rs | 7 +
> proxmox-notify/src/api/sendmail.rs | 254 +++++++++++++++++++++++++++++
> 2 files changed, 261 insertions(+)
> create mode 100644 proxmox-notify/src/api/sendmail.rs
>
> diff --git a/proxmox-notify/src/api/mod.rs b/proxmox-notify/src/api/mod.rs
> index db9ad1ca..4baae899 100644
> --- a/proxmox-notify/src/api/mod.rs
> +++ b/proxmox-notify/src/api/mod.rs
> @@ -5,6 +5,8 @@ use crate::Config;
> use serde::Serialize;
>
> pub mod common;
> +#[cfg(feature = "sendmail")]
> +pub mod sendmail;
>
> #[derive(Debug, Serialize)]
> pub struct ApiError {
> @@ -83,6 +85,11 @@ fn verify_digest(config: &Config, digest: Option<&[u8]>) -> Result<(), ApiError>
> fn endpoint_exists(config: &Config, name: &str) -> bool {
> let mut exists = false;
>
> + #[cfg(feature = "sendmail")]
> + {
> + exists = exists || sendmail::get_endpoint(config, name).is_ok();
> + }
> +
> exists
> }
>
> diff --git a/proxmox-notify/src/api/sendmail.rs b/proxmox-notify/src/api/sendmail.rs
> new file mode 100644
> index 00000000..8eafe359
> --- /dev/null
> +++ b/proxmox-notify/src/api/sendmail.rs
> @@ -0,0 +1,254 @@
> +use crate::api::ApiError;
> +use crate::endpoints::sendmail::{
> + DeleteableSendmailProperty, SendmailConfig, SendmailConfigUpdater, SENDMAIL_TYPENAME,
> +};
> +use crate::Config;
> +
> +/// Get a list of all sendmail endpoints.
> +///
> +/// The caller is responsible for any needed permission checks.
> +/// Returns a list of all sendmail endpoints or an `ApiError` if the config is erroneous.
> +pub fn get_endpoints(config: &Config) -> Result<Vec<SendmailConfig>, ApiError> {
> + config
> + .config
> + .convert_to_typed_array(SENDMAIL_TYPENAME)
> + .map_err(|e| ApiError::internal_server_error("Could not fetch endpoints", Some(e.into())))
> +}
> +
> +/// Get sendmail endpoint with given `name`.
> +///
> +/// The caller is responsible for any needed permission checks.
> +/// Returns the endpoint or an `ApiError` if the endpoint was not found.
> +pub fn get_endpoint(config: &Config, name: &str) -> Result<SendmailConfig, ApiError> {
> + config
> + .config
> + .lookup(SENDMAIL_TYPENAME, name)
> + .map_err(|_| ApiError::not_found(format!("endpoint '{name}' not found"), None))
^ Technically `.lookup()` could have found the name but as a wrong type.
It might make sense to not use `.lookup()` in this case.
> +}
> +
(...)
More information about the pve-devel
mailing list