From f2040886198b8f4a25ce1a9f65585097b299f9fa Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 22 Oct 2024 16:33:36 -0500 Subject: attempt at human readable message in JSON output Signed-off-by: Jeff Carr --- helpers.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 16 deletions(-) (limited to 'helpers.go') diff --git a/helpers.go b/helpers.go index 8f602ee..4ffd13c 100644 --- a/helpers.go +++ b/helpers.go @@ -8,6 +8,16 @@ import ( "google.golang.org/protobuf/encoding/prototext" ) +// 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) } @@ -36,22 +46,6 @@ 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 { @@ -69,3 +63,48 @@ func (c *Cluster) FindHypervisor(name string) *Hypervisor { } 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(hostname string, cpus int, mem int) *Droplet { + d := c.FindDroplet(hostname) + if d != nil { + return d + } + + // Generate a new UUID + id := uuid.New() + d = &Droplet{ + Uuid: id.String(), + Hostname: hostname, + Cpus: int64(cpus), + } + + if cpus < 0 { + d.Cpus = 1 + } + d.Memory = SetGB(mem * 32) + d.Testsi.Capacity = SetGB(mem * 32) + // d.Testsi = StorageInfo{Capacity: 64} + c.Droplets = append(c.Droplets, d) + return d +} -- cgit v1.2.3