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

chore: Implement user deletion #952

Merged
merged 2 commits into from
Feb 5, 2025
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
6 changes: 3 additions & 3 deletions openstack_tui/.config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ mode_keybindings:
"y":
action: DescribeApiResponse
description: YAML
"ctrl-d":
action: IdentityUserDelete
description: Delete
"e":
action: IdentityUserFlipEnable
description: Enable/Disable user
"r":
action: IdentityUserDelete
description: Delete user (todo!)
"a":
action: IdentityUserCreate
description: Create new user (todo!)
Expand Down
21 changes: 1 addition & 20 deletions openstack_tui/src/components/describe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,13 @@ use ratatui::{
};
use serde_json::Value;
use std::cmp;
use std::collections::HashMap;

use crate::{action::Action, config::Config, error::TuiError, mode::Mode};

#[derive(Default)]
pub struct Describe {
config: Config,
pub keymap: HashMap<KeyEvent, Action>,
pub text: Vec<String>,
pub last_events: Vec<KeyEvent>,
text: Vec<String>,
title: Option<String>,
is_focused: bool,
is_loading: bool,
Expand All @@ -49,17 +46,6 @@ impl Describe {
}
}

pub fn keymap(mut self, keymap: HashMap<KeyEvent, Action>) -> Self {
self.keymap = keymap;
self
}

pub fn tick(&mut self) {
self.last_events.drain(..);
}

pub fn render_tick(&mut self) {}

pub fn set_loading(&mut self, loading: bool) {
self.is_loading = loading;
}
Expand Down Expand Up @@ -98,11 +84,6 @@ impl Describe {
Ok(())
}

pub fn set_title(&mut self, title: Option<String>) -> Result<()> {
self.title = title;
Ok(())
}

pub fn set_focus(&mut self, focus: bool) -> Result<()> {
self.is_focused = focus;
Ok(())
Expand Down
6 changes: 0 additions & 6 deletions openstack_tui/src/components/error_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use ratatui::{
prelude::*,
widgets::{block::*, *},
};
use std::collections::HashMap;

use crate::{
action::Action, components::Component, config::Config, error::TuiError, mode::Mode,
Expand All @@ -28,8 +27,6 @@ use crate::{

pub struct ErrorPopup {
config: Config,
pub keymap: HashMap<KeyEvent, Action>,
pub last_events: Vec<KeyEvent>,
text: Vec<String>,
scroll: (u16, u16),
}
Expand All @@ -44,14 +41,11 @@ impl ErrorPopup {
pub fn new() -> Self {
Self {
config: Config::default(),
keymap: HashMap::new(),
text: Vec::new(),
last_events: Vec::new(),
scroll: (0, 0),
}
}

pub fn render_tick(&mut self) {}
pub fn scroll_right(&mut self) {
self.scroll.0 = self.scroll.0.saturating_add(1);
}
Expand Down
15 changes: 1 addition & 14 deletions openstack_tui/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//
// SPDX-License-Identifier: Apache-2.0

use crossterm::event::KeyEvent;
use eyre::{Result, WrapErr};
use itertools::Itertools;
use ratatui::{
Expand All @@ -25,7 +24,6 @@ use ratatui::{
};
use serde::Deserialize;
use serde_json::Value;
use std::collections::HashMap;
use tokio::sync::mpsc::UnboundedSender;

use crate::{
Expand Down Expand Up @@ -78,25 +76,14 @@ pub struct Home {
project_id: Option<String>,
compute_quota: Option<ComputeQuota>,
network_quota: Option<NetworkQuota>,
pub keymap: HashMap<KeyEvent, Action>,
pub last_events: Vec<KeyEvent>,
}

impl Home {
pub fn new() -> Self {
Self::default()
}

pub fn keymap(mut self, keymap: HashMap<KeyEvent, Action>) -> Self {
self.keymap = keymap;
self
}

pub fn tick(&mut self) {
self.last_events.drain(..);
}

pub fn render_tick(&mut self) {}
pub fn tick(&mut self) {}

pub fn set_loading(&mut self, loading: bool) {
self.is_loading = loading;
Expand Down
31 changes: 30 additions & 1 deletion openstack_tui/src/components/identity/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
action::Action,
cloud_worker::identity::v3::{
IdentityApiRequest, IdentityUserApiRequest, IdentityUserApplicationCredentialListBuilder,
IdentityUserList, IdentityUserSetBuilder,
IdentityUserDelete, IdentityUserDeleteBuilder, IdentityUserList, IdentityUserSetBuilder,
},
cloud_worker::types::ApiRequest,
components::{table_view::TableViewComponentBase, Component},
Expand Down Expand Up @@ -118,6 +118,27 @@
}
}
}
Action::IdentityUserDelete => {
// only if we are currently in the proper mode
if current_mode == Mode::IdentityUsers {
// and have command_tx
if let Some(command_tx) = self.get_command_tx() {
// and have a selected entry
if let Some(row) = self.get_selected() {
// send action to set Delete User
command_tx.send(Action::Confirm(ApiRequest::from(
IdentityUserApiRequest::Delete(Box::new(
IdentityUserDeleteBuilder::default()
.id(row.id.clone())
.name(row.name.clone())
.build()
.wrap_err("cannot prepare user delete request")?,
)),
)))?;
}
}
}
}
Action::ShowIdentityUserApplicationCredentials => {
// only if we are currently in the proper mode
if current_mode == Mode::IdentityUsers {
Expand Down Expand Up @@ -170,6 +191,14 @@
self.sync_table_data()?;
}
self.set_loading(false);
} else if let IdentityUserApiRequest::Delete(del) = *req {
if let IdentityUserDelete { id, .. } = *del {

Check warning

Code scanning / clippy

irrefutable if let pattern Warning

irrefutable if let pattern

Check warning

Code scanning / clippy

irrefutable if let pattern Warning

irrefutable if let pattern
if self.delete_item_row_by_res_id_mut(&id)?.is_none() {
return Ok(Some(Action::Refresh));
}
self.sync_table_data()?;
self.set_loading(false);
}
}
}
_ => {}
Expand Down
14 changes: 0 additions & 14 deletions openstack_tui/src/components/resource_select_popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use ratatui::{
prelude::*,
widgets::{block::*, *},
};
use std::collections::HashMap;
use tokio::sync::mpsc::UnboundedSender;

use crate::{
Expand All @@ -34,26 +33,13 @@ use crate::{
pub struct ApiRequestSelect {
command_tx: Option<UnboundedSender<Action>>,
config: Config,
pub keymap: HashMap<KeyEvent, Action>,
pub last_events: Vec<KeyEvent>,
fuzzy_list: FuzzySelectList,
}

impl ApiRequestSelect {
pub fn new() -> Self {
Self::default()
}

pub fn keymap(mut self, keymap: HashMap<KeyEvent, Action>) -> Self {
self.keymap = keymap;
self
}

pub fn tick(&mut self) {
self.last_events.drain(..);
}

pub fn render_tick(&mut self) {}
}

impl Component for ApiRequestSelect {
Expand Down
19 changes: 19 additions & 0 deletions openstack_tui/src/components/table_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,25 @@ where
None
}

/// delete the row with the typed data matching resource id
#[instrument(level = "debug", skip(self))]
pub fn delete_item_row_by_res_id_mut(&mut self, search_id: &String) -> Result<Option<usize>> {
let mut item_idx: Option<usize> = None;
for (idx, raw_item) in self.raw_items.iter_mut().enumerate() {
if let Some(row_item_id) = raw_item.get("id").or(raw_item.get("uuid")) {
if row_item_id == search_id {
item_idx = Some(idx);
break;
}
}
}
if let Some(idx) = item_idx {
self.raw_items.remove(idx);
self.items.remove(idx);
}
Ok(item_idx)
}

pub fn get_selected_raw(&self) -> Option<&Value> {
self.state.selected().map(|x| &self.raw_items[x])
}
Expand Down
18 changes: 9 additions & 9 deletions openstack_tui/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,18 @@ use crate::{action::Action, mode::Mode};

const CONFIG: &str = include_str!("../.config/config.yaml");

#[derive(Clone, Debug, Deserialize, Default)]
pub struct AppConfig {
#[serde(default)]
pub _data_dir: PathBuf,
#[serde(default)]
pub _config_dir: PathBuf,
}
//#[derive(Clone, Debug, Deserialize, Default)]
//pub struct AppConfig {
// #[serde(default)]
// pub _data_dir: PathBuf,
// #[serde(default)]
// pub _config_dir: PathBuf,
//}

#[derive(Clone, Debug, Default, Deserialize)]
pub struct Config {
#[serde(default, flatten)]
pub config: AppConfig,
//#[serde(default, flatten)]
//pub config: AppConfig,
#[serde(default)]
pub mode_keybindings: HashMap<Mode, KeyBindings>,
#[serde(default)]
Expand Down
6 changes: 3 additions & 3 deletions openstack_tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub(crate) mod action;
pub mod app;
pub mod cli;
pub(crate) mod cloud_worker;
pub(crate) mod components;
pub(crate) mod config;
pub mod components;
pub mod config;
pub(crate) mod error;
pub(crate) mod mode;
pub(crate) mod tui;
pub mod utils;
pub(crate) mod widgets;
pub mod widgets;