delete all buttons

This commit is contained in:
2025-03-14 14:49:43 +10:30
parent 1c1058e202
commit a80c3fc59f
2 changed files with 76 additions and 25 deletions
+41 -2
View File
@@ -8,6 +8,12 @@ def render():
"""
st.header("Container Workload Management")
# Initialize session state variables for delete confirmation
if 'container_delete_confirmation_shown' not in st.session_state:
st.session_state.container_delete_confirmation_shown = False
if 'container_delete_confirmed' not in st.session_state:
st.session_state.container_delete_confirmed = False
# Fetch all VDCs for the dropdown
vdcs = st.session_state.client.get_vdcs()
vdc_options = {vdc['name']: vdc['id'] for vdc in vdcs} if vdcs else {}
@@ -55,6 +61,40 @@ def render():
else:
filtered_containers = containers
# Delete All functionality
if not st.session_state.container_delete_confirmation_shown:
if st.button("Delete All Containers", key="delete_all_containers", type="primary", help="This will delete all visible containers"):
st.session_state.container_delete_confirmation_shown = True
st.rerun()
# Show confirmation if delete was clicked
if st.session_state.container_delete_confirmation_shown and not st.session_state.container_delete_confirmed:
st.warning("Are you sure you want to delete all containers? This action cannot be undone.")
confirm_col1, confirm_col2 = st.columns([1, 1])
with confirm_col1:
if st.button("Yes, Delete All", key="confirm_delete_all_containers"):
st.session_state.container_delete_confirmed = True
st.rerun()
with confirm_col2:
if st.button("Cancel", key="cancel_delete_all_containers"):
st.session_state.container_delete_confirmation_shown = False
st.rerun()
# Perform deletion if confirmed
if st.session_state.container_delete_confirmed:
deletion_count = 0
for container in filtered_containers:
# Call the delete API for each container
result = st.session_state.client.delete_container(container['id'])
if result:
deletion_count += 1
st.success(f"Successfully deleted {deletion_count} containers.")
# Reset the confirmation flags
st.session_state.container_delete_confirmation_shown = False
st.session_state.container_delete_confirmed = False
# Display the containers
for container in filtered_containers:
with st.container():
col1, col2, col3 = st.columns([3, 1, 1])
@@ -87,5 +127,4 @@ def render():
st.rerun()
else:
st.info("No container workloads found.")
st.info("No container workloads found.")
+35 -23
View File
@@ -7,6 +7,12 @@ def render():
"""
st.header("VM Workload Management")
# Initialize session state variables for delete confirmation
if 'delete_confirmation_shown' not in st.session_state:
st.session_state.delete_confirmation_shown = False
if 'delete_confirmed' not in st.session_state:
st.session_state.delete_confirmed = False
# Fetch all VDCs for the dropdown
vdcs = st.session_state.client.get_vdcs()
vdc_options = {vdc['name']: vdc['id'] for vdc in vdcs} if vdcs else {}
@@ -95,49 +101,55 @@ def render():
else:
filtered_VirtualMachines = VirtualMachines
# Add Delete All button with confirmation
if st.button("Delete All VMs", key="delete_all_vms", type="primary", help="This will delete all visible virtual machines"):
# Create a confirmation dialog
if 'delete_confirmation' not in st.session_state:
st.session_state.delete_confirmation = False
for vm in filtered_VirtualMachines:
# Call the delete API for each VM
result = st.session_state.client.delete_virtual_machine(vm['id'])
print("Deleted vm")
st.success(f"Successfully deleted {len(filtered_VirtualMachines)} virtual machines.")
# Delete All functionality
if not st.session_state.delete_confirmation_shown:
if st.button("Delete All VMs", key="delete_all_vms", type="primary", help="This will delete all visible virtual machines"):
st.session_state.delete_confirmation_shown = True
st.rerun()
# Show confirmation if delete was clicked
if st.session_state.delete_confirmation_shown and not st.session_state.delete_confirmed:
st.warning("Are you sure you want to delete all virtual machines? This action cannot be undone.")
confirm_col1, confirm_col2 = st.columns([1, 1])
with confirm_col1:
print("adding button")
if st.button("Yes, Delete All", key="confirm_delete_all"):
print("Deleting all")
for vm in filtered_VirtualMachines:
# Call the delete API for each VM
result = st.session_state.client.delete_virtual_machine(vm['id'])
print("Deleted vm")
st.success(f"Successfully deleted {len(filtered_VirtualMachines)} virtual machines.")
st.session_state.delete_confirmed = True
st.rerun()
with confirm_col2:
if st.button("Cancel", key="cancel_delete_all"):
st.session_state.delete_confirmation_shown = False
st.rerun()
# Perform deletion if confirmed
if st.session_state.delete_confirmed:
deletion_count = 0
for vm in filtered_VirtualMachines:
# Call the delete API for each VM
result = st.session_state.client.delete_virtual_machine(vm['id'])
if result:
deletion_count += 1
st.success(f"Successfully deleted {deletion_count} virtual machines.")
# Reset the confirmation flags
st.session_state.delete_confirmation_shown = False
st.session_state.delete_confirmed = False
filtered_VirtualMachines=[]
# Display the VMs
for VirtualMachine in filtered_VirtualMachines:
with st.container():
col1, col2, col3 = st.columns([3, 1, 1])
with col1:
# Safely handle empty or None launch_params
launch_params = VirtualMachine.get('launch_params') # Use .get() to avoid KeyError if 'launch_params' doesn't exist
if launch_params: # Check if launch_params is not empty or None
launch_params = VirtualMachine.get('launch_params')
if launch_params:
try:
VirtualMachine_name = json.loads(launch_params).get('docker_image') # Use .get() to avoid KeyError if 'docker_image' doesn't exist
VirtualMachine_name = json.loads(launch_params).get('docker_image')
except json.JSONDecodeError:
print("LP not vald json")
# Handle the case where launch_params is not valid JSON
VirtualMachine_name = None
else:
print("lp empty")
# Handle the case where launch_params is empty or None
VirtualMachine_name = None
st.markdown(f"### {VirtualMachine['name']}")
st.text(f"Status: {VirtualMachine['status']}")