Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

commands: filter get users by project and permissions #232

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Add ability to get dataset stats
- Show Global Permissions in `get users`
- Upgrade `ordered-float` version, which is exposed in the public crate api.
- Add ability to filter users by project and permission

## v0.19.0

Expand Down
52 changes: 41 additions & 11 deletions cli/src/commands/get/users.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context, Result};
use reinfer_client::{Client, UserIdentifier};
use anyhow::{bail, Context, Result};
use reinfer_client::{Client, ProjectName, ProjectPermission, UserIdentifier};
use structopt::StructOpt;

use crate::printer::Printer;
Expand All @@ -9,24 +9,54 @@ pub struct GetUsersArgs {
#[structopt(short = "u", long = "user")]
/// Use to retrieve a single user with the provided id
user: Option<UserIdentifier>,

#[structopt(short = "o", long = "project")]
/// Filter users by a given project
project_name_filter: Option<ProjectName>,

#[structopt(short = "p", long = "permission")]
/// Filter users by a given project permission
project_permission_filter: Option<ProjectPermission>,
}

pub fn get(client: &Client, args: &GetUsersArgs, printer: &Printer) -> Result<()> {
let GetUsersArgs { user } = args;
match user {
let GetUsersArgs {
user,
project_name_filter,
project_permission_filter,
} = args;

if project_name_filter.is_none() && project_permission_filter.is_some() {
bail!("You cannot filter on `permission` without a `project`")
}

Comment on lines +29 to +31
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue you could implement this such that if you didn't pass in a project, you got back all users with that permission in any project.

But if that's not the behaviour you want, happy with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah good shout - will save that for a future PR. Thanks

let mut users = match user {
Some(user_id) => {
let user = client
.get_user(user_id.clone())
.context("Operation to get user has failed.")?;
printer.print_resources(&[user])
}
None => {
let users = client
.get_users()
.context("Operation to list users has failed.")?;
printer.print_resources(&users)
vec![user]
}
None => client
.get_users()
.context("Operation to list users has failed.")?,
};

if let Some(project_name) = project_name_filter {
users.retain(|user| {
user.project_permissions
.get(project_name)
.is_some_and(|user_permissions| {
if let Some(project_permission) = project_permission_filter {
user_permissions.contains(project_permission)
} else {
true
}
})
})
}

printer.print_resources(&users)
}

pub fn get_current_user(client: &Client, printer: &Printer) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion cli/tests/test_datasets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ fn test_create_dataset_wrong_model_family() {
.unwrap();
assert!(!output.status.success());
assert!(String::from_utf8_lossy(&output.stderr)
.contains("API request failed with 400 Bad Request: 'non-existent-family' is not one of"))
.contains("API request failed with 400 Bad Request: Invalid request - Unsupported model family: non-existent-family"))
}

#[test]
Expand Down
Loading