99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
// "reflect"
|
|
|
|
"errors"
|
|
"fmt"
|
|
|
|
"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"
|
|
)
|
|
|
|
func convertToAnypb(x any) *anypb.Any {
|
|
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:
|
|
log.Error(errors.New("convertToAnypb() unknown type"), "v =", v, "x =", x)
|
|
return nil
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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))
|
|
case uint:
|
|
return fmt.Sprintf("%d", x.(uint))
|
|
case bool:
|
|
if x.(bool) {
|
|
return "true"
|
|
}
|
|
return "false"
|
|
default:
|
|
log.Info("convertToSTring() unknown type", v)
|
|
log.Error(errors.New("convertToSTring() unknown type"), "v =", v, "x =", x)
|
|
return ""
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Wrapping the int into a protobuf message
|
|
func NewChangeEvent(d *pb.Droplet, fname string, origval any, newval any) *pb.Event {
|
|
var e *pb.Event
|
|
e = new(pb.Event)
|
|
|
|
e.Droplet = d.Hostname
|
|
e.OrigVal = convertToString(origval)
|
|
e.NewVal = convertToString(newval)
|
|
|
|
// this also works, but it's a bit overkill
|
|
// e.NewAny = convertToAnypb(newval)
|
|
|
|
// me.events.Events = append(me.events.Events, e)
|
|
// stuff := me.events.FormatJSON()
|
|
// log.Info("events:", stuff)
|
|
return e
|
|
}
|
|
|
|
// update the droplet memory
|
|
func (d *DropletT) SetMemory(b int64) *pb.Event {
|
|
oldm := pb.HumanFormatBytes(d.pb.Memory)
|
|
newm := pb.HumanFormatBytes(b)
|
|
if d.pb.Memory == b {
|
|
// log.Info("droplet", d.pb.Hostname, "memory unchanged", oldm, "to", newm)
|
|
return nil
|
|
}
|
|
log.Info("droplet", d.pb.Hostname, "memory change from", oldm, "to", newm)
|
|
|
|
return NewChangeEvent(d.pb, "Droplet.Memory", d.pb.Memory, b)
|
|
}
|
|
|
|
// update the droplet memory
|
|
func (d *DropletT) SetCpus(b int64) {
|
|
log.Info("Set the number of cpus for the droplet", b)
|
|
}
|