Files
3cloud-backend/streamlit_server/views/regions.py
T
2025-04-01 14:14:02 +10:30

140 lines
5.7 KiB
Python

import streamlit as st
import pandas as pd
from streamlit_server.utils.helpers import format_timestamp
def render_list():
st.header("Region Management")
# Create Region Form
with st.expander("Create New Region"):
with st.form("create_region"):
name = st.text_input("Region Name")
description = st.text_area("Description")
submit = st.form_submit_button("Create Region")
if submit and name:
result = st.session_state.client.create_region(name, description)
if result:
st.success(f"Region '{name}' created successfully!")
# List Regions
st.subheader("Existing Regions")
regions = st.session_state.client.get_regions()
if regions:
for region in regions:
with st.container():
col1, col2, col3 = st.columns([3, 1, 1])
with col1:
st.markdown(f"### {region['name']}")
if region.get('description'):
st.text(region['description'])
st.text(f"Created: {format_timestamp(region['created_at'])}")
with col2:
st.text(f"ID: {region['id'][:8]}...")
with col3:
if st.button("View Details", key=f"view_{region['id']}"):
st.session_state.selected_resource = region
st.session_state.view_type = 'detail'
st.session_state.selected_resource_type='region'
st.rerun() # Force Streamlit to rerun the script
def render_detail():
region = st.session_state.client.get_region(st.session_state.selected_resource['id'])
st.title(f"Region: {region['name']}")
if st.button("Back to list view"):
st.session_state.view_type = 'list'
st.rerun()
st.header("Region Details")
col1, col2 = st.columns(2)
with col1:
st.code(f"{region['id']}")
st.markdown(f"**Created:** {region.get('created_at', 'Unknown')}")
st.markdown("**Region Enrollment Key**(Confidental)")
st.code(f"{region['enrollment_key']}")
with col2:
if region.get('updated_at'):
st.markdown(f"**Last Updated:** {region['updated_at']}")
if region.get('description'):
st.markdown(f"**Description:** {region['description']}")
st.header("Region Access Entries")
region_access = st.session_state.client.get_region_access(region['id'])
if region_access:
# Fetch all projects and regions
projects = st.session_state.client.get_projects()
regions = st.session_state.client.get_regions()
# Create dictionaries to map IDs to names
project_map = {project['id']: project['name'] for project in projects}
region_map = {region['id']: region['name'] for region in regions}
# Add project and region names to each entry
for entry in region_access:
entry['project_name'] = project_map.get(entry['project_id'], 'Unknown Project')
entry['region_name'] = region_map.get(entry['region_id'], 'Unknown Region')
# Create a DataFrame with the enriched data
df = pd.DataFrame(region_access)
df = df[['project_id', 'project_name', 'region_id', 'region_name']] # Include names in the table
st.dataframe(df, use_container_width=True)
else:
st.info("No access entries found for this region.")
st.header("Virtual Data Centers in this Region")
all_vdcs = st.session_state.client.get_virtual_data_centers()
region_vdcs = [vdc for vdc in all_vdcs if vdc.get('region_id') == region['id']] if all_vdcs else []
if region_vdcs:
# Fetch project names for better display
projects = st.session_state.client.get_projects()
project_map = {project['id']: project['name'] for project in projects} if projects else {}
for vdc in region_vdcs:
vdc['project_name'] = project_map.get(vdc.get('project_id'), 'Unknown Project')
vdc['created_at'] = format_timestamp(vdc.get('created_at')) # Format timestamp
# Select and rename columns for display
vdc_df = pd.DataFrame(region_vdcs)
# Display VDCs with clickable links
st.markdown("---") # Add a separator
cols = st.columns([2, 3, 3, 2, 2, 2]) # Adjust column ratios as needed
headers = ["VDC ID", "VDC Name", "Project", "Status", "Created At", "Actions"]
for col, header in zip(cols, headers):
col.markdown(f"**{header}**")
for vdc in region_vdcs:
col1, col2, col3, col4, col5, col6 = st.columns([2, 3, 3, 2, 2, 2])
with col1:
st.caption(f"{vdc['id'][:8]}...")
with col2:
st.write(vdc['name'])
with col3:
st.write(vdc['project_name'])
with col4:
st.write(vdc.get('status', 'N/A'))
with col5:
st.write(vdc['created_at'])
with col6:
if st.button("View Details", key=f"view_vdc_{vdc['id']}"):
st.session_state.selected_resource = vdc
st.session_state.selected_resource_type = 'virtual_data_center'
st.session_state.view_type = 'detail'
st.switch_page("streamlit_server/views/virtual_data_centers.py")
# st.rerun()
st.markdown("---") # Separator between VDC entries
else:
st.info("No Virtual Data Centers found in this region.")
if st.session_state.view_type == 'detail' and st.session_state.selected_resource_type=='region':
render_detail()
else:
render_list()