Skip to content

Commit

Permalink
Updated REST principal API
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Feb 18, 2024
1 parent afe10e6 commit 8027f13
Show file tree
Hide file tree
Showing 13 changed files with 224 additions and 150 deletions.
58 changes: 34 additions & 24 deletions crates/cli/src/modules/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl AccountCommands {
..Default::default()
};
let account_id = client
.http_request::<u32, _>(Method::POST, "/admin/principal", Some(principal))
.http_request::<u32, _>(Method::POST, "/api/principal", Some(principal))
.await;
eprintln!("Successfully created account {name:?} with id {account_id}.");
}
Expand Down Expand Up @@ -131,7 +131,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(changes),
)
.await;
Expand All @@ -144,7 +144,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
addresses
.into_iter()
Expand All @@ -164,7 +164,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
addresses
.into_iter()
Expand All @@ -184,7 +184,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
member_of
.into_iter()
Expand All @@ -204,7 +204,7 @@ impl AccountCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
member_of
.into_iter()
Expand All @@ -224,7 +224,7 @@ impl AccountCommands {
client
.http_request::<Value, String>(
Method::DELETE,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
None,
)
.await;
Expand All @@ -233,9 +233,13 @@ impl AccountCommands {
AccountCommands::Display { name } => {
client.display_principal(&name).await;
}
AccountCommands::List { from, limit } => {
AccountCommands::List {
filter,
limit,
page,
} => {
client
.list_principals("individual", "Account", from, limit)
.list_principals("individual", "Account", filter, page, limit)
.await;
}
}
Expand All @@ -245,11 +249,7 @@ impl AccountCommands {
impl Client {
pub async fn display_principal(&self, name: &str) {
let principal = self
.http_request::<Principal, String>(
Method::GET,
&format!("/admin/principal/{name}"),
None,
)
.http_request::<Principal, String>(Method::GET, &format!("/api/principal/{name}"), None)
.await;
let mut table = Table::new();
if let Some(name) = principal.name {
Expand Down Expand Up @@ -318,31 +318,35 @@ impl Client {
&self,
record_type: &str,
record_name: &str,
from: Option<String>,
filter: Option<String>,
page: Option<usize>,
limit: Option<usize>,
) {
let mut query = form_urlencoded::Serializer::new("/admin/principal?".to_string());
let mut query = form_urlencoded::Serializer::new("/api/principal?".to_string());

query.append_pair("type", record_type);

if let Some(from) = &from {
query.append_pair("from", from);
if let Some(filter) = &filter {
query.append_pair("filter", filter);
}
if let Some(limit) = limit {
query.append_pair("limit", &limit.to_string());
}
if let Some(page) = page {
query.append_pair("page", &page.to_string());
}

let results = self
.http_request::<Vec<String>, String>(Method::GET, &query.finish(), None)
.http_request::<ListResponse, String>(Method::GET, &query.finish(), None)
.await;
if !results.is_empty() {
if !results.items.is_empty() {
let mut table = Table::new();
table.add_row(Row::new(vec![
Cell::new(&format!("{record_name} Name")).with_style(Attr::Bold)
]));

for domain in &results {
table.add_row(Row::new(vec![Cell::new(domain)]));
for item in &results.items {
table.add_row(Row::new(vec![Cell::new(item)]));
}

eprintln!();
Expand All @@ -352,13 +356,19 @@ impl Client {

eprintln!(
"\n\n{} {}{} found.\n",
results.len(),
results.total,
record_name.to_ascii_lowercase(),
if results.len() == 1 { "" } else { "s" }
if results.total == 1 { "" } else { "s" }
);
}
}

#[derive(Debug, serde::Deserialize)]
struct ListResponse {
pub total: usize,
pub items: Vec<String>,
}

impl Display for Type {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down
18 changes: 12 additions & 6 deletions crates/cli/src/modules/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,12 @@ pub enum AccountCommands {

/// List all user accounts
List {
/// Starting point for listing accounts
from: Option<String>,
/// Filter accounts by keywords
filter: Option<String>,
/// Maximum number of accounts to list
limit: Option<usize>,
/// Page number
page: Option<usize>,
},
}

Expand Down Expand Up @@ -255,10 +257,12 @@ pub enum ListCommands {

/// List all mailing lists
List {
/// Starting point for listing mailing lists
from: Option<String>,
/// Filter mailing lists by keywords
filter: Option<String>,
/// Maximum number of mailing lists to list
limit: Option<usize>,
/// Page number
page: Option<usize>,
},
}

Expand Down Expand Up @@ -320,10 +324,12 @@ pub enum GroupCommands {

/// List all groups
List {
/// Starting point for listing groups
from: Option<String>,
/// Filter groups by keywords
filter: Option<String>,
/// Maximum number of groups to list
limit: Option<usize>,
/// Page number
page: Option<usize>,
},
}

Expand Down
12 changes: 6 additions & 6 deletions crates/cli/src/modules/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ impl ServerCommands {
match self {
ServerCommands::DatabaseMaintenance {} => {
client
.http_request::<Value, String>(Method::GET, "/admin/store/maintenance", None)
.http_request::<Value, String>(Method::GET, "/api/store/maintenance", None)
.await;
eprintln!("Success.");
}
ServerCommands::ReloadCertificates {} => {
client
.http_request::<Value, String>(Method::GET, "/admin/reload/certificates", None)
.http_request::<Value, String>(Method::GET, "/api/reload/certificates", None)
.await;
eprintln!("Success.");
}
ServerCommands::ReloadConfig {} => {
client
.http_request::<Value, String>(Method::GET, "/admin/reload/config", None)
.http_request::<Value, String>(Method::GET, "/api/reload/config", None)
.await;
eprintln!("Success.");
}
ServerCommands::AddConfig { key, value } => {
client
.http_request::<Value, _>(
Method::POST,
"/admin/config",
"/api/config",
Some(vec![(key.clone(), value.unwrap_or_default())]),
)
.await;
Expand All @@ -62,7 +62,7 @@ impl ServerCommands {
client
.http_request::<Value, String>(
Method::DELETE,
&format!("/admin/config/{key}"),
&format!("/api/config/{key}"),
None,
)
.await;
Expand All @@ -72,7 +72,7 @@ impl ServerCommands {
let results = client
.http_request::<Vec<(String, String)>, String>(
Method::GET,
&format!("/admin/config/{}", prefix.unwrap_or_default()),
&format!("/api/config/{}", prefix.unwrap_or_default()),
None,
)
.await;
Expand Down
8 changes: 4 additions & 4 deletions crates/cli/src/modules/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl DomainCommands {
client
.http_request::<Value, String>(
Method::POST,
&format!("/admin/domain/{name}"),
&format!("/api/domain/{name}"),
None,
)
.await;
Expand All @@ -46,17 +46,17 @@ impl DomainCommands {
client
.http_request::<Value, String>(
Method::DELETE,
&format!("/admin/domain/{name}"),
&format!("/api/domain/{name}"),
None,
)
.await;
eprintln!("Successfully deleted domain {name:?}");
}
DomainCommands::List { from, limit } => {
let query = if from.is_none() && limit.is_none() {
Cow::Borrowed("/admin/domain")
Cow::Borrowed("/api/domain")
} else {
let mut query = "/admin/domain?".to_string();
let mut query = "/api/domain?".to_string();
if let Some(from) = &from {
query.push_str(&format!("from={from}"));
}
Expand Down
20 changes: 13 additions & 7 deletions crates/cli/src/modules/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ impl GroupCommands {
..Default::default()
};
let account_id = client
.http_request::<u32, _>(Method::POST, "/admin/principal", Some(principal))
.http_request::<u32, _>(Method::POST, "/api/principal", Some(principal))
.await;
if let Some(members) = members {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(vec![PrincipalUpdate::set(
PrincipalField::Members,
PrincipalValue::StringList(members),
Expand Down Expand Up @@ -103,7 +103,7 @@ impl GroupCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(changes),
)
.await;
Expand All @@ -116,7 +116,7 @@ impl GroupCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
members
.into_iter()
Expand All @@ -136,7 +136,7 @@ impl GroupCommands {
client
.http_request::<Value, _>(
Method::PATCH,
&format!("/admin/principal/{name}"),
&format!("/api/principal/{name}"),
Some(
members
.into_iter()
Expand All @@ -155,8 +155,14 @@ impl GroupCommands {
GroupCommands::Display { name } => {
client.display_principal(&name).await;
}
GroupCommands::List { from, limit } => {
client.list_principals("group", "Group", from, limit).await;
GroupCommands::List {
filter,
limit,
page,
} => {
client
.list_principals("group", "Group", filter, page, limit)
.await;
}
}
}
Expand Down
Loading

0 comments on commit 8027f13

Please sign in to comment.