|
| 1 | +package compute |
| 2 | + |
| 3 | +import appcompute "github.com/JSYoo5B/SandStack/internal/app/compute" |
| 4 | + |
| 5 | +type hypervisorListResponse struct { |
| 6 | + Hypervisors []hypervisorDocument `json:"hypervisors"` |
| 7 | +} |
| 8 | + |
| 9 | +type hypervisorResponse struct { |
| 10 | + Hypervisor hypervisorDocument `json:"hypervisor"` |
| 11 | +} |
| 12 | + |
| 13 | +type hypervisorStatisticsResponse struct { |
| 14 | + HypervisorStatistics hypervisorStatisticsDocument `json:"hypervisor_statistics"` |
| 15 | +} |
| 16 | + |
| 17 | +type hypervisorDocument struct { |
| 18 | + ID string `json:"id"` |
| 19 | + CPUInfo hypervisorCPUInfoDocument `json:"cpu_info"` |
| 20 | + CurrentWorkload int `json:"current_workload"` |
| 21 | + Status string `json:"status"` |
| 22 | + State string `json:"state"` |
| 23 | + DiskAvailableLeast int `json:"disk_available_least"` |
| 24 | + HostIP string `json:"host_ip"` |
| 25 | + FreeDiskGB int `json:"free_disk_gb"` |
| 26 | + FreeRAMMB int `json:"free_ram_mb"` |
| 27 | + HypervisorHostname string `json:"hypervisor_hostname"` |
| 28 | + HypervisorType string `json:"hypervisor_type"` |
| 29 | + HypervisorVersion int `json:"hypervisor_version"` |
| 30 | + LocalGB int `json:"local_gb"` |
| 31 | + LocalGBUsed int `json:"local_gb_used"` |
| 32 | + MemoryMB int `json:"memory_mb"` |
| 33 | + MemoryMBUsed int `json:"memory_mb_used"` |
| 34 | + RunningVMs int `json:"running_vms"` |
| 35 | + Service hypervisorServiceDocument `json:"service"` |
| 36 | + VCPUs int `json:"vcpus"` |
| 37 | + VCPUsUsed int `json:"vcpus_used"` |
| 38 | +} |
| 39 | + |
| 40 | +type hypervisorCPUInfoDocument struct { |
| 41 | + Vendor string `json:"vendor"` |
| 42 | + Arch string `json:"arch"` |
| 43 | + Model string `json:"model"` |
| 44 | + Features []string `json:"features"` |
| 45 | + Topology hypervisorCPUTopologyDocument `json:"topology"` |
| 46 | +} |
| 47 | + |
| 48 | +type hypervisorCPUTopologyDocument struct { |
| 49 | + Cells int `json:"cells"` |
| 50 | + Sockets int `json:"sockets"` |
| 51 | + Cores int `json:"cores"` |
| 52 | + Threads int `json:"threads"` |
| 53 | +} |
| 54 | + |
| 55 | +type hypervisorServiceDocument struct { |
| 56 | + ID string `json:"id"` |
| 57 | + Host string `json:"host"` |
| 58 | + DisabledReason string `json:"disabled_reason"` |
| 59 | +} |
| 60 | + |
| 61 | +type hypervisorStatisticsDocument struct { |
| 62 | + Count int `json:"count"` |
| 63 | + CurrentWorkload int `json:"current_workload"` |
| 64 | + DiskAvailableLeast int `json:"disk_available_least"` |
| 65 | + FreeDiskGB int `json:"free_disk_gb"` |
| 66 | + FreeRAMMB int `json:"free_ram_mb"` |
| 67 | + LocalGB int `json:"local_gb"` |
| 68 | + LocalGBUsed int `json:"local_gb_used"` |
| 69 | + MemoryMB int `json:"memory_mb"` |
| 70 | + MemoryMBUsed int `json:"memory_mb_used"` |
| 71 | + RunningVMs int `json:"running_vms"` |
| 72 | + VCPUs int `json:"vcpus"` |
| 73 | + VCPUsUsed int `json:"vcpus_used"` |
| 74 | +} |
| 75 | + |
| 76 | +func toHypervisorDocuments( |
| 77 | + hypervisors []appcompute.Hypervisor, |
| 78 | +) []hypervisorDocument { |
| 79 | + documents := make([]hypervisorDocument, 0, len(hypervisors)) |
| 80 | + for _, hypervisor := range hypervisors { |
| 81 | + documents = append(documents, toHypervisorDocument(hypervisor)) |
| 82 | + } |
| 83 | + |
| 84 | + return documents |
| 85 | +} |
| 86 | + |
| 87 | +func toHypervisorDocument( |
| 88 | + hypervisor appcompute.Hypervisor, |
| 89 | +) hypervisorDocument { |
| 90 | + return hypervisorDocument{ |
| 91 | + ID: hypervisor.ID, |
| 92 | + CPUInfo: defaultCPUInfo(), |
| 93 | + CurrentWorkload: hypervisor.CurrentWorkload, |
| 94 | + Status: hypervisor.Status, |
| 95 | + State: hypervisor.State, |
| 96 | + DiskAvailableLeast: hypervisor.DiskAvailableLeast, |
| 97 | + HostIP: hypervisor.HostIP, |
| 98 | + FreeDiskGB: hypervisor.FreeDiskGB, |
| 99 | + FreeRAMMB: hypervisor.FreeRAMMB, |
| 100 | + HypervisorHostname: hypervisor.Hostname, |
| 101 | + HypervisorType: hypervisor.Type, |
| 102 | + HypervisorVersion: hypervisor.Version, |
| 103 | + LocalGB: hypervisor.LocalGB, |
| 104 | + LocalGBUsed: hypervisor.LocalGBUsed, |
| 105 | + MemoryMB: hypervisor.MemoryMB, |
| 106 | + MemoryMBUsed: hypervisor.MemoryMBUsed, |
| 107 | + RunningVMs: hypervisor.RunningVMs, |
| 108 | + Service: hypervisorServiceDocument{ |
| 109 | + ID: hypervisor.ServiceID, |
| 110 | + Host: hypervisor.Hostname, |
| 111 | + }, |
| 112 | + VCPUs: hypervisor.VCPUs, |
| 113 | + VCPUsUsed: hypervisor.VCPUsUsed, |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func toHypervisorStatisticsDocument( |
| 118 | + hypervisors []appcompute.Hypervisor, |
| 119 | +) hypervisorStatisticsDocument { |
| 120 | + stats := hypervisorStatisticsDocument{Count: len(hypervisors)} |
| 121 | + for _, hypervisor := range hypervisors { |
| 122 | + stats.CurrentWorkload += hypervisor.CurrentWorkload |
| 123 | + stats.DiskAvailableLeast += hypervisor.DiskAvailableLeast |
| 124 | + stats.FreeDiskGB += hypervisor.FreeDiskGB |
| 125 | + stats.FreeRAMMB += hypervisor.FreeRAMMB |
| 126 | + stats.LocalGB += hypervisor.LocalGB |
| 127 | + stats.LocalGBUsed += hypervisor.LocalGBUsed |
| 128 | + stats.MemoryMB += hypervisor.MemoryMB |
| 129 | + stats.MemoryMBUsed += hypervisor.MemoryMBUsed |
| 130 | + stats.RunningVMs += hypervisor.RunningVMs |
| 131 | + stats.VCPUs += hypervisor.VCPUs |
| 132 | + stats.VCPUsUsed += hypervisor.VCPUsUsed |
| 133 | + } |
| 134 | + |
| 135 | + return stats |
| 136 | +} |
| 137 | + |
| 138 | +func defaultCPUInfo() hypervisorCPUInfoDocument { |
| 139 | + return hypervisorCPUInfoDocument{ |
| 140 | + Vendor: "SandStack", |
| 141 | + Arch: "x86_64", |
| 142 | + Model: "compat", |
| 143 | + Features: []string{}, |
| 144 | + Topology: hypervisorCPUTopologyDocument{ |
| 145 | + Cells: 1, |
| 146 | + Sockets: 1, |
| 147 | + Cores: 1, |
| 148 | + Threads: 1, |
| 149 | + }, |
| 150 | + } |
| 151 | +} |
0 commit comments