Implement base operator #1

Merged
pierre merged 21 commits from operator into master 2023-05-08 16:20:17 +00:00
Showing only changes of commit c90e46f038 - Show all commits

View File

@ -25,6 +25,8 @@ enum MinioError {
SetRetentionFailed, SetRetentionFailed,
#[error("Failed to set policy!")] #[error("Failed to set policy!")]
ApplyPolicyFailed, ApplyPolicyFailed,
#[error("Failed to create user!")]
CreateUserFailed,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -110,6 +112,12 @@ struct PolicyInfo {
Policy: serde_json::Value, Policy: serde_json::Value,
} }
#[allow(non_snake_case)]
#[derive(Debug, Clone, Deserialize)]
struct MinioUserListRes {
accessKey: String,
}
impl BasicMinioResult { impl BasicMinioResult {
pub fn success(&self) -> bool { pub fn success(&self) -> bool {
self.status == "success" self.status == "success"
@ -474,11 +482,42 @@ impl MinioService {
Ok(serde_json::to_string(&policy.policyInfo.Policy)?) Ok(serde_json::to_string(&policy.policyInfo.Policy)?)
} }
/// Apply a user
pub async fn user_apply(&self, user: &MinioUser) -> anyhow::Result<()> {
let res = self
.exec_mc_cmd::<BasicMinioResult>(&[
"admin",
"user",
"add",
MC_ALIAS_NAME,
user.username.as_str(),
user.password.as_str(),
])
.await?;
if res.get(0).map(|r| r.success()) != Some(true) {
return Err(MinioError::CreateUserFailed.into());
}
Ok(())
}
/// Get the list of users
pub async fn user_list(&self) -> anyhow::Result<Vec<String>> {
Ok(self
.exec_mc_cmd::<MinioUserListRes>(&["admin", "user", "list", MC_ALIAS_NAME])
.await?
.iter()
.map(|p| p.accessKey.to_string())
.collect())
}
} }
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::crd::{BucketRetention, MinioBucketSpec, RetentionType}; use crate::crd::{BucketRetention, MinioBucketSpec, RetentionType};
use crate::minio::MinioUser;
use crate::minio_test_server::MinioTestServer; use crate::minio_test_server::MinioTestServer;
const TEST_BUCKET_NAME: &str = "mybucket"; const TEST_BUCKET_NAME: &str = "mybucket";
@ -943,14 +982,52 @@ mod test {
assert_ne!(policy_1, policy_2); assert_ne!(policy_1, policy_2);
assert!(!service.policy_list().await.unwrap().contains(&TEST_POLICY_NAME.to_string())); assert!(!service
.policy_list()
.await
.unwrap()
.contains(&TEST_POLICY_NAME.to_string()));
service.policy_apply(TEST_POLICY_NAME, &policy_1).await.unwrap(); service
assert!(service.policy_list().await.unwrap().contains(&TEST_POLICY_NAME.to_string())); .policy_apply(TEST_POLICY_NAME, &policy_1)
assert_eq!(unify_policy(&service.policy_content(TEST_POLICY_NAME).await.unwrap()), policy_1); .await
.unwrap();
assert!(service
.policy_list()
.await
.unwrap()
.contains(&TEST_POLICY_NAME.to_string()));
assert_eq!(
unify_policy(&service.policy_content(TEST_POLICY_NAME).await.unwrap()),
policy_1
);
service.policy_apply(TEST_POLICY_NAME, &policy_2).await.unwrap(); service
assert!(service.policy_list().await.unwrap().contains(&TEST_POLICY_NAME.to_string())); .policy_apply(TEST_POLICY_NAME, &policy_2)
assert_eq!(unify_policy(&service.policy_content(TEST_POLICY_NAME).await.unwrap()), policy_2); .await
.unwrap();
assert!(service
.policy_list()
.await
.unwrap()
.contains(&TEST_POLICY_NAME.to_string()));
assert_eq!(
unify_policy(&service.policy_content(TEST_POLICY_NAME).await.unwrap()),
policy_2
);
}
#[tokio::test]
async fn policy_user() {
let _ = env_logger::builder().is_test(true).try_init();
let srv = MinioTestServer::start().await.unwrap();
let service = srv.as_service();
let user = MinioUser::gen_random();
assert!(!service.user_list().await.unwrap().contains(&user.username));
service.user_apply(&user).await.unwrap();
assert!(service.user_list().await.unwrap().contains(&user.username));
} }
} }