diff --git a/change.go b/change.go index 840b6d5..0c4797f 100644 --- a/change.go +++ b/change.go @@ -129,3 +129,65 @@ func (d *Droplet) SetMemory(b int64) *Event { func (d *Droplet) SetCpus(b int64) { log.Info("Set the number of cpus for the droplet", b) } + +// records an event that the droplet changed state (aka turned on, turned off, etc) +func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error { + if c == nil { + return errors.New("cluster is nil") + } + if d == nil { + return errors.New("droplet is nil") + } + if d.CurrentState == newState { + // droplet status didn't change + return nil + } + var e *Event + e = new(Event) + + e.Droplet = d.Hostname + e.OrigVal = convertToString(d.CurrentState) + e.NewVal = convertToString(newState) + e.FieldName = "status" + + now := time.Now() + e.Start = timestamppb.New(now) + + c.Events = append(c.Events, e) + return nil +} + +// records an event that the droplet migrated to another hypervisor +func (c *Cluster) DropletMoved(d *Droplet, newh *Hypervisor) error { + if c == nil { + return errors.New("cluster is nil") + } + if d == nil { + return errors.New("droplet is nil") + } + if newh == nil { + return errors.New("hypervisor is nil") + } + if d.CurrentHypervisor == newh.Hostname { + // droplet didn't move + return nil + } + + // make a change event + var e *Event + e = new(Event) + + e.Droplet = d.Hostname + e.OrigVal = d.CurrentHypervisor + e.NewVal = newh.Hostname + e.FieldName = "droplet migrate" + + now := time.Now() + e.Start = timestamppb.New(now) + + c.Events = append(c.Events, e) + + // update the droplet record + d.CurrentHypervisor = newh.Hostname + return nil +}