Skip to content

Commit

Permalink
feat: adjust enroll logic and output for the new subscription plans
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Nov 22, 2024
1 parent 0fcfdec commit 4d05c78
Show file tree
Hide file tree
Showing 25 changed files with 277 additions and 128 deletions.
129 changes: 92 additions & 37 deletions implementations/rust/ockam/ockam_api/src/cloud/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use ockam_node::Context;
use crate::cloud::email_address::EmailAddress;
use crate::cloud::project::models::AdminInfo;
use crate::cloud::project::{Project, ProjectsOrchestratorApi};
use crate::cloud::subscription::Subscription;
use crate::cloud::subscription::{Subscription, SUBSCRIPTION_PAGE};
use crate::cloud::{ControllerClient, HasSecureClient};
use crate::colors::{color_primary, color_uri, color_warn};
use crate::colors::{color_primary, color_uri, color_warn, OckamColor};
use crate::fmt_log;
use crate::nodes::InMemoryNode;
use crate::output::{comma_separated, Output};
Expand All @@ -39,8 +39,11 @@ impl Space {
self.name.clone()
}

pub fn has_subscription(&self) -> bool {
self.subscription.is_some()
pub fn has_valid_subscription(&self) -> bool {
self.subscription
.as_ref()
.map(|s| s.is_valid())
.unwrap_or(false)
}

pub fn is_in_free_trial_subscription(&self) -> bool {
Expand All @@ -52,46 +55,44 @@ impl Space {
.unwrap_or_default()
}

pub fn subscription_status_message(&self, space_is_new: bool) -> crate::Result<String> {
pub fn subscription_status_message(&self) -> crate::Result<String> {
use colorful::{Colorful, RGB};

let mut f = String::new();
if let Some(subscription) = &self.subscription {
let color = match subscription.name.as_str() {
"gold" => RGB::new(255, 215, 0),
"silver" => RGB::new(230, 232, 250),
"bronze" => RGB::new(140, 120, 83),
_ => OckamColor::PrimaryResource.color(),
};
let trial_text = if subscription.is_free_trial {
" (trial)"
} else {
""
};
writeln!(
f,
"{}",
fmt_log!(
"This Space has a {} Subscription attached to it.",
color_primary(&subscription.name)
"This Space has a {}{} Subscription attached to it.",
subscription.name.clone().color(color),
trial_text
)
)?;
if let (Some(start_date), Some(end_date)) =
(&subscription.start_date(), &subscription.end_date())
{
writeln!(
f,
"{}",
fmt_log!("Started on {} and will end on {}.", start_date, end_date)
)?;
}
if subscription.is_free_trial {
if space_is_new {
writeln!(f)?;
writeln!(f, "{}", fmt_log!("As a courtesy, we created a temporary Space for you, so you can continue to build.\n"))?;
writeln!(
f,
"{}",
fmt_log!(
"Please subscribe to an Ockam plan within two weeks {}",
color_uri("https://www.ockam.io/pricing")
)
)?;
writeln!(f, "{}", fmt_log!("{}", color_warn("If you don't subscribe in that time, your Space and all associated Projects will be permanently deleted.")))?;
} else if let (Some(start_date), Some(end_date)) =
(&subscription.start_date(), &subscription.end_date())
{
writeln!(f)?;
writeln!(
f,
"{}",
fmt_log!(
"Your free trial started on {} and will end on {}.\n",
start_date,
end_date
)
)?;
writeln!(f, "{}", fmt_log!("Please subscribe to an Ockam plan before the trial ends to avoid any service interruptions {}", color_uri("https://www.ockam.io/pricing")))?;
writeln!(f, "{}", fmt_log!("{}", color_warn("If you don't subscribe in that time, your Space and all associated Projects will be permanently deleted.")))?;
}
writeln!(f)?;
writeln!(f, "{}", fmt_log!("Please subscribe to an Ockam plan before the trial ends to avoid any service interruptions {}", color_uri(SUBSCRIPTION_PAGE)))?;
writeln!(f, "{}", fmt_log!("{}", color_warn("If you don't subscribe in that time, your Space and all associated Projects will be permanently deleted.")))?;
}
} else {
writeln!(
Expand Down Expand Up @@ -362,10 +363,11 @@ impl ControllerClient {

#[cfg(test)]
pub mod tests {
use quickcheck::{quickcheck, Arbitrary, Gen, TestResult};

use crate::cloud::space::CreateSpace;
use crate::schema::tests::validate_with_schema;
use quickcheck::{quickcheck, Arbitrary, Gen, TestResult};
use time::format_description::well_known::Iso8601;
use time::OffsetDateTime;

use super::*;

Expand Down Expand Up @@ -402,4 +404,57 @@ pub mod tests {
}
}
}

#[test]
fn valid_subscription_check() {
let mut g = Gen::new(100);
let mut space = Space::arbitrary(&mut g);

// No subscription
space.subscription = None;
assert!(!space.has_valid_subscription());

// Paid subscription
let mut sub = Subscription::arbitrary(&mut g);
sub.is_free_trial = false;
space.subscription = Some(sub.clone());
assert!(space.has_valid_subscription());

// Trial subscription with no dates
let mut sub = Subscription::arbitrary(&mut g);
sub.is_free_trial = true;
sub.start_date = None;
sub.end_date = None;
space.subscription = Some(sub.clone());
assert!(!space.has_valid_subscription());

// Trial subscription with date values
sub.start_date = Some(
(OffsetDateTime::now_utc() - time::Duration::days(1))
.format(&Iso8601::DEFAULT)
.unwrap()
.to_string(),
);
sub.end_date = None;
space.subscription = Some(sub.clone());
assert!(!space.has_valid_subscription());

sub.end_date = Some(
(OffsetDateTime::now_utc() - time::Duration::hours(1))
.format(&Iso8601::DEFAULT)
.unwrap()
.to_string(),
);
space.subscription = Some(sub.clone());
assert!(!space.has_valid_subscription());

sub.end_date = Some(
(OffsetDateTime::now_utc() + time::Duration::hours(1))
.format(&Iso8601::DEFAULT)
.unwrap()
.to_string(),
);
space.subscription = Some(sub.clone());
assert!(space.has_valid_subscription());
}
}
21 changes: 18 additions & 3 deletions implementations/rust/ockam/ockam_api/src/cloud/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use ockam_node::Context;
const TARGET: &str = "ockam_api::cloud::subscription";
const API_SERVICE: &str = "subscriptions";

pub const SUBSCRIPTION_PAGE: &str = "https://orchestrator.ockam.io";

#[derive(Encode, Decode, CborLen, Debug)]
#[cfg_attr(test, derive(Clone))]
#[rustfmt::skip]
Expand Down Expand Up @@ -58,15 +60,15 @@ impl ActivateSubscription {
#[cbor(map)]
pub struct Subscription {
#[n(1)]
pub name: String,
pub name: String, // TODO: Use enum for known values?
#[n(2)]
pub is_free_trial: bool,
#[n(3)]
pub marketplace: Option<String>,
#[n(4)]
start_date: Option<String>,
pub(crate) start_date: Option<String>,
#[n(5)]
end_date: Option<String>,
pub(crate) end_date: Option<String>,
}

impl PartialEq for Subscription {
Expand Down Expand Up @@ -116,6 +118,19 @@ impl Subscription {
.as_ref()
.and_then(|date| parse_date(date).ok())
}

/// A subscription is valid if:
/// - is in a trial period and end date is in the future
/// - a plan is not in trial status
pub fn is_valid(&self) -> bool {
if self.is_free_trial {
self.end_date()
.map(|end_date| end_date >= OffsetDateTime::now_utc())
.unwrap_or(false)
} else {
true
}
}
}

impl Display for Subscription {
Expand Down
4 changes: 2 additions & 2 deletions implementations/rust/ockam/ockam_api/src/ui/terminal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ impl<W: TerminalWriter + Debug> Terminal<W> {
self.stderr.is_tty() && self.can_write_to_stderr()
}

pub fn progress_bar(&self) -> Option<ProgressBar> {
pub fn spinner(&self) -> Option<ProgressBar> {
if !self.can_use_progress_bar() {
return None;
}
Expand Down Expand Up @@ -424,7 +424,7 @@ impl<W: TerminalWriter + Debug> Terminal<W> {
if output_messages.is_empty() {
return Ok(());
}
let pb = match self.progress_bar() {
let pb = match self.spinner() {
Some(pb) => pb,
None => return Ok(()),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl<T: TerminalWriter + Debug + Send + 'static> NotificationHandler<T> {
Notification::Progress(contents) => {
if self.terminal.can_use_progress_bar() {
if self.progress_bar.is_none() {
self.progress_bar = self.terminal.progress_bar();
self.progress_bar = self.terminal.spinner();
}
if let Some(pb) = self.progress_bar.as_ref() {
pb.set_message(contents);
Expand Down
Loading

0 comments on commit 4d05c78

Please sign in to comment.