Add UpdateRoleAction
This change adds the Action implementation to handle the "role update"
command.
@@ -17,7 +17,7 @@ use clap::ArgMatches;
use crate::error::CliError;
use super::{
- api::{RoleBuilder, SplinterRestClient, SplinterRestClientBuilder},
+ api::{RoleBuilder, RoleUpdateBuilder, SplinterRestClient, SplinterRestClientBuilder},
create_cylinder_jwt_auth, print_table, Action, DEFAULT_SPLINTER_REST_API_URL,
SPLINTER_REST_API_URL_ENV,
};
@@ -120,6 +120,52 @@ impl Action for CreateRoleAction {
}
}
+pub struct UpdateRoleAction;
+
+impl Action for UpdateRoleAction {
+ fn run<'a>(&mut self, arg_matches: Option<&ArgMatches<'a>>) -> Result<(), CliError> {
+ let role_id = arg_matches
+ .and_then(|args| args.value_of("role_id"))
+ .ok_or_else(|| CliError::ActionError("A role id must be provided.".into()))?;
+
+ let display_name = arg_matches
+ .and_then(|args| args.value_of("display_name"))
+ .map(|s| s.to_owned());
+
+ let permissions_to_add = arg_matches
+ .and_then(|args| args.values_of("add_permission"))
+ .map(|vals| vals.map(|s| s.to_owned()).collect())
+ .unwrap_or_else(Vec::new);
+
+ let permissions_to_rm = arg_matches
+ .and_then(|args| args.values_of("rm_permission"))
+ .map(|vals| vals.map(|s| s.to_owned()).collect())
+ .unwrap_or_else(Vec::new);
+
+ let client = new_client(&arg_matches)?;
+
+ let role = client.get_role(role_id)?;
+
+ let mut permissions = role
+ .permissions
+ .into_iter()
+ .chain(permissions_to_add.into_iter())
+ .filter(|perm| !permissions_to_rm.contains(&perm))
+ .collect::<Vec<_>>();
+
+ permissions.sort();
+ permissions.dedup();
+
+ client.update_role(
+ RoleUpdateBuilder::default()
+ .with_role_id(role_id.into())
+ .with_display_name(display_name)
+ .with_permissions(Some(permissions))
+ .build()?,
+ )
+ }
+}
+
fn new_client(arg_matches: &Option<&ArgMatches<'_>>) -> Result<SplinterRestClient, CliError> {
let url = arg_matches
.and_then(|args| args.value_of("url"))
