use axum::{
extract::{Path, State},
http::HeaderMap,
response::Response,
};
use crate::model::{NotifiersResponse, ObjectType, TopicsResponse};
use super::super::{ApiError, AppState, auth::Scope, notify::Topics};
use super::{Ctx, ok, parse_id};
pub async fn describe(
State(state): State<AppState>,
ctx: Ctx,
headers: HeaderMap,
) -> Result<Response, ApiError> {
ctx.require_read()?;
let response = NotifiersResponse {
webhook: state.notifier.handles(crate::vtn::notify::Channel::Webhook),
mqtt: state.config.mqtt.clone(),
};
ok(&state, &headers, &response)
}
fn topics(state: &AppState) -> Topics {
Topics::new(state.config.mqtt_topic_prefix.clone())
}
fn require_mqtt(state: &AppState) -> Result<(), ApiError> {
if state.config.mqtt.is_some() {
Ok(())
} else {
Err(ApiError::NotImplemented(
"this VTN does not offer an MQTT notifier; see GET /notifiers".into(),
))
}
}
macro_rules! collection_topics {
($name:ident, $object_type:expr, $scope:expr, $doc:literal) => {
#[doc = $doc]
pub async fn $name(
State(state): State<AppState>,
ctx: Ctx,
headers: HeaderMap,
) -> Result<Response, ApiError> {
require_mqtt(&state)?;
ctx.require($scope)?;
let t = topics(&state);
ok(
&state,
&headers,
&TopicsResponse::under(&t.collection($object_type)),
)
}
};
}
collection_topics!(
programs,
ObjectType::Program,
Scope::ReadAll,
"`GET /notifiers/mqtt/topics/programs`"
);
collection_topics!(
events,
ObjectType::Event,
Scope::ReadBl,
"`GET /notifiers/mqtt/topics/events`"
);
collection_topics!(
reports,
ObjectType::Report,
Scope::ReadBl,
"`GET /notifiers/mqtt/topics/reports`"
);
collection_topics!(
subscriptions,
ObjectType::Subscription,
Scope::ReadBl,
"`GET /notifiers/mqtt/topics/subscriptions`"
);
collection_topics!(
vens,
ObjectType::Ven,
Scope::ReadBl,
"`GET /notifiers/mqtt/topics/vens`"
);
collection_topics!(
resources,
ObjectType::Resource,
Scope::ReadBl,
"`GET /notifiers/mqtt/topics/resources`"
);
pub async fn program_by_id(
State(state): State<AppState>,
ctx: Ctx,
headers: HeaderMap,
Path(id): Path<String>,
) -> Result<Response, ApiError> {
require_mqtt(&state)?;
ctx.require(Scope::ReadAll)?;
let id = parse_id(&id)?;
let _ = state.storage.get_program(&id).await?;
let t = topics(&state);
ok(
&state,
&headers,
&TopicsResponse::under_without_create(&t.object(ObjectType::Program, &id)),
)
}
pub async fn program_events(
State(state): State<AppState>,
ctx: Ctx,
headers: HeaderMap,
Path(id): Path<String>,
) -> Result<Response, ApiError> {
require_mqtt(&state)?;
ctx.require(Scope::ReadAll)?;
let id = parse_id(&id)?;
let _ = state.storage.get_program(&id).await?;
let t = topics(&state);
ok(
&state,
&headers,
&TopicsResponse::under(&t.program_events(&id)),
)
}
pub async fn ven_by_id(
State(state): State<AppState>,
ctx: Ctx,
headers: HeaderMap,
Path(id): Path<String>,
) -> Result<Response, ApiError> {
require_mqtt(&state)?;
let id = parse_id(&id)?;
require_own_ven(&state, &ctx, &id).await?;
let t = topics(&state);
ok(
&state,
&headers,
&TopicsResponse::under_without_create(&t.ven_scoped(ObjectType::Ven, &id)),
)
}
macro_rules! ven_scoped_topics {
($name:ident, $object_type:expr, $doc:literal) => {
#[doc = $doc]
pub async fn $name(
State(state): State<AppState>,
ctx: Ctx,
headers: HeaderMap,
Path(id): Path<String>,
) -> Result<Response, ApiError> {
require_mqtt(&state)?;
let id = parse_id(&id)?;
require_own_ven(&state, &ctx, &id).await?;
let t = topics(&state);
ok(
&state,
&headers,
&TopicsResponse::under(&t.ven_scoped($object_type, &id)),
)
}
};
}
ven_scoped_topics!(
ven_events,
ObjectType::Event,
"`GET /notifiers/mqtt/topics/vens/{id}/events`"
);
ven_scoped_topics!(
ven_programs,
ObjectType::Program,
"`GET /notifiers/mqtt/topics/vens/{id}/programs`"
);
ven_scoped_topics!(
ven_resources,
ObjectType::Resource,
"`GET /notifiers/mqtt/topics/vens/{id}/resources`"
);
ven_scoped_topics!(
ven_reports,
ObjectType::Report,
"`GET /notifiers/mqtt/topics/vens/{id}/reports}` — an extension; see the module note."
);
ven_scoped_topics!(
ven_subscriptions,
ObjectType::Subscription,
"`GET /notifiers/mqtt/topics/vens/{id}/subscriptions` — an extension; see the module note."
);
async fn require_own_ven(
state: &AppState,
ctx: &Ctx,
ven_id: &crate::model::ObjectId,
) -> Result<(), ApiError> {
if !ctx.is_business_logic() {
ctx.require(Scope::ReadVenObjects)?;
}
let ven = state.storage.get_ven(ven_id).await?;
if ctx.is_business_logic() {
return Ok(());
}
if ctx.principal.client_id.as_ref() == Some(&ven.client_id) {
return Ok(());
}
Err(ApiError::Storage(
super::super::store::StorageError::NotFound {
object_type: ObjectType::Ven,
id: ven_id.clone(),
},
))
}