2024-10-24 21:39:07 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
// "reflect"
|
|
|
|
|
|
|
|
"errors"
|
2024-10-25 03:34:39 -05:00
|
|
|
"fmt"
|
2024-10-24 21:39:07 -05:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/types/known/anypb"
|
|
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
|
|
|
|
|
|
|
pb "go.wit.com/lib/protobuf/virtbuf"
|
|
|
|
"go.wit.com/log"
|
|
|
|
)
|
|
|
|
|
2024-10-25 03:34:39 -05:00
|
|
|
func convertToAnypb(x any) *anypb.Any {
|
2024-10-24 21:39:07 -05:00
|
|
|
switch v := x.(type) {
|
|
|
|
case int64:
|
|
|
|
var a *anypb.Any
|
|
|
|
a, _ = anypb.New(wrapperspb.Int64(x.(int64)))
|
|
|
|
return a
|
|
|
|
case string:
|
|
|
|
var a *anypb.Any
|
|
|
|
a, _ = anypb.New(wrapperspb.String(x.(string)))
|
|
|
|
return a
|
|
|
|
case int:
|
|
|
|
var a *anypb.Any
|
|
|
|
a, _ = anypb.New(wrapperspb.Int64(x.(int64)))
|
|
|
|
return a
|
|
|
|
case bool:
|
|
|
|
var a *anypb.Any
|
|
|
|
a, _ = anypb.New(wrapperspb.Bool(x.(bool)))
|
|
|
|
return a
|
|
|
|
default:
|
2024-10-25 06:41:22 -05:00
|
|
|
log.Error(errors.New("convertToAnypb() unknown type"), "v =", v, "x =", x)
|
2024-10-24 21:39:07 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-25 03:34:39 -05:00
|
|
|
func convertToString(x any) string {
|
|
|
|
switch v := x.(type) {
|
|
|
|
case int64:
|
|
|
|
return fmt.Sprintf("%d", x.(int64))
|
|
|
|
case string:
|
|
|
|
return x.(string)
|
|
|
|
case int:
|
|
|
|
return fmt.Sprintf("%d", x.(int64))
|
2024-10-25 06:41:22 -05:00
|
|
|
case uint:
|
|
|
|
return fmt.Sprintf("%d", x.(uint))
|
2024-10-25 03:34:39 -05:00
|
|
|
case bool:
|
|
|
|
if x.(bool) {
|
|
|
|
return "true"
|
|
|
|
}
|
|
|
|
return "false"
|
|
|
|
default:
|
2024-10-25 06:41:22 -05:00
|
|
|
log.Info("convertToSTring() unknown type", v)
|
|
|
|
log.Error(errors.New("convertToSTring() unknown type"), "v =", v, "x =", x)
|
2024-10-25 03:34:39 -05:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-10-24 21:39:07 -05:00
|
|
|
// Wrapping the int into a protobuf message
|
2024-10-25 04:30:35 -05:00
|
|
|
func NewChangeEvent(d *pb.Droplet, fname string, origval any, newval any) *pb.Event {
|
2024-10-24 21:39:07 -05:00
|
|
|
var e *pb.Event
|
|
|
|
e = new(pb.Event)
|
|
|
|
|
2024-10-25 03:34:39 -05:00
|
|
|
e.Droplet = d.Hostname
|
|
|
|
e.OrigVal = convertToString(origval)
|
|
|
|
e.NewVal = convertToString(newval)
|
2024-10-25 07:24:41 -05:00
|
|
|
e.FieldName = fname
|
2024-10-25 03:34:39 -05:00
|
|
|
|
|
|
|
// this also works, but it's a bit overkill
|
|
|
|
// e.NewAny = convertToAnypb(newval)
|
2024-10-25 03:11:55 -05:00
|
|
|
|
2024-10-25 04:30:35 -05:00
|
|
|
// me.events.Events = append(me.events.Events, e)
|
|
|
|
// stuff := me.events.FormatJSON()
|
|
|
|
// log.Info("events:", stuff)
|
2024-10-24 21:39:07 -05:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the droplet memory
|
|
|
|
func (d *DropletT) SetMemory(b int64) *pb.Event {
|
2024-10-25 03:11:55 -05:00
|
|
|
oldm := pb.HumanFormatBytes(d.pb.Memory)
|
|
|
|
newm := pb.HumanFormatBytes(b)
|
2024-10-24 21:39:07 -05:00
|
|
|
if d.pb.Memory == b {
|
2024-10-25 04:30:35 -05:00
|
|
|
// log.Info("droplet", d.pb.Hostname, "memory unchanged", oldm, "to", newm)
|
2024-10-25 03:34:39 -05:00
|
|
|
return nil
|
2024-10-24 21:39:07 -05:00
|
|
|
}
|
2024-10-25 03:11:55 -05:00
|
|
|
log.Info("droplet", d.pb.Hostname, "memory change from", oldm, "to", newm)
|
|
|
|
|
2024-10-25 04:30:35 -05:00
|
|
|
return NewChangeEvent(d.pb, "Droplet.Memory", d.pb.Memory, b)
|
2024-10-24 21:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the droplet memory
|
|
|
|
func (d *DropletT) SetCpus(b int64) {
|
|
|
|
log.Info("Set the number of cpus for the droplet", b)
|
|
|
|
}
|