things for create. might be duplicates

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-16 05:22:11 -06:00
parent 74da63276e
commit 81cbb6e9d7
2 changed files with 31 additions and 3 deletions

17
add.go
View File

@ -1,7 +1,6 @@
package virtbuf package virtbuf
import ( import (
"errors"
"fmt" "fmt"
"time" "time"
@ -9,6 +8,7 @@ import (
"go.wit.com/log" "go.wit.com/log"
) )
/*
func (c *Cluster) InitDroplet(hostname string) (*Droplet, error) { func (c *Cluster) InitDroplet(hostname string) (*Droplet, error) {
var d *Droplet var d *Droplet
d = new(Droplet) d = new(Droplet)
@ -34,6 +34,7 @@ func (c *Cluster) appendDroplet(d *Droplet) {
c.d.Droplets = append(c.d.Droplets, d) c.d.Droplets = append(c.d.Droplets, d)
} }
*/
// can the json protobuf output use a string and have a type handler // can the json protobuf output use a string and have a type handler
// to convert it back to int64? // to convert it back to int64?
@ -94,8 +95,18 @@ func (c *Cluster) AddEvent(e *Event) {
c.e.Events = append(c.e.Events, e) c.e.Events = append(c.e.Events, e)
} }
func (c *Cluster) AddDroplet(d *Droplet) { // creates a new droplet with default values
c.d.Droplets = append(c.d.Droplets, d) func NewDefaultDroplet(hostname string) *Droplet {
id := uuid.New() // Generate a new UUID
d := &Droplet{
Uuid: id.String(),
Hostname: hostname,
Cpus: int64(2),
}
d.Memory = SetGB(2)
d.StartState = DropletState_OFF
return d
} }
func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet { func (c *Cluster) AddDropletSimple(uuid string, hostname string, cpus int, mem int) *Droplet {

View File

@ -17,3 +17,20 @@ type Cluster struct {
Unstable *timestamppb.Timestamp Unstable *timestamppb.Timestamp
UnstableTimeout *durationpb.Duration UnstableTimeout *durationpb.Duration
} }
// adds a new droplet. enforce unique hostnames
func (c *Cluster) AddDroplet(newd *Droplet) bool {
c.Lock()
defer c.Unlock()
for _, d := range c.d.Droplets {
if newd.Hostname == d.Hostname {
// boo. that one is already here
return false
}
}
// everything is ok, this hostname is new
c.d.Droplets = append(c.d.Droplets, newd)
return true
}