-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
806a2d6
commit 1f6565b
Showing
9 changed files
with
235 additions
and
23 deletions.
There are no files selected for viewing
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
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,2 @@ | ||
//! Application administration API. | ||
pub mod v1; |
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,156 @@ | ||
use super::data::*; | ||
use crate::error::ClientError; | ||
use crate::openid::TokenProvider; | ||
use crate::util::Client; | ||
use std::fmt::Debug; | ||
use tracing::instrument; | ||
use url::Url; | ||
|
||
/// A device registry client, backed by reqwest. | ||
#[derive(Clone, Debug)] | ||
pub struct AdminClient<TP> | ||
where | ||
TP: TokenProvider, | ||
{ | ||
client: reqwest::Client, | ||
api_url: Url, | ||
token_provider: TP, | ||
} | ||
|
||
enum AdministrationOperation { | ||
Transfer, | ||
Accept, | ||
Members, | ||
} | ||
|
||
type ClientResult<T> = Result<T, ClientError<reqwest::Error>>; | ||
|
||
impl<TP> Client<TP> for AdminClient<TP> | ||
where | ||
TP: TokenProvider, | ||
{ | ||
/// Create a new client instance. | ||
fn new(client: reqwest::Client, api_url: Url, token_provider: TP) -> Self { | ||
Self { | ||
client, | ||
api_url, | ||
token_provider, | ||
} | ||
} | ||
|
||
fn client(&self) -> &reqwest::Client { | ||
&self.client | ||
} | ||
|
||
fn token_provider(&self) -> &TP { | ||
&self.token_provider | ||
} | ||
} | ||
|
||
impl<TP> AdminClient<TP> | ||
where | ||
TP: TokenProvider, | ||
{ | ||
fn url(&self, application: &str, operation: AdministrationOperation) -> ClientResult<Url> { | ||
let mut url = self.api_url.clone(); | ||
|
||
{ | ||
let mut path = url | ||
.path_segments_mut() | ||
.map_err(|_| ClientError::Request("Failed to get paths".into()))?; | ||
|
||
path.extend(&["api", "admin", "v1alpha1", "apps"]); | ||
if !application.is_empty() { | ||
path.push(application); | ||
} | ||
match operation { | ||
AdministrationOperation::Transfer => path.push("transfer-ownership"), | ||
AdministrationOperation::Accept => path.push("accept-ownership"), | ||
AdministrationOperation::Members => path.push("members"), | ||
}; | ||
} | ||
|
||
Ok(url) | ||
} | ||
|
||
/// Get the application members and their roles | ||
#[instrument] | ||
pub async fn get_members<A>(&self, application: A) -> ClientResult<Option<Members>> | ||
where | ||
A: AsRef<str> + Debug, | ||
{ | ||
self.read(self.url(application.as_ref(), AdministrationOperation::Members)?) | ||
.await | ||
} | ||
|
||
/// Update the application members and their roles | ||
#[instrument] | ||
pub async fn update_members<A>(&self, application: A, members: Members) -> ClientResult<bool> | ||
where | ||
A: AsRef<str> + Debug, | ||
{ | ||
self.update( | ||
self.url(application.as_ref(), AdministrationOperation::Members)?, | ||
Some(members), | ||
) | ||
.await | ||
} | ||
|
||
/// Transfer the application ownership to another user | ||
#[instrument] | ||
pub async fn initiate_app_transfer<A, U>( | ||
&self, | ||
application: A, | ||
username: U, | ||
) -> ClientResult<bool> | ||
where | ||
A: AsRef<str> + Debug, | ||
U: AsRef<str> + Debug, | ||
{ | ||
let payload = TransferOwnership { | ||
new_user: username.as_ref().to_string(), | ||
}; | ||
|
||
self.update( | ||
self.url(application.as_ref(), AdministrationOperation::Transfer)?, | ||
Some(payload), | ||
) | ||
.await | ||
} | ||
|
||
/// Cancel the application ownership transfer | ||
#[instrument] | ||
pub async fn cancel_app_transfer<A>(&self, application: A) -> ClientResult<bool> | ||
where | ||
A: AsRef<str> + Debug, | ||
{ | ||
self.delete(self.url(application.as_ref(), AdministrationOperation::Transfer)?) | ||
.await | ||
} | ||
|
||
/// Accept the application ownership transfer | ||
#[instrument] | ||
pub async fn accept_app_transfer<A>(&self, application: A) -> ClientResult<bool> | ||
where | ||
A: AsRef<str> + Debug, | ||
{ | ||
self.update( | ||
self.url(application.as_ref(), AdministrationOperation::Accept)?, | ||
None::<()>, | ||
) | ||
.await | ||
} | ||
|
||
/// Read the application ownership transfer state | ||
#[instrument] | ||
pub async fn read_app_transfer<A>( | ||
&self, | ||
application: A, | ||
) -> ClientResult<Option<TransferOwnership>> | ||
where | ||
A: AsRef<str> + Debug, | ||
{ | ||
self.read(self.url(application.as_ref(), AdministrationOperation::Transfer)?) | ||
.await | ||
} | ||
} |
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,45 @@ | ||
use core::fmt::{Display, Formatter}; | ||
use indexmap::IndexMap; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct TransferOwnership { | ||
pub new_user: String, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Members { | ||
#[serde(skip_serializing_if = "Option::is_none")] | ||
pub resource_version: Option<String>, | ||
#[serde(default)] | ||
pub members: IndexMap<String, MemberEntry>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Clone, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct MemberEntry { | ||
pub role: Role, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Serialize, Deserialize, Eq, PartialEq)] | ||
#[serde(rename_all = "camelCase")] | ||
pub enum Role { | ||
/// Allow everything, including changing members | ||
Admin, | ||
/// Allow reading and writing, but not changing members. | ||
Manager, | ||
/// Allow reading only. | ||
Reader, | ||
} | ||
|
||
impl Display for Role { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
Self::Admin => write!(f, "Administrator"), | ||
Self::Manager => write!(f, "Manager"), | ||
Self::Reader => write!(f, "Reader"), | ||
} | ||
} | ||
} |
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,7 @@ | ||
#[cfg(feature = "reqwest")] | ||
mod client; | ||
mod data; | ||
|
||
#[cfg(feature = "reqwest")] | ||
pub use client::*; | ||
pub use data::*; |
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,5 +1,6 @@ | ||
//! A client for the Drogue IoT Cloud APIs. | ||
pub mod admin; | ||
pub mod core; | ||
pub mod error; | ||
pub mod meta; | ||
|
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
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
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