-
Notifications
You must be signed in to change notification settings - Fork 5
feat(backend): added test sessionspaces for development #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TBThomas56
wants to merge
2
commits into
main
Choose a base branch
from
tbt/test-namespaces
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -161,4 +161,6 @@ pub enum Instrument { | |
| S03, | ||
| #[strum(serialize = "s04")] | ||
| S04, | ||
| #[strum(serialize = "t01")] | ||
| T01, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
backend/sessionspaces/src/permissionables/static_sessions.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| use super::Session; | ||
| use crate::{instruments::Instrument, permissionables::Sessions}; | ||
| use serde::Deserialize; | ||
| use std::{collections::BTreeSet, path::Path, str::FromStr}; | ||
| use time::{macros::format_description, PrimitiveDateTime}; | ||
|
|
||
| /// Deserialises an [`Instrument`] from its string representation (e.g. `"i03"`). | ||
| fn deserialize_instrument<'de, D>(deserializer: D) -> Result<Instrument, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| let s = String::deserialize(deserializer)?; | ||
| Instrument::from_str(&s).map_err(serde::de::Error::custom) | ||
| } | ||
|
|
||
| /// Deserialises a [`PrimitiveDateTime`] from `"YYYY-MM-DD HH:MM:SS"` format. | ||
| fn deserialize_datetime<'de, D>(deserializer: D) -> Result<PrimitiveDateTime, D::Error> | ||
| where | ||
| D: serde::Deserializer<'de>, | ||
| { | ||
| let s = String::deserialize(deserializer)?; | ||
| let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"); | ||
| PrimitiveDateTime::parse(&s, format).map_err(serde::de::Error::custom) | ||
| } | ||
|
|
||
| /// A single statically-defined visit session, deserialised from the config file. | ||
| /// Field names and semantics are identical to those of [`Session`], except `visits` accepts | ||
| /// multiple visit numbers so one entry can produce several namespaces sharing the same members. | ||
| #[derive(Debug, Deserialize)] | ||
| struct StaticSessionEntry { | ||
| /// The two-letter prefix code associated with the proposal (e.g. `"ks"`). | ||
| proposal_code: String, | ||
| /// The unique number of the proposal. | ||
| proposal_number: u32, | ||
| /// One or more visit numbers. Each produces a separate namespace (`{code}{number}-{visit}`). | ||
| visits: Vec<u32>, | ||
| /// The instrument with which the session is associated. | ||
| #[serde(deserialize_with = "deserialize_instrument")] | ||
| instrument: Instrument, | ||
| /// Set of usernames granted access. Defaults to empty if omitted. | ||
| #[serde(default)] | ||
| members: BTreeSet<String>, | ||
| /// Posix GID of the session group. `null` if no LDAP group exists for this session. | ||
| gid: Option<u32>, | ||
| /// Session start date and time. | ||
| #[serde(deserialize_with = "deserialize_datetime")] | ||
| start_date: PrimitiveDateTime, | ||
| /// Session end date and time. | ||
| #[serde(deserialize_with = "deserialize_datetime")] | ||
| end_date: PrimitiveDateTime, | ||
| } | ||
|
|
||
| impl StaticSessionEntry { | ||
| /// Expands this entry into one [`Session`] per visit number. | ||
| fn into_sessions(self) -> impl Iterator<Item = (String, Session)> { | ||
| self.visits.into_iter().map(move |visit| { | ||
| let name = format!("{}{}-{}", self.proposal_code, self.proposal_number, visit); | ||
| let session = Session { | ||
| proposal_code: self.proposal_code.clone(), | ||
| proposal_number: self.proposal_number, | ||
| visit, | ||
| instrument: self.instrument, | ||
| members: self.members.clone(), | ||
| gid: self.gid, | ||
| start_date: self.start_date, | ||
| end_date: self.end_date, | ||
| }; | ||
| (name, session) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| /// A set of statically-configured sessions that emulate ISPyB-driven visit namespaces. | ||
| /// Loaded once at startup from a JSON file (an array of session objects). On each reconcile | ||
| /// tick these sessions are merged into the live [`Sessions`] map so they pass through the | ||
| /// exact same create / update / delete lifecycle as real visits. | ||
| #[derive(Debug, Default, Clone)] | ||
| pub struct StaticSessions(Sessions); | ||
|
|
||
| impl StaticSessions { | ||
| pub fn from_path(path: &Path) -> Result<Self, anyhow::Error> { | ||
| let content = std::fs::read_to_string(path)?; | ||
| let entries: Vec<StaticSessionEntry> = serde_json::from_str(&content)?; | ||
| let mut sessions = Sessions::default(); | ||
| for entry in entries { | ||
| for (name, session) in entry.into_sessions() { | ||
| sessions.insert(name, session); | ||
| } | ||
| } | ||
| Ok(Self(sessions)) | ||
| } | ||
|
|
||
| /// Inserts each static session into `target`, using the same namespace naming scheme as | ||
| /// ISPyB sessions (`{proposal_code}{proposal_number}-{visit}`). Real ISPyB sessions with | ||
| /// the same name take precedence; static entries are skipped if the name already exists. | ||
| pub fn merge_into(&self, target: &mut Sessions) { | ||
| for (name, session) in self.0.iter() { | ||
| target | ||
| .entry(name.clone()) | ||
| .or_insert_with(|| session.clone()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| staticSessions: | ||
| enabled: true | ||
| sessions: | ||
| - proposal_code: ks | ||
| proposal_number: 10000 | ||
| visits: [1, 2, 3, 4, 5] | ||
| instrument: t01 | ||
| members: [] | ||
|
davehadley marked this conversation as resolved.
|
||
| gid: 123456 | ||
| start_date: "2024-01-01 00:00:00" | ||
| end_date: "2099-12-31 23:59:59" | ||
|
|
||
| image: | ||
| repository: ghcr.io/diamondlightsource/workflows-sessionspaces | ||
| pullPolicy: Always | ||
| imagePullSecrets: [] | ||
| tag: "dev" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
charts/sessionspaces/templates/static-sessions-configmap.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| {{- if $.Values.staticSessions.enabled }} | ||
| apiVersion: v1 | ||
| kind: ConfigMap | ||
| metadata: | ||
| name: {{ include "common.names.fullname" $ }}-static-sessions | ||
| namespace: {{ .Release.Namespace }} | ||
| labels: | ||
| {{- include "common.labels.standard" $ | nindent 4 }} | ||
| data: | ||
| static-sessions.json: {{ $.Values.staticSessions.sessions | toJson | quote }} | ||
| {{- end }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.