|
1 | 1 | use super::{MANAGED_BY, MANAGED_BY_LABEL};
|
2 | 2 | use crate::permissionables::Session;
|
3 |
| -use k8s_openapi::api::core::v1::ConfigMap; |
| 3 | +use itertools::Itertools; |
| 4 | +use k8s_openapi::api::{core::v1::ConfigMap, rbac::v1::Subject}; |
4 | 5 | use kube::{
|
5 | 6 | api::{ObjectMeta, Patch, PatchParams},
|
6 | 7 | Api, Client,
|
@@ -31,6 +32,21 @@ pub async fn create_configmap(
|
31 | 32 | "members".to_string(),
|
32 | 33 | serde_json::to_string(&session.members)?,
|
33 | 34 | ),
|
| 35 | + ( |
| 36 | + "member_refs".to_string(), |
| 37 | + serde_json::to_string( |
| 38 | + &session |
| 39 | + .members |
| 40 | + .iter() |
| 41 | + .map(|member| Subject { |
| 42 | + kind: "User".to_string(), |
| 43 | + name: format!("oidc:{member}"), |
| 44 | + ..Default::default() |
| 45 | + }) |
| 46 | + .collect_vec(), |
| 47 | + ) |
| 48 | + .unwrap(), |
| 49 | + ), |
34 | 50 | ("start_date".to_string(), session.start_date.to_string()),
|
35 | 51 | ("end_date".to_string(), session.end_date.to_string()),
|
36 | 52 | ]);
|
@@ -71,3 +87,99 @@ pub async fn create_configmap(
|
71 | 87 | info!("ConfigMap {NAME} created / updated for {namespace}");
|
72 | 88 | Ok(())
|
73 | 89 | }
|
| 90 | + |
| 91 | +#[cfg(test)] |
| 92 | +mod tests { |
| 93 | + use super::create_configmap; |
| 94 | + use crate::{instruments::Instrument, permissionables::Session}; |
| 95 | + use k8s_openapi::api::{core::v1::ConfigMap, rbac::v1::Subject}; |
| 96 | + use kube::{api::ObjectMeta, Client, Config}; |
| 97 | + use std::collections::{BTreeMap, BTreeSet}; |
| 98 | + use time::macros::datetime; |
| 99 | + use wiremock::{ |
| 100 | + matchers::{body_partial_json, method, path, query_param}, |
| 101 | + Mock, MockServer, ResponseTemplate, |
| 102 | + }; |
| 103 | + |
| 104 | + #[tokio::test] |
| 105 | + async fn create_new_configmap() { |
| 106 | + let server = MockServer::start().await; |
| 107 | + let configmap = ConfigMap { |
| 108 | + metadata: ObjectMeta { |
| 109 | + name: Some("sessionspaces".to_string()), |
| 110 | + ..Default::default() |
| 111 | + }, |
| 112 | + data: Some(BTreeMap::from([ |
| 113 | + ("proposal_code".to_string(), "cm".to_string()), |
| 114 | + ("proposal_number".to_string(), "37235".to_string()), |
| 115 | + ("visit".to_string(), "3".to_string()), |
| 116 | + ("instrument".to_string(), "i03".to_string()), |
| 117 | + ( |
| 118 | + "members".to_string(), |
| 119 | + serde_json::to_string(&vec![&"enu43627", &"iat69393", &"mrg27357"]).unwrap(), |
| 120 | + ), |
| 121 | + ( |
| 122 | + "member_refs".to_string(), |
| 123 | + serde_json::to_string(&vec![ |
| 124 | + Subject { |
| 125 | + kind: "User".to_string(), |
| 126 | + name: "oidc:enu43627".to_string(), |
| 127 | + ..Default::default() |
| 128 | + }, |
| 129 | + Subject { |
| 130 | + kind: "User".to_string(), |
| 131 | + name: "oidc:iat69393".to_string(), |
| 132 | + ..Default::default() |
| 133 | + }, |
| 134 | + Subject { |
| 135 | + kind: "User".to_string(), |
| 136 | + name: "oidc:mrg27357".to_string(), |
| 137 | + ..Default::default() |
| 138 | + }, |
| 139 | + ]) |
| 140 | + .unwrap(), |
| 141 | + ), |
| 142 | + ( |
| 143 | + "start_date".to_string(), |
| 144 | + datetime!(2024-05-24 09:00:00).to_string(), |
| 145 | + ), |
| 146 | + ( |
| 147 | + "end_date".to_string(), |
| 148 | + datetime!(2024-08-09 09:00:00).to_string(), |
| 149 | + ), |
| 150 | + ])), |
| 151 | + ..Default::default() |
| 152 | + }; |
| 153 | + let _mock = Mock::given(method("PATCH")) |
| 154 | + .and(path( |
| 155 | + "/api/v1/namespaces/cm37235-3/configmaps/sessionspaces", |
| 156 | + )) |
| 157 | + .and(query_param("fieldManager", "sessionspaces")) |
| 158 | + .and(body_partial_json(configmap.clone())) |
| 159 | + .respond_with(ResponseTemplate::new(201).set_body_json(configmap)) |
| 160 | + .mount(&server) |
| 161 | + .await; |
| 162 | + let config = Config::new(server.uri().parse().unwrap()); |
| 163 | + let k8s_client = Client::try_from(config).unwrap(); |
| 164 | + create_configmap( |
| 165 | + "cm37235-3", |
| 166 | + Session { |
| 167 | + proposal_code: "cm".to_string(), |
| 168 | + proposal_number: 37235, |
| 169 | + visit: 3, |
| 170 | + instrument: Instrument::I03, |
| 171 | + members: BTreeSet::from([ |
| 172 | + "enu43627".to_string(), |
| 173 | + "iat69393".to_string(), |
| 174 | + "mrg27357".to_string(), |
| 175 | + ]), |
| 176 | + gid: Some(161025), |
| 177 | + start_date: datetime!(2024-05-24 09:00:00), |
| 178 | + end_date: datetime!(2024-08-09 09:00:00), |
| 179 | + }, |
| 180 | + k8s_client, |
| 181 | + ) |
| 182 | + .await |
| 183 | + .unwrap(); |
| 184 | + } |
| 185 | +} |
0 commit comments