2024-10-22 04:18:40 -05:00
|
|
|
package virtbuf
|
|
|
|
|
2024-10-22 04:37:28 -05:00
|
|
|
import (
|
2024-10-22 06:19:24 -05:00
|
|
|
"fmt"
|
|
|
|
|
2024-10-22 15:58:28 -05:00
|
|
|
"github.com/google/uuid"
|
2024-10-22 06:19:24 -05:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/encoding/prototext"
|
2024-10-22 04:37:28 -05:00
|
|
|
)
|
2024-10-22 04:26:29 -05:00
|
|
|
|
2024-10-22 16:33:36 -05:00
|
|
|
// can the json protobuf output use a string and have a type handler
|
|
|
|
// to convert it back to int64?
|
|
|
|
func SetGB(gb int) int64 {
|
|
|
|
return int64(gb * 1024 * 1024 * 1024)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetMB(mb int) int64 {
|
|
|
|
return int64(mb * 1024 * 1024)
|
|
|
|
}
|
|
|
|
|
2024-10-22 04:26:29 -05:00
|
|
|
func (x *Hypervisor) SetMemoryGB(gb int) {
|
2024-10-22 04:18:40 -05:00
|
|
|
x.Memory = int64(gb * 1024 * 1024 * 1024)
|
|
|
|
}
|
2024-10-22 04:26:29 -05:00
|
|
|
|
|
|
|
func (x *Hypervisor) GetMemoryPrintable() string {
|
|
|
|
i := x.Memory / (1024 * 1024 * 1024)
|
|
|
|
return fmt.Sprintf("%d GB", i)
|
|
|
|
}
|
2024-10-22 04:37:28 -05:00
|
|
|
|
|
|
|
func (c *Cluster) MarshalJSON() ([]byte, error) {
|
2024-10-22 06:19:24 -05:00
|
|
|
return protojson.Marshal(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) FormatJSON() string {
|
|
|
|
return protojson.Format(c)
|
|
|
|
}
|
|
|
|
|
2024-10-22 04:37:28 -05:00
|
|
|
func (c *Cluster) UnmarshalJSON(data []byte) error {
|
2024-10-22 06:19:24 -05:00
|
|
|
return protojson.Unmarshal(data, c)
|
2024-10-22 04:37:28 -05:00
|
|
|
}
|
2024-10-22 06:25:31 -05:00
|
|
|
|
|
|
|
// apparently this isn't supposed to be used?
|
|
|
|
// https://protobuf.dev/reference/go/faq/#unstable-text
|
|
|
|
// this is a shame because this is much nicer output than JSON Format()
|
|
|
|
func (c *Cluster) FormatTEXT() string {
|
|
|
|
return prototext.Format(c)
|
|
|
|
}
|
2024-10-22 15:58:28 -05:00
|
|
|
|
2024-10-23 00:24:09 -05:00
|
|
|
func (d *Droplets) FormatTEXT() string {
|
|
|
|
return prototext.Format(d)
|
|
|
|
}
|
|
|
|
|
2024-10-22 15:58:28 -05:00
|
|
|
func (c *Cluster) FindDroplet(name string) *Droplet {
|
|
|
|
for _, d := range c.Droplets {
|
|
|
|
if d.Hostname == name {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cluster) FindHypervisor(name string) *Hypervisor {
|
|
|
|
for _, h := range c.Hypervisors {
|
|
|
|
if h.Hostname == name {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2024-10-22 16:33:36 -05:00
|
|
|
|
|
|
|
func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor {
|
|
|
|
h := c.FindHypervisor(hostname)
|
|
|
|
if h != nil {
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
// Generate a new UUID
|
|
|
|
id := uuid.New()
|
|
|
|
h = &Hypervisor{
|
|
|
|
Uuid: id.String(),
|
|
|
|
Hostname: hostname,
|
|
|
|
Cpus: int64(cpus),
|
|
|
|
Comment: "this is a fake hypervisor",
|
|
|
|
}
|
|
|
|
if cpus < 0 {
|
|
|
|
h.Cpus = 1
|
|
|
|
}
|
|
|
|
h.SetMemoryGB(mem * 32)
|
|
|
|
c.Hypervisors = append(c.Hypervisors, h)
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
2024-10-23 06:37:08 -05:00
|
|
|
func (c *Cluster) AddDroplet(uuid string, hostname string, cpus int, mem int) *Droplet {
|
2024-10-22 16:33:36 -05:00
|
|
|
d := c.FindDroplet(hostname)
|
|
|
|
if d != nil {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
d = &Droplet{
|
2024-10-23 06:37:08 -05:00
|
|
|
Uuid: uuid,
|
2024-10-22 16:33:36 -05:00
|
|
|
Hostname: hostname,
|
|
|
|
Cpus: int64(cpus),
|
|
|
|
}
|
|
|
|
|
|
|
|
if cpus < 0 {
|
|
|
|
d.Cpus = 1
|
|
|
|
}
|
|
|
|
d.Memory = SetGB(mem * 32)
|
2024-10-22 17:12:32 -05:00
|
|
|
if d.Humantest == nil {
|
2024-10-22 16:42:14 -05:00
|
|
|
var newInfo StorageInfo
|
|
|
|
newInfo = StorageInfo{Capacity: 64}
|
2024-10-22 17:12:32 -05:00
|
|
|
d.Humantest = &newInfo
|
2024-10-22 16:42:14 -05:00
|
|
|
} else {
|
2024-10-22 17:12:32 -05:00
|
|
|
d.Humantest.Capacity = SetGB(mem * 32)
|
2024-10-22 16:42:14 -05:00
|
|
|
}
|
2024-10-22 16:33:36 -05:00
|
|
|
c.Droplets = append(c.Droplets, d)
|
|
|
|
return d
|
|
|
|
}
|
2024-10-23 07:18:20 -05:00
|
|
|
|
2024-10-23 16:06:02 -05:00
|
|
|
// This isn't for the marketing department
|
|
|
|
// so this isn't going to use 'MiB' and 'GiB'
|
|
|
|
func HumanFormatBytes(b int64) string {
|
2024-10-23 07:18:20 -05:00
|
|
|
if b < 2000 {
|
|
|
|
return fmt.Sprintf("%d B", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
kb := int(b / 1024)
|
|
|
|
if kb < 2000 {
|
|
|
|
return fmt.Sprintf("%d KB", kb)
|
|
|
|
}
|
|
|
|
|
|
|
|
mb := int(b / (1024 * 1024))
|
|
|
|
if mb < 2000 {
|
|
|
|
return fmt.Sprintf("%d MB", mb)
|
|
|
|
}
|
|
|
|
|
|
|
|
gb := int(b / (1024 * 1024 * 1024))
|
|
|
|
if gb < 2000 {
|
|
|
|
return fmt.Sprintf("%d GB", gb)
|
|
|
|
}
|
|
|
|
|
|
|
|
tb := int(b / (1024 * 1024 * 1024 * 1024))
|
|
|
|
return fmt.Sprintf("%d TB", tb)
|
|
|
|
}
|