Skip to content

Commit

Permalink
feat(rust): wip add "lease list" service
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianbenavides committed Sep 12, 2024
1 parent b515e72 commit 5ef3ee4
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 158 deletions.
1 change: 1 addition & 0 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 implementations/rust/ockam/ockam_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ serde_json = "1.0.118"
sha2 = "0.10.8"
sqlx-build-trust = { version = "0.7.8" }
strip-ansi-escapes = "0.2"
strum = { version = "0.26.3", default-features = false, features = ["derive"] }
sysinfo = "0.30"
thiserror = "1.0"
time = { version = "0.3.36", default-features = false, features = ["std", "formatting", "local-offset", "macros"] }
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion implementations/rust/ockam/ockam_api/src/cloud/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub use secure_clients::*;
pub mod addon;
pub mod email_address;
pub mod enroll;
pub mod lease_manager;
pub mod operation;
pub mod project;
pub mod secure_clients;
Expand Down
113 changes: 113 additions & 0 deletions implementations/rust/ockam/ockam_api/src/influxdb/lease_token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use crate::colors::{color_error, color_primary};
use crate::output::Output;
use crate::terminal::fmt;
use crate::ApiError;
use minicbor::{CborLen, Decode, Encode};
use ockam::identity::Identifier;
use ockam_core::compat::fmt::Error as FmtError;
use serde::{Deserialize, Serialize};
use std::cmp::PartialEq;
use std::fmt::{Display, Formatter};
use strum::{Display, EnumString};
use time::OffsetDateTime;

#[derive(Encode, Decode, CborLen, Serialize, Deserialize, Debug, PartialEq)]
#[cbor(map)]
pub struct LeaseToken {
#[cbor(n(1))]
pub id: String,

#[cbor(n(2))]
pub issued_for: Identifier,

#[cbor(n(3))]
pub created_at: i64,

#[cbor(n(4))]
pub expires_at: i64,

#[cbor(n(5))]
pub token: String,

#[cbor(n(6))]
pub status: TokenStatus,
}

impl LeaseToken {
pub fn is_active(&self) -> bool {
self.status == TokenStatus::Active
}

pub fn expires_at(&self) -> ockam_core::Result<OffsetDateTime> {
OffsetDateTime::from_unix_timestamp(self.expires_at)
.map_err(|e| ApiError::core(e.to_string()))
}

pub fn is_expired(&self) -> ockam_core::Result<bool> {
Ok(self.expires_at()? < OffsetDateTime::now_utc())
}
}

impl Display for LeaseToken {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{}", color_primary(&self.id))?;
writeln!(f, "{}Token: {}", fmt::INDENTATION, color_primary(&self.id))?;

let expires_at = self.expires_at().map_err(|_| FmtError)?.to_string();
let expiration_time = if self.is_expired().map_err(|_| FmtError)? {
format!("Expired at {}", color_error(&expires_at))
} else {
format!("Expires at {}", color_primary(&expires_at))
};
let status = if self.is_active() {
color_primary(self.status.to_string())
} else {
color_error(self.status.to_string())
};
writeln!(f, "{}{expiration_time} ({status})", fmt::INDENTATION)?;
Ok(())
}
}

impl Output for LeaseToken {
fn item(&self) -> crate::Result<String> {
Ok(self.padded_display())
}
}

#[derive(
Encode, Decode, CborLen, Serialize, Deserialize, PartialEq, Debug, EnumString, Display,
)]
pub enum TokenStatus {
#[n(0)]
#[strum(serialize = "active")]
Active,

#[n(1)]
#[strum(serialize = "inactive")]
Revoked,
}

#[cfg(test)]
mod tests {
use super::*;
use std::str::FromStr;

#[test]
fn lease_token_display() {
let token = LeaseToken {
id: "token_id".to_string(),
issued_for: Identifier::from_str(
"I0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
)
.unwrap(),
created_at: OffsetDateTime::now_utc().unix_timestamp(),
expires_at: OffsetDateTime::now_utc().unix_timestamp(),
token: "token".to_string(),
status: TokenStatus::Active,
};
assert!(token.expires_at().is_ok());
assert!(token.is_expired().is_ok());
assert!(token.item().is_ok());
}
}
1 change: 1 addition & 0 deletions implementations/rust/ockam/ockam_api/src/influxdb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod lease_token;
pub mod token_lessor_node_service;
mod token_lessor_worker;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::cloud::lease_manager::models::influxdb::LeaseToken;
use crate::influxdb::token_lessor_worker::InfluxDBTokenLessorWorker;
use crate::nodes::models::services::{DeleteServiceRequest, StartServiceRequest};
use crate::nodes::service::messages::Messages;
Expand All @@ -12,6 +11,7 @@ use ockam_core::{async_trait, Address};
use ockam_multiaddr::MultiAddr;
use ockam_node::{Context, WorkerBuilder};
use serde::{Deserialize, Serialize};
use crate::influxdb::lease_token::LeaseToken;

impl NodeManagerWorker {
pub(crate) async fn start_influxdb_token_lease_manager_service(
Expand Down
Loading

0 comments on commit 5ef3ee4

Please sign in to comment.