Implement POST /indexes-swap

This commit is contained in:
Loïc Lecrenier 2022-10-17 16:30:18 +02:00 committed by Clément Renault
parent bdb3702510
commit 14a44776f6
No known key found for this signature in database
GPG key ID: 92ADA4E935E71FA4
18 changed files with 463 additions and 64 deletions

View file

@ -0,0 +1,77 @@
use std::collections::HashSet;
use crate::extractors::authentication::{policies::*, GuardedData};
use crate::extractors::sequential_extractor::SeqHandler;
use crate::routes::tasks::TaskView;
use actix_web::web::Data;
use actix_web::{web, HttpResponse};
use index_scheduler::IndexScheduler;
use meilisearch_types::error::{Code, ResponseError};
use meilisearch_types::tasks::KindWithContent;
use serde::Deserialize;
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(web::resource("").route(web::post().to(SeqHandler(indexes_swap))));
}
// TODO: Lo: revisit this struct once we have decided on what the payload should be
#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct IndexesSwapPayload {
indexes: (String, String),
}
pub async fn indexes_swap(
index_scheduler: GuardedData<ActionPolicy<{ actions::INDEXES_SWAP }>, Data<IndexScheduler>>,
params: web::Json<Vec<IndexesSwapPayload>>,
) -> Result<HttpResponse, ResponseError> {
let search_rules = &index_scheduler.filters().search_rules;
// TODO: Lo: error when the params are empty
// TODO: Lo: error when the same index appears more than once
// TODO: Lo: error when not authorized to swap
let mut swaps = vec![];
let mut indexes_set = HashSet::<String>::default();
for IndexesSwapPayload {
indexes: (lhs, rhs),
} in params.into_inner().into_iter()
{
if !search_rules.is_index_authorized(&lhs) || !search_rules.is_index_authorized(&lhs) {
return Err(ResponseError::from_msg(
"TODO: error message when we swap with an index were not allowed to access"
.to_owned(),
Code::BadRequest,
));
}
swaps.push((lhs.clone(), rhs.clone()));
// TODO: Lo: should this check be here or within the index scheduler?
let is_unique_index_lhs = indexes_set.insert(lhs);
if !is_unique_index_lhs {
return Err(ResponseError::from_msg(
"TODO: error message when same index is in more than one swap".to_owned(),
Code::BadRequest,
));
}
let is_unique_index_rhs = indexes_set.insert(rhs);
if !is_unique_index_rhs {
return Err(ResponseError::from_msg(
"TODO: error message when same index is in more than one swap".to_owned(),
Code::BadRequest,
));
}
}
if swaps.is_empty() {
return Err(ResponseError::from_msg(
"TODO: error message when swaps is empty".to_owned(),
Code::BadRequest,
));
}
let task = KindWithContent::IndexSwap { swaps };
let task = index_scheduler.register(task)?;
let task_view = TaskView::from_task(&task);
Ok(HttpResponse::Accepted().json(task_view))
}

View file

@ -20,6 +20,7 @@ use self::indexes::IndexStats;
mod api_key;
mod dump;
pub mod indexes;
mod indexes_swap;
mod tasks;
pub fn configure(cfg: &mut web::ServiceConfig) {
@ -29,7 +30,8 @@ pub fn configure(cfg: &mut web::ServiceConfig) {
.service(web::scope("/dumps").configure(dump::configure))
.service(web::resource("/stats").route(web::get().to(get_stats)))
.service(web::resource("/version").route(web::get().to(get_version)))
.service(web::scope("/indexes").configure(indexes::configure));
.service(web::scope("/indexes").configure(indexes::configure))
.service(web::scope("indexes-swap").configure(indexes_swap::configure));
}
/// Extracts the raw values from the `StarOr` types and

View file

@ -71,12 +71,10 @@ pub struct TaskView {
}
impl TaskView {
fn from_task(task: &Task) -> TaskView {
pub fn from_task(task: &Task) -> TaskView {
TaskView {
uid: task.uid,
index_uid: task
.indexes()
.and_then(|vec| vec.first().map(|i| i.to_string())),
index_uid: task.index_uid().map(ToOwned::to_owned),
status: task.status,
kind: task.kind.as_kind(),
canceled_by: task.canceled_by,
@ -119,6 +117,8 @@ pub struct DetailsView {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(flatten)]
pub settings: Option<Settings<Unchecked>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub indexes: Option<Vec<(String, String)>>,
}
impl From<Details> for DetailsView {
@ -176,6 +176,10 @@ impl From<Details> for DetailsView {
dump_uid: Some(dump_uid),
..DetailsView::default()
},
Details::IndexSwap { swaps } => DetailsView {
indexes: Some(swaps),
..Default::default()
},
}
}
}

View file

@ -22,6 +22,7 @@ pub static AUTHORIZATIONS: Lazy<HashMap<(&'static str, &'static str), HashSet<&'
("PATCH", "/indexes/products/") => hashset!{"indexes.update", "indexes.*", "*"},
("GET", "/indexes/products/") => hashset!{"indexes.get", "indexes.*", "*"},
("DELETE", "/indexes/products/") => hashset!{"indexes.delete", "indexes.*", "*"},
("POST", "/indexes-swap") => hashset!{"indexes.swap", "indexes.*", "*"},
("POST", "/indexes") => hashset!{"indexes.create", "indexes.*", "*"},
("GET", "/indexes") => hashset!{"indexes.get", "indexes.*", "*"},
("GET", "/indexes/products/settings") => hashset!{"settings.get", "settings.*", "*"},