diff --git a/add.go b/add.go index 96df708..25b6248 100644 --- a/add.go +++ b/add.go @@ -125,6 +125,6 @@ func (c *Cluster) BlankFields() { } } -func (c *Cluster) AppendEvent(e *Event) { - c.Events = append(c.Events, e) +func (epb *Events) AppendEvent(e *Event) { + epb.Events = append(epb.Events, e) } diff --git a/change.go b/change.go index f7e57a3..3a788af 100644 --- a/change.go +++ b/change.go @@ -153,7 +153,7 @@ func (c *Cluster) ChangeDropletState(d *Droplet, newState DropletState) error { now := time.Now() e.Start = timestamppb.New(now) - c.Events = append(c.Events, e) + c.E.Events = append(c.E.Events, e) return nil } @@ -185,7 +185,7 @@ func (c *Cluster) DropletMoved(d *Droplet, newh *Hypervisor) error { now := time.Now() e.Start = timestamppb.New(now) - c.Events = append(c.Events, e) + c.E.Events = append(c.E.Events, e) // update the droplet record d.CurrentHypervisor = newh.Hostname diff --git a/cluster.proto b/cluster.proto index 0d07d5a..0c2616d 100644 --- a/cluster.proto +++ b/cluster.proto @@ -8,7 +8,12 @@ import "event.proto"; message Cluster { int64 id = 1; repeated string dirs = 2; + repeated Droplet droplets = 3; repeated Hypervisor hypervisors = 4; - repeated Event events = 5; + // repeated Event events = 5; + + Droplets d = 6; + Hypervisors h = 7; + Events e = 8; } diff --git a/config.go b/config.go index 5528731..1d7ca3d 100644 --- a/config.go +++ b/config.go @@ -47,14 +47,11 @@ func (c *Cluster) ConfigSave() error { return err } - var e *Events - e = new(Events) - e.Events = c.Events - if err := ConfigWriteJSON(e, "events.json"); err != nil { + if err := ConfigWriteJSON(c.E, "events.json"); err != nil { fmt.Println("events.json write failed") return err } - if err := ConfigWriteTEXT(e, "events.text"); err != nil { + if err := ConfigWriteTEXT(c.E, "events.text"); err != nil { fmt.Println("events.json write failed") return err } @@ -68,7 +65,7 @@ func (c *Cluster) ConfigSave() error { newc.Dirs = c.Dirs newc.Droplets = nil newc.Hypervisors = nil - newc.Events = nil + newc.E = nil if err := ConfigWriteTEXT(&newc, "cluster.text"); err != nil { fmt.Println("cluster.json write failed") return err @@ -88,45 +85,6 @@ func backupConfigFiles() error { return backupFiles(srcDir, destDir) } -/* -func (c *Cluster) ConfigLoadOld2() error { - if c == nil { - return errors.New("It's not safe to run ConfigLoad() on a nil cluster") - } - - // erase or zero fields that shouldn't ever be written to the config file - c.BlankFields() - - // load the cluster config file - if data, err := loadFile("virtigo.json"); err == nil { - if err = protojson.Unmarshal(data, c); err != nil { - fmt.Println("broken cluster.json config file") - fmt.Println(err) - return errors.New("cluster.json file is broken") - } - } else { - return err - } - - var e *Events - e = new(Events) - // load the events config file - if data, err := loadFile("events.json"); err == nil { - if err = protojson.Unmarshal(data, e); err != nil { - fmt.Println("broken events.json config file") - return err - } - } else { - return err - } - // copy them over. is this needed? does the memory free otherwise? - for _, a := range e.Events { - c.Events = append(c.Events, a) - } - return nil -} -*/ - func (c *Cluster) ConfigLoad() error { if c == nil { return errors.New("It's not safe to run ConfigLoad() on a nil cluster") @@ -177,20 +135,51 @@ func (c *Cluster) ConfigLoad() error { c.Hypervisors = append(c.Hypervisors, a) } - var e *Events - e = new(Events) - // load the events config file - if data, err := loadFile("events.json"); err == nil { - if err = protojson.Unmarshal(data, e); err != nil { - fmt.Println("broken events.json config file") - return err - } - } else { + if err := c.loadEvents(); err != nil { return err } - // copy them over. is this needed? does the memory free otherwise? - for _, a := range e.Events { - c.Events = append(c.Events, a) + return nil +} + +func (c *Cluster) loadEvents() error { + var data []byte + var err error + + if c == nil { + fmt.Println("cluster == nil") + os.Exit(-1) + } + + if c.E == nil { + fmt.Println("cluster.E == nil") + c.E = new(Events) + } + + if c.E == nil { + fmt.Println("cluster.E == nil") + os.Exit(-1) + } + + // load the events config file + if data, err = loadFile("events.json"); err != nil { + fmt.Println("broken events.json config file") + return err + } + + err = protojson.Unmarshal(data, c.E) + if err != nil { + fmt.Println("broken events.json config file") + // json load failed. try loading prototext + if data, err = loadFile("events.text"); err != nil { + fmt.Println("broken events.text config file") + fmt.Println(err) + return errors.New("events.text file is broken") + } + if err = prototext.Unmarshal(data, c.E); err != nil { + fmt.Println("broken events.text config file") + fmt.Println(err) + return errors.New("events.text file is broken") + } } return nil } diff --git a/helpers.go b/helpers.go index 49f8cfb..6a8fe69 100644 --- a/helpers.go +++ b/helpers.go @@ -6,6 +6,7 @@ package virtbuf import ( "google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/prototext" + "google.golang.org/protobuf/proto" ) // human readable JSON @@ -77,3 +78,7 @@ func (d *Droplet) UnmarshalJSON(data []byte) error { func (e *Events) UnmarshalJSON(data []byte) error { return protojson.Unmarshal(data, e) } + +func (d *Droplet) Unmarshal(data []byte) error { + return proto.Unmarshal(data, d) +} diff --git a/test.proto b/test.proto new file mode 100644 index 0000000..8079240 --- /dev/null +++ b/test.proto @@ -0,0 +1,8 @@ +syntax = "proto3"; +package virtbuf; + +message Cluster { + int64 id = 1; + repeated string s = 2; + repeated int i = 3; +}