From 6a0d4d3e3883de811cf594b0bcddd1f2c9736b43 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 30 Oct 2024 11:04:55 -0500 Subject: rename Signed-off-by: Jeff Carr --- add.go | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ helpers.go | 130 ------------------------------------------------------------- 2 files changed, 130 insertions(+), 130 deletions(-) create mode 100644 add.go delete mode 100644 helpers.go diff --git a/add.go b/add.go new file mode 100644 index 0000000..96df708 --- /dev/null +++ b/add.go @@ -0,0 +1,130 @@ +package virtbuf + +import ( + "fmt" + + "github.com/google/uuid" +) + +// 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) +} + +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 (all *Droplets) FindDroplet(name string) *Droplet { + for _, d := range all.Droplets { + if d.Hostname == name { + return d + } + } + return nil +} + +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 +} + +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 +} + +func (c *Cluster) AddDroplet(uuid string, hostname string, cpus int, mem int) *Droplet { + d := c.FindDroplet(hostname) + if d != nil { + return d + } + + d = &Droplet{ + Uuid: uuid, + Hostname: hostname, + Cpus: int64(cpus), + } + + if cpus < 0 { + d.Cpus = 1 + } + d.Memory = SetGB(mem * 32) + c.Droplets = append(c.Droplets, d) + return d +} + +// This isn't for the marketing department +// so this isn't going to use 'MiB' and 'GiB' +func HumanFormatBytes(b int64) string { + 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) +} + +func (c *Cluster) BlankFields() { + for _, d := range c.Droplets { + d.CurrentState = 0 + } +} + +func (c *Cluster) AppendEvent(e *Event) { + c.Events = append(c.Events, e) +} diff --git a/helpers.go b/helpers.go deleted file mode 100644 index 96df708..0000000 --- a/helpers.go +++ /dev/null @@ -1,130 +0,0 @@ -package virtbuf - -import ( - "fmt" - - "github.com/google/uuid" -) - -// 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) -} - -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 (all *Droplets) FindDroplet(name string) *Droplet { - for _, d := range all.Droplets { - if d.Hostname == name { - return d - } - } - return nil -} - -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 -} - -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 -} - -func (c *Cluster) AddDroplet(uuid string, hostname string, cpus int, mem int) *Droplet { - d := c.FindDroplet(hostname) - if d != nil { - return d - } - - d = &Droplet{ - Uuid: uuid, - Hostname: hostname, - Cpus: int64(cpus), - } - - if cpus < 0 { - d.Cpus = 1 - } - d.Memory = SetGB(mem * 32) - c.Droplets = append(c.Droplets, d) - return d -} - -// This isn't for the marketing department -// so this isn't going to use 'MiB' and 'GiB' -func HumanFormatBytes(b int64) string { - 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) -} - -func (c *Cluster) BlankFields() { - for _, d := range c.Droplets { - d.CurrentState = 0 - } -} - -func (c *Cluster) AppendEvent(e *Event) { - c.Events = append(c.Events, e) -} -- cgit v1.2.3