Add VM from streamlit works!

This commit is contained in:
2025-03-07 18:00:13 +10:30
parent 923c1543bf
commit 83d7427ddc
12 changed files with 436 additions and 86 deletions
+60 -2
View File
@@ -127,6 +127,7 @@ def render_detail_view_network(network):
col1, col2 = st.columns(2)
with col1:
st.markdown(f"**ID:** `{network['id']}`")
st.markdown(f"**VDC ID:** `{network['vdc_id']}`")
st.markdown(f"**Created:** {network.get('created_at', 'Unknown')}")
st.markdown(f"**IPv4 CIDR:** {network.get('ipv4_cidr', 'N/A')}")
st.markdown(f"**IPv4 Gateway:** {network.get('ipv4_gateway', 'N/A')}")
@@ -155,7 +156,7 @@ def render_detail_view_network(network):
df = pd.DataFrame(network_ports)
df = df[[
'id', 'workload_id', 'workload_name', 'ip_address', 'mac_address',
'port_type', 'state'
'port_type'
]]
st.dataframe(df, use_container_width=True)
else:
@@ -200,4 +201,61 @@ def render_detail_view_region(region):
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.info("No access entries found for this region.")
import streamlit as st
def render_detail_view_vm(vm):
"""
Renders the details of a specific VM workload.
"""
vm = st.session_state.client.get_vm(vm['id'])
if not vm:
st.error("VM workload not found.")
return
st.title(f"VM Workload: {vm['vm_name']}")
st.header("VM Workload Details")
col1, col2 = st.columns(2)
with col1:
st.markdown(f"**ID:** `{vm['id']}`")
st.markdown(f"**Created:** {format_timestamp(vm.get('created_at', 'Unknown'))}")
st.markdown(f"**VDC ID:** {vm['vdc_id']}")
st.markdown(f"**Workload Host ID:** {vm.get('workload_host_id', 'N/A')}")
with col2:
if vm.get('updated_at'):
st.markdown(f"**Last Updated:** {format_timestamp(vm['updated_at'])}")
st.markdown(f"**Status:** {vm['status']}")
st.markdown(f"**Launch Parameters:** {vm.get('launch_params', 'N/A')}")
# Edit VM Workload Form
with st.expander("Edit VM Workload"):
with st.form("edit_vm"):
new_status = st.selectbox("Status", ["running", "stopped", "pending", "failed-deleted", "pending-deleted", "pending-allocation", "failed-allocation", "pending-allocated"], index=["running", "stopped", "pending", "failed-deleted", "pending-deleted", "pending-allocation", "failed-allocation", "pending-allocated"].index(vm['status']))
new_launch_params = st.text_input("Launch Parameters", value=vm.get('launch_params', ''))
submit = st.form_submit_button("Update VM Workload")
if submit:
result = st.session_state.client.edit_vm(vm['id'], {
"status": new_status,
"launch_params": new_launch_params
})
if result:
st.success("VM workload updated successfully!")
st.session_state.selected_resource = vm
st.session_state.view_type = 'detail'
st.session_state.selected_resource_type = 'vm'
st.rerun()
# Delete VM Workload Button
if st.button("Delete VM Workload"):
result = st.session_state.client.delete_vm(vm['id'])
if result:
st.success("VM workload deleted successfully!")
st.session_state.selected_resource = None
st.session_state.view_type = 'list'
st.session_state.selected_resource_type = 'vm'
st.rerun()
+1 -1
View File
@@ -13,7 +13,7 @@ def render():
st.session_state.api_key = api_key
st.sidebar.header("Navigation")
views = ["Universes", "Projects", "Virtual Data Centers", "Networks", "Regions", "Hosts", "Images", "Containers", "Websocket"]
views = ["Universes", "Projects", "Virtual Data Centers", "Networks", "Regions", "Hosts", "Images", "Containers", "Websocket","Virtual Machines"]
if st.session_state.view_type == 'list':
# if st.sidebar.radio("Select View", views):
@@ -0,0 +1,84 @@
import json
import streamlit as st
def render():
"""
Renders a list of all VM workloads.
"""
st.header("VM Workload Management")
# 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 {}
# Add a dropdown to select VDC
selected_vdc_name = st.selectbox("Filter by Virtual Data Center", list(vdc_options.keys()))
# Fetch all Workload Hosts for the dropdown
workload_hosts = st.session_state.client.get_workload_hosts()
workload_host_options = {workload_host['hostname']: workload_host['id'] for workload_host in workload_hosts} if workload_hosts else {}
# Create VM Workload Form
with st.expander("Create New VM Workload"):
with st.form("create_virtual_machine"):
# Dropdown for VDC selection
selected_vdc_name_create = st.selectbox("VDC", list(vdc_options.keys()))
vm_name = st.text_input("VM Name", value="my-vm")
memory = st.number_input("Memory (MB)", min_value=512, value=2048)
vcpu = st.number_input("vCPU", min_value=1, value=2)
# Networks Dropdown
networks = st.session_state.client.get_networks()
network_options = {net['id']: net['name'] for net in networks}
selected_network_ids = st.multiselect("Networks", options=list(network_options.keys()), format_func=lambda x: network_options[x])
# Volumes Section
st.subheader("Volumes")
if 'volumes' not in st.session_state:
st.session_state.volumes = []
for i, volume in enumerate(st.session_state.volumes):
col1, col2 = st.columns([3, 2])
with col1:
volume_name = st.text_input(f"Volume Name {i+1}", value=volume['name'], key=f"volume_name_{i}")
with col2:
volume_size = st.number_input(f"Size (GB) {i+1}", min_value=1, value=volume['size_gb'], key=f"volume_size_{i}")
# Store updated volume info
st.session_state.volumes[i] = {"name": volume_name, "size_gb": volume_size}
submit = st.form_submit_button("Create VM Workload")
# **Move the buttons OUTSIDE of the form**
col1, col2 = st.columns([1, 1])
with col1:
if st.button("Add Volume"):
st.session_state.volumes.append({"name": "", "size_gb": 10})
st.rerun()
with col2:
for i in range(len(st.session_state.volumes)):
if st.button(f"Remove Volume {i+1}", key=f"remove_volume_{i}"):
st.session_state.volumes.pop(i)
st.rerun()
if submit and selected_vdc_name_create and vm_name:
# Use the selected VDC's ID
vdc_id = vdc_options[selected_vdc_name_create]
vm_config = {
"memory": memory,
"vcpu": vcpu,
"volumes": st.session_state.volumes,
"networks": [{"id": net_id} for net_id in selected_network_ids]
}
result = st.session_state.client.create_virtual_machine({
"vdc": vdc_id,
"virtual-machines": [{
"vm_name": vm_name,
"vm_config": vm_config
}]
})
if result:
st.success("VM workload created successfully!")
st.session_state.volumes = [] # Reset volumes after creation
st.rerun()