diff --git a/add.go b/add.go index d4bd08a..a7f9b7a 100644 --- a/add.go +++ b/add.go @@ -1,6 +1,7 @@ package virtbuf import ( + "errors" "fmt" "time" @@ -8,6 +9,32 @@ import ( "go.wit.com/log" ) +func (c *NewCluster) InitDroplet(hostname string) (*Droplet, error) { + var d *Droplet + d = new(Droplet) + d.Current = new(Current) + + d = c.FindDropletByName(hostname) + if d != nil { + return d, errors.New("duplicate hostname: " + hostname) + } + d.Hostname = hostname + // d.Uuid = uuid.New() // not appropriate here + + // set some defaults + d.StartState = DropletState_OFF + d.Current.State = DropletState_UNKNOWN + c.appendDroplet(d) + return d, nil +} + +func (c *NewCluster) appendDroplet(d *Droplet) { + c.Lock() + defer c.Unlock() + + c.d.Droplets = append(c.d.Droplets, d) +} + // can the json protobuf output use a string and have a type handler // to convert it back to int64? func SetGB(gb int) int64 { @@ -22,15 +49,6 @@ func (x *Hypervisor) SetMemoryGB(gb int) { x.Memory = int64(gb * 1024 * 1024 * 1024) } -func (all *Droplets) oldFindDroplet(name string) *Droplet { - for _, d := range all.Droplets { - if d.Hostname == name { - return d - } - } - return nil -} - func (c *NewCluster) FindDropletByName(name string) *Droplet { loop := c.DropletsAll() // get the list of droplets for loop.Scan() {