aboutsummaryrefslogtreecommitdiff
path: root/src/api/admin/worker.rs
blob: 78508175be407caa8d96b84e6b050d8017a3ae23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;

use garage_model::garage::Garage;

use crate::api::*;
use crate::error::Error;
use crate::{Admin, RequestHandler};

#[async_trait]
impl RequestHandler for LocalGetWorkerVariableRequest {
	type Response = LocalGetWorkerVariableResponse;

	async fn handle(
		self,
		garage: &Arc<Garage>,
		_admin: &Admin,
	) -> Result<LocalGetWorkerVariableResponse, Error> {
		let mut res = HashMap::new();
		if let Some(k) = self.variable {
			res.insert(k.clone(), garage.bg_vars.get(&k)?);
		} else {
			let vars = garage.bg_vars.get_all();
			for (k, v) in vars.iter() {
				res.insert(k.to_string(), v.to_string());
			}
		}
		Ok(LocalGetWorkerVariableResponse(res))
	}
}

#[async_trait]
impl RequestHandler for LocalSetWorkerVariableRequest {
	type Response = LocalSetWorkerVariableResponse;

	async fn handle(
		self,
		garage: &Arc<Garage>,
		_admin: &Admin,
	) -> Result<LocalSetWorkerVariableResponse, Error> {
		garage.bg_vars.set(&self.variable, &self.value)?;

		Ok(LocalSetWorkerVariableResponse {
			variable: self.variable,
			value: self.value,
		})
	}
}