use manta_backend_dispatcher::error::Error;
use manta_backend_dispatcher::interfaces::hsm::redfish_endpoint::RedfishEndpointTrait;
use manta_backend_dispatcher::types::hsm::inventory::{
RedfishEndpoint, RedfishEndpointArray,
};
use crate::{
server::common::{app_context::InfraContext, jwt_ops},
service::authorization::validate_user_group_members_access,
};
pub use manta_shared::types::api::redfish_endpoints::{
GetRedfishEndpointsParams, UpdateRedfishEndpointParams,
};
pub(crate) fn params_to_redfish_endpoint(
params: UpdateRedfishEndpointParams,
) -> RedfishEndpoint {
RedfishEndpoint {
id: params.id,
name: params.name,
hostname: params.hostname,
domain: params.domain,
fqdn: params.fqdn,
enabled: Some(params.enabled),
user: params.user,
password: params.password,
use_ssdp: Some(params.use_ssdp),
mac_required: Some(params.mac_required),
mac_addr: params.mac_addr,
ip_address: params.ip_address,
rediscover_on_update: Some(params.rediscover_on_update),
template_id: params.template_id,
r#type: None,
uuid: None,
discovery_info: None,
}
}
pub async fn get_redfish_endpoints(
infra: &InfraContext<'_>,
token: &str,
params: &GetRedfishEndpointsParams,
) -> Result<RedfishEndpointArray, Error> {
tracing::info!("Get Redfish endpoints");
if !jwt_ops::is_user_admin(token) {
let Some(xname) = params.id.as_deref() else {
return Err(Error::BadRequest(
"Non-admin callers must scope a Redfish-endpoints query by `id`."
.to_string(),
));
};
validate_user_group_members_access(infra, token, &[xname.to_string()])
.await?;
}
infra
.backend
.get_redfish_endpoints(
token,
params.id.as_deref(),
params.fqdn.as_deref(),
None,
params.uuid.as_deref(),
params.macaddr.as_deref(),
params.ipaddress.as_deref(),
None,
)
.await
}
pub async fn add_redfish_endpoint(
infra: &InfraContext<'_>,
token: &str,
params: UpdateRedfishEndpointParams,
) -> Result<(), Error> {
tracing::info!("Add Redfish endpoint id={}", params.id);
validate_user_group_members_access(
infra,
token,
std::slice::from_ref(¶ms.id),
)
.await?;
let endpoint = params_to_redfish_endpoint(params);
let array = RedfishEndpointArray {
redfish_endpoints: Some(vec![endpoint]),
};
infra.backend.add_redfish_endpoint(token, &array).await
}
pub async fn update_redfish_endpoint(
infra: &InfraContext<'_>,
token: &str,
params: UpdateRedfishEndpointParams,
) -> Result<(), Error> {
tracing::info!("Update Redfish endpoint id={}", params.id);
validate_user_group_members_access(
infra,
token,
std::slice::from_ref(¶ms.id),
)
.await?;
let endpoint = params_to_redfish_endpoint(params);
infra
.backend
.update_redfish_endpoint(token, &endpoint)
.await
}
pub async fn delete_redfish_endpoint(
infra: &InfraContext<'_>,
token: &str,
id: &str,
) -> Result<(), Error> {
tracing::info!("Delete Redfish endpoint id={}", id);
validate_user_group_members_access(infra, token, &[id.to_string()]).await?;
infra
.backend
.delete_redfish_endpoint(token, id)
.await
.map(|_| ())
}