use serde_json::{Map, Value};
use crate::error::Result;
use crate::http::HttpClient;
pub struct Policy<'a> {
http: &'a HttpClient,
memory_id: String,
}
impl<'a> Policy<'a> {
pub(crate) fn new(http: &'a HttpClient, memory_id: String) -> Self {
Self { http, memory_id }
}
pub async fn get(&self) -> Result<Value> {
let path = format!("/memories/{}/policy", self.memory_id);
self.http._get(&path, None).await
}
pub async fn resolved(&self) -> Result<Value> {
let path = format!("/memories/{}/policy/resolved", self.memory_id);
self.http._get(&path, None).await
}
pub async fn set(
&self,
add_policies: Option<&[&str]>,
remove_policies: Option<&[&str]>,
downgrade_reason: Option<&str>,
) -> Result<Value> {
let mut body = Map::new();
if let Some(a) = add_policies {
body.insert(
"add_policies".into(),
Value::Array(a.iter().map(|p| Value::String(p.to_string())).collect()),
);
}
if let Some(r) = remove_policies {
body.insert(
"remove_policies".into(),
Value::Array(r.iter().map(|p| Value::String(p.to_string())).collect()),
);
}
if let Some(d) = downgrade_reason {
body.insert("downgrade_reason".into(), Value::String(d.to_string()));
}
let path = format!("/memories/{}/policy", self.memory_id);
self.http._put(&path, Some(&Value::Object(body))).await
}
pub async fn enforcement_status(&self) -> Result<Value> {
let path = format!("/memories/{}/policy/enforcement-status", self.memory_id);
self.http._get(&path, None).await
}
pub async fn enforce(&self, opts: Option<Value>) -> Result<Value> {
let body = opts.unwrap_or_else(|| Value::Object(Map::new()));
let path = format!("/memories/{}/policy/enforce", self.memory_id);
self.http._post(&path, Some(&body)).await
}
pub async fn simulate(&self, opts: Option<Value>) -> Result<Value> {
let body = opts.unwrap_or_else(|| Value::Object(Map::new()));
let path = format!("/memories/{}/policy/simulate", self.memory_id);
self.http._post(&path, Some(&body)).await
}
pub async fn set_residency(&self, residency: Value) -> Result<Value> {
let path = format!("/memories/{}/policy/residency", self.memory_id);
self.http._put(&path, Some(&residency)).await
}
pub async fn delete_residency(&self) -> Result<()> {
let path = format!("/memories/{}/policy/residency", self.memory_id);
self.http._delete(&path).await.map(|_| ())
}
}