-
-
Notifications
You must be signed in to change notification settings - Fork 563
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rust): wip add "lease list" service
- Loading branch information
1 parent
b515e72
commit 5ef3ee4
Showing
12 changed files
with
254 additions
and
158 deletions.
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 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
1 change: 0 additions & 1 deletion
1
implementations/rust/ockam/ockam_api/src/cloud/lease_manager/mod.rs
This file was deleted.
Oops, something went wrong.
76 changes: 0 additions & 76 deletions
76
implementations/rust/ockam/ockam_api/src/cloud/lease_manager/models/influxdb.rs
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
implementations/rust/ockam/ockam_api/src/cloud/lease_manager/models/mod.rs
This file was deleted.
Oops, something went wrong.
This file contains 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
113 changes: 113 additions & 0 deletions
113
implementations/rust/ockam/ockam_api/src/influxdb/lease_token.rs
This file contains 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,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()); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod lease_token; | ||
pub mod token_lessor_node_service; | ||
mod token_lessor_worker; | ||
|
||
|
This file contains 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.