Skip to content

Commit

Permalink
Merge pull request #23 from anuket-project/iol-dev
Browse files Browse the repository at this point in the history
11/19/2024 Deploy
  • Loading branch information
jfchoquette authored Nov 19, 2024
2 parents 0bb3ba6 + 5b418c5 commit 704c59d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ pub struct NotificationConfig {

pub admin_send_to_email: Option<Email>,
pub templates_directory: String,
pub vpn_config_path: PathBuf,
}
#[derive(Debug, Deserialize, Clone)]
pub struct CobblerConfig {
Expand Down
79 changes: 54 additions & 25 deletions crates/notifications/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ use std::{
use tera::Tera;
pub mod email;

use common::prelude::{anyhow, chrono::{self, Utc}, futures, itertools::Itertools, serde_json::json, tracing};
use common::prelude::{
anyhow,
chrono::{self, Utc},
futures,
itertools::Itertools,
serde_json::json,
tracing,
};
use config::{settings, RenderTarget, Situation};

pub type Username = String;
Expand Down Expand Up @@ -101,7 +108,7 @@ pub async fn send_booking_notification(
situation: Situation,
owner_title: String,
collab_title: String,
extra: Option<serde_json::Value>
extra: Option<serde_json::Value>,
) -> Result<(), Vec<anyhow::Error>> {
let users = info
.collaborators
Expand Down Expand Up @@ -230,14 +237,18 @@ pub async fn send_test_email(
}
}

pub async fn booking_started(env: &Env, info: &BookingInfo, extra: Option<serde_json::Value>) -> Result<(), Vec<anyhow::Error>> {
pub async fn booking_started(
env: &Env,
info: &BookingInfo,
extra: Option<serde_json::Value>,
) -> Result<(), Vec<anyhow::Error>> {
send_booking_notification(
env,
info,
Situation::BookingCreated,
"You Have Created a New Booking.".to_owned(),
"You Have Been Added To a New Booking.".to_owned(),
extra
extra,
)
.await
}
Expand All @@ -251,7 +262,7 @@ pub async fn booking_ending(env: &Env, info: &BookingInfo) -> Result<(), Vec<any
"A Booking You Collaborate On Is About to Expire.".to_owned(),
Some(json!({
"days": (info.end_date.unwrap_or(Utc::now()) - Utc::now()).num_days()
}))
})),
)
.await
}
Expand All @@ -263,7 +274,7 @@ pub async fn booking_ended(env: &Env, info: &BookingInfo) -> Result<(), Vec<anyh
Situation::BookingExpired,
"Your Booking Has Expired.".to_owned(),
"A Booking You Collaborate On Has Expired.".to_owned(),
None
None,
)
.await
}
Expand Down Expand Up @@ -353,7 +364,6 @@ pub async fn request_booking_extension(
extension_date: &String,
extension_reason: &String,
) -> Result<(), Vec<anyhow::Error>> {

let styles = read_styles(
settings()
.projects
Expand Down Expand Up @@ -393,10 +403,21 @@ pub async fn request_booking_extension(
context.insert("extension_reason", extension_reason);
context.insert("extension_date", extension_date);

tracing::error!("it needs to get send to {}", config::settings().notifications.admin_send_to_email.clone().expect("expected admin email address").as_address_string());
tracing::error!(
"it needs to get send to {}",
config::settings()
.notifications
.admin_send_to_email
.clone()
.expect("expected admin email address")
.as_address_string()
);

let notification = Notification {
title: format!("Booking Extension Request ({} - {})", info.project, info.purpose),
title: format!(
"Booking Extension Request ({} - {})",
info.project, info.purpose
),
send_to: format!("N/A"), // Ignored by the send_to_admins_email_template() function.
by_methods: vec![Method::Email()],
situation: Situation::RequestBookingExtension,
Expand All @@ -405,16 +426,14 @@ pub async fn request_booking_extension(
attachment: None,
};

match send_to_admins_email_template(env, notification).await {
Ok(_) => {
Ok(())
}
Err(e) => {
tracing::error!("Failed to send email with error {e:#?}");
Err(vec![e])
match send_to_admins_email_template(env, notification).await {
Ok(_) => Ok(()),
Err(e) => {
tracing::error!("Failed to send email with error {e:#?}");
Err(vec![e])
}
}
}
}

/// Send email containing ipa username, temp password, openvpn config, and instructions
pub async fn send_new_account_notification(
Expand All @@ -433,8 +452,16 @@ pub async fn send_new_account_notification(
}))
.expect("Expected to create context for notification"),
attachment: Some(AttachmentInfo {
name: "os-vpn-client.ovpn".to_owned(),
path: PathBuf::from("./config_data/os-vpn-client.ovpn"),
name: settings()
.notifications
.vpn_config_path
.file_name()
.expect("expected a file name")
.to_str()
.expect("expected a string")
.to_owned(),

path: settings().notifications.vpn_config_path.clone(),
}),
};

Expand Down Expand Up @@ -479,12 +506,14 @@ pub struct AccessInfo {
// }

static TERA: once_cell::sync::Lazy<tera::Tera> =
once_cell::sync::Lazy::new(|| match Tera::new(&settings().notifications.templates_directory) {
Ok(t) => t,
Err(e) => {
panic!("Couldn't create templating instance, failure: {e}")
}
});
once_cell::sync::Lazy::new(
|| match Tera::new(&settings().notifications.templates_directory) {
Ok(t) => t,
Err(e) => {
panic!("Couldn't create templating instance, failure: {e}")
}
},
);

pub mod templates {
pub fn retrieve(
Expand Down
3 changes: 2 additions & 1 deletion sample_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ notifications:
username: example
domain: mail.com
templates_directory: templates/**/*.html
vpn_config_path: /etc/laas-reflab/os-vpn-client.ovpn

cobbler:
url: http://cobbler.example.com/cobbler_api
Expand Down Expand Up @@ -99,4 +100,4 @@ projects:
email: ""
phone: ""
is_dynamic: false
dashboard_url: https://example.iol.unh.edu/
dashboard_url: https://example.iol.unh.edu/

0 comments on commit 704c59d

Please sign in to comment.