Skip to content

Commit

Permalink
refactor(client)!: AuthClient::role_delete
Browse files Browse the repository at this point in the history
Signed-off-by: lxl66566 <[email protected]>
  • Loading branch information
lxl66566 committed Aug 2, 2024
1 parent f6525e0 commit 8e03c92
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 69 deletions.
11 changes: 3 additions & 8 deletions crates/xline-client/examples/auth_role.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use anyhow::Result;
use xline_client::{
types::auth::{
AuthRoleDeleteRequest, AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
Permission, PermissionType,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest, Permission, PermissionType,
},
Client, ClientOptions,
};
Expand Down Expand Up @@ -55,12 +54,8 @@ async fn main() -> Result<()> {
.await?;

// delete roles
client
.role_delete(AuthRoleDeleteRequest::new("role1"))
.await?;
client
.role_delete(AuthRoleDeleteRequest::new("role2"))
.await?;
client.role_delete("role1").await?;
client.role_delete("role2").await?;

Ok(())
}
16 changes: 6 additions & 10 deletions crates/xline-client/src/clients/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use xlineapi::{

use crate::{
error::{Result, XlineClientError},
types::auth::{
AuthRoleDeleteRequest, AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
},
types::auth::{AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest},
AuthService, CurpClient,
};

Expand Down Expand Up @@ -626,7 +624,7 @@ impl AuthClient {
/// # Examples
///
/// ```no_run
/// use xline_client::{types::auth::AuthRoleDeleteRequest, Client, ClientOptions};
/// use xline_client::{Client, ClientOptions};
/// use anyhow::Result;
///
/// #[tokio::main]
Expand All @@ -640,18 +638,16 @@ impl AuthClient {
/// // add the role
///
/// client
/// .role_delete(AuthRoleDeleteRequest::new("role"))
/// .role_delete("role")
/// .await?;
///
/// Ok(())
/// }
///```
#[inline]
pub async fn role_delete(
&self,
request: AuthRoleDeleteRequest,
) -> Result<AuthRoleDeleteResponse> {
self.handle_req(request.inner, false).await
pub async fn role_delete(&self, name: impl Into<String>) -> Result<AuthRoleDeleteResponse> {
self.handle_req(xlineapi::AuthRoleDeleteRequest { role: name.into() }, false)
.await
}

/// Grants role permission.
Expand Down
26 changes: 0 additions & 26 deletions crates/xline-client/src/types/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,6 @@ pub use xlineapi::{
AuthenticateResponse, Type as PermissionType,
};

/// Request for `AuthRoleDelete`
#[derive(Debug, PartialEq)]
pub struct AuthRoleDeleteRequest {
/// Inner request
pub(crate) inner: xlineapi::AuthRoleDeleteRequest,
}

impl AuthRoleDeleteRequest {
/// Creates a new `AuthRoleDeleteRequest`
///
/// `role` is the name of the role to delete.
#[inline]
pub fn new(role: impl Into<String>) -> Self {
Self {
inner: xlineapi::AuthRoleDeleteRequest { role: role.into() },
}
}
}

impl From<AuthRoleDeleteRequest> for xlineapi::AuthRoleDeleteRequest {
#[inline]
fn from(req: AuthRoleDeleteRequest) -> Self {
req.inner
}
}

/// Request for `AuthRoleGrantPermission`
#[derive(Debug, PartialEq)]
pub struct AuthRoleGrantPermissionRequest {
Expand Down
15 changes: 4 additions & 11 deletions crates/xline-client/tests/it/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use xline_client::{
error::Result,
types::auth::{
AuthRoleDeleteRequest, AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest,
Permission, PermissionType,
AuthRoleGrantPermissionRequest, AuthRoleRevokePermissionRequest, Permission, PermissionType,
},
};

Expand All @@ -28,12 +27,8 @@ async fn role_operations_should_success_in_normal_path() -> Result<()> {
vec![role1.to_owned(), role2.to_owned()]
);

client
.role_delete(AuthRoleDeleteRequest::new(role1))
.await?;
client
.role_delete(AuthRoleDeleteRequest::new(role2))
.await?;
client.role_delete(role1).await?;
client.role_delete(role2).await?;

client.role_get(role1).await.unwrap_err();
client.role_get(role2).await.unwrap_err();
Expand Down Expand Up @@ -105,9 +100,7 @@ async fn permission_operations_should_success_in_normal_path() -> Result<()> {
let role_get_resp = client.role_get(role1).await?;
assert!(role_get_resp.perm.is_empty());

client
.role_delete(AuthRoleDeleteRequest::new(role1))
.await?;
client.role_delete(role1).await?;
Ok(())
}

Expand Down
8 changes: 2 additions & 6 deletions crates/xline/tests/it/auth_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use utils::config::{
TraceConfig, XlineServerConfig,
};
use xline_test_utils::{
enable_auth, set_user,
types::{auth::AuthRoleDeleteRequest, kv::RangeRequest},
Client, ClientOptions, Cluster,
enable_auth, set_user, types::kv::RangeRequest, Client, ClientOptions, Cluster,
};

#[tokio::test(flavor = "multi_thread")]
Expand Down Expand Up @@ -145,9 +143,7 @@ async fn test_role_delete() -> Result<(), Box<dyn Error>> {
set_user(client, "u", "123", "r", b"foo", &[]).await?;
let user = auth_client.user_get("u").await?;
assert_eq!(user.roles.len(), 1);
auth_client
.role_delete(AuthRoleDeleteRequest::new("r"))
.await?;
auth_client.role_delete("r").await?;
let user = auth_client.user_get("u").await?;
assert_eq!(user.roles.len(), 0);

Expand Down
15 changes: 7 additions & 8 deletions crates/xlinectl/src/command/role/delete.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clap::{arg, ArgMatches, Command};
use xline_client::{error::Result, types::auth::AuthRoleDeleteRequest, Client};
use xline_client::{error::Result, Client};

use crate::utils::printer::Printer;

Expand All @@ -11,9 +11,11 @@ pub(super) fn command() -> Command {
}

/// Build request from matches
pub(super) fn build_request(matches: &ArgMatches) -> AuthRoleDeleteRequest {
///
/// Returns the name of the role to be deleted
pub(super) fn build_request(matches: &ArgMatches) -> String {
let name = matches.get_one::<String>("name").expect("required");
AuthRoleDeleteRequest::new(name)
name.to_owned()
}

/// Execute the command
Expand All @@ -30,14 +32,11 @@ mod tests {
use super::*;
use crate::test_case_struct;

test_case_struct!(AuthRoleDeleteRequest);
test_case_struct!(String);

#[test]
fn command_parse_should_be_valid() {
let test_cases = vec![TestCase::new(
vec!["delete", "Admin"],
Some(AuthRoleDeleteRequest::new("Admin")),
)];
let test_cases = vec![TestCase::new(vec!["delete", "Admin"], Some("Admin".into()))];

for case in test_cases {
case.run_test();
Expand Down

0 comments on commit 8e03c92

Please sign in to comment.