virtbuf/helpers.go

72 lines
1.5 KiB
Go

package virtbuf
import (
"fmt"
"github.com/google/uuid"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
)
func (x *Hypervisor) SetMemoryGB(gb int) {
x.Memory = int64(gb * 1024 * 1024 * 1024)
}
func (x *Hypervisor) GetMemoryPrintable() string {
i := x.Memory / (1024 * 1024 * 1024)
return fmt.Sprintf("%d GB", i)
}
func (c *Cluster) MarshalJSON() ([]byte, error) {
return protojson.Marshal(c)
}
func (c *Cluster) FormatJSON() string {
return protojson.Format(c)
}
func (c *Cluster) UnmarshalJSON(data []byte) error {
return protojson.Unmarshal(data, c)
}
// 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)
}
func (c *Cluster) AddHypervisor(hostname string, cpus int, mem int) *Hypervisor {
// Generate a new UUID
id := uuid.New()
h := &Hypervisor{
Uuid: id.String(),
Hostname: hostname,
Cpus: 16,
Memory: 256,
Comment: "this is a fake hypervisor",
}
h.Cpus = int64(cpus)
h.SetMemoryGB(mem * 32)
c.Hypervisors = append(c.Hypervisors, h)
return h
}
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
}