[pve-devel] [PATCH v3 proxmox 08/66] notify: api: add API for gotify endpoints

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jul 18 14:44:40 CEST 2023


On Mon, Jul 17, 2023 at 04:59:53PM +0200, Lukas Wagner wrote:
> Signed-off-by: Lukas Wagner <l.wagner at proxmox.com>
> ---
>  proxmox-notify/src/api/gotify.rs | 284 +++++++++++++++++++++++++++++++
>  proxmox-notify/src/api/mod.rs    |   6 +
>  2 files changed, 290 insertions(+)
>  create mode 100644 proxmox-notify/src/api/gotify.rs
> 
> diff --git a/proxmox-notify/src/api/gotify.rs b/proxmox-notify/src/api/gotify.rs
> new file mode 100644
> index 00000000..fcc1aabf
> --- /dev/null
> +++ b/proxmox-notify/src/api/gotify.rs
> @@ -0,0 +1,284 @@
> +use crate::api::ApiError;
> +use crate::endpoints::gotify::{
> +    DeleteableGotifyProperty, GotifyConfig, GotifyConfigUpdater, GotifyPrivateConfig,
> +    GotifyPrivateConfigUpdater, GOTIFY_TYPENAME,
> +};
> +use crate::Config;
> +
> +/// Get a list of all gotify endpoints.
> +///
> +/// The caller is responsible for any needed permission checks.
> +/// Returns a list of all gotify endpoints or an `ApiError` if the config is erroneous.
> +pub fn get_endpoints(config: &Config) -> Result<Vec<GotifyConfig>, ApiError> {
> +    config
> +        .config
> +        .convert_to_typed_array(GOTIFY_TYPENAME)
> +        .map_err(|e| ApiError::internal_server_error("Could not fetch endpoints", Some(e.into())))
> +}
> +
> +/// Get gotify 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<GotifyConfig, ApiError> {
> +    config
> +        .config
> +        .lookup(GOTIFY_TYPENAME, name)
> +        .map_err(|_| ApiError::not_found(format!("endpoint '{name}' not found"), None))
> +}
> +
> +/// Add a new gotify endpoint.
> +///
> +/// The caller is responsible for any needed permission checks.
> +/// The caller also responsible for locking the configuration files.
> +/// Returns an `ApiError` if an endpoint with the same name already exists,
> +/// or if the endpoint could not be saved.
> +pub fn add_endpoint(
> +    config: &mut Config,
> +    endpoint_config: &GotifyConfig,
> +    private_endpoint_config: &GotifyPrivateConfig,
> +) -> Result<(), ApiError> {
> +    if endpoint_config.name != private_endpoint_config.name {
> +        // Programming error by the user of the crate, thus we panic
> +        panic!("name for endpoint config and private config must be identical");
> +    }
> +
> +    if super::endpoint_exists(config, &endpoint_config.name) {

(*could* dedup the whole if into a helper in `super` so we don't need to
copy-pasta the message to every new endpoint - we already have this
twice now ;-) )

> +        return Err(ApiError::bad_request(
> +            format!(
> +                "endpoint with name '{}' already exists!",
> +                endpoint_config.name
> +            ),
> +            None,
> +        ));
> +    }
> +
> +    set_private_config_entry(config, private_endpoint_config)?;
> +
> +    config
> +        .config
> +        .set_data(&endpoint_config.name, GOTIFY_TYPENAME, endpoint_config)
> +        .map_err(|e| {
> +            ApiError::internal_server_error(
> +                format!("could not save endpoint '{}'", endpoint_config.name),
> +                Some(e.into()),
> +            )
> +        })?;
> +
> +    Ok(())
> +}
> +
(...)





More information about the pve-devel mailing list