From f8ba7c9bc3ddce2dfdf0376a4c9fbd978b44e52e Mon Sep 17 00:00:00 2001 From: Maximilian Wilhelm Date: Thu, 26 Oct 2023 18:18:13 +0200 Subject: [PATCH] .toProto() methods should return nil values for empty slices Signed-off-by: Maximilian Wilhelm --- pkg/connector/netbox/netbox_test.go | 113 ++++++---------------------- pkg/model/device.go | 27 ++++--- pkg/model/device_test.go | 8 +- pkg/model/interface.go | 27 ++++--- pkg/model/interface_test.go | 3 +- pkg/model/topology.go | 55 ++++++++------ 6 files changed, 94 insertions(+), 139 deletions(-) diff --git a/pkg/connector/netbox/netbox_test.go b/pkg/connector/netbox/netbox_test.go index 7acab6c..2f974d0 100644 --- a/pkg/connector/netbox/netbox_test.go +++ b/pkg/connector/netbox/netbox_test.go @@ -68,18 +68,11 @@ func TestEnrichment(t *testing.T) { Name: "DUS01", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), Devices: []*octopuspb.Device{ { - Name: "ccr01.dus01", - Role: "ccr", - SiteName: "DUS01", - Interfaces: []*octopuspb.Interface{}, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, + Name: "ccr01.dus01", + Role: "ccr", + SiteName: "DUS01", }, }, }, @@ -256,10 +249,6 @@ func TestEnrichment(t *testing.T) { Name: "DUS01", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), Devices: []*octopuspb.Device{ { Name: "GCP", @@ -267,17 +256,14 @@ func TestEnrichment(t *testing.T) { SiteName: "ANY", Interfaces: []*octopuspb.Interface{ { - Name: "Interconnect0", - Type: "10gbase-x-sfpp", - Units: []*octopuspb.InterfaceUnit{}, + Name: "Interconnect0", + Type: "10gbase-x-sfpp", MetaData: &octopuspb.MetaData{ SemanticTags: map[string]string{}, Tags: []string{"net:foo"}, }, }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, MetaData: &octopuspb.MetaData{ SemanticTags: map[string]string{ "NET:ASN": "16550", @@ -295,10 +281,8 @@ func TestEnrichment(t *testing.T) { Type: "10gbase-x-sfpp", Units: []*octopuspb.InterfaceUnit{ { - Id: 100, - InnerTag: 100, - Ipv4Addresses: []*octopuspb.IPAddress{}, - Ipv6Addresses: []*octopuspb.IPAddress{}, + Id: 100, + InnerTag: 100, }, }, }, @@ -306,22 +290,17 @@ func TestEnrichment(t *testing.T) { Name: "Ethernet0/1", Type: "10gbase-x-sfpp", LagMemberOf: "bond0", - Units: []*octopuspb.InterfaceUnit{}, }, { Name: "Ethernet0/2", Type: "10gbase-x-sfpp", LagMemberOf: "bond0", - Units: []*octopuspb.InterfaceUnit{}, }, { - Name: "bond0", - Type: "lag", - Units: []*octopuspb.InterfaceUnit{}, + Name: "bond0", + Type: "lag", }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, }, }, @@ -452,10 +431,6 @@ func TestEnrichment(t *testing.T) { Name: "DUS01", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), Devices: []*octopuspb.Device{ { Name: "GCP", @@ -473,7 +448,6 @@ func TestEnrichment(t *testing.T) { IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 0), 31).ToProto(), }, }, - Ipv6Addresses: []*octopuspb.IPAddress{}, }, }, MetaData: &octopuspb.MetaData{ @@ -482,8 +456,6 @@ func TestEnrichment(t *testing.T) { }, }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, { Name: "ccr01.dus01", @@ -501,13 +473,10 @@ func TestEnrichment(t *testing.T) { IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 1), 31).ToProto(), }, }, - Ipv6Addresses: []*octopuspb.IPAddress{}, }, { - Id: 100, - InnerTag: 100, - Ipv4Addresses: []*octopuspb.IPAddress{}, - Ipv6Addresses: []*octopuspb.IPAddress{}, + Id: 100, + InnerTag: 100, }, }, }, @@ -515,22 +484,17 @@ func TestEnrichment(t *testing.T) { Name: "Ethernet0/1", Type: "10gbase-x-sfpp", LagMemberOf: "bond0", - Units: []*octopuspb.InterfaceUnit{}, }, { Name: "Ethernet0/2", Type: "10gbase-x-sfpp", LagMemberOf: "bond0", - Units: []*octopuspb.InterfaceUnit{}, }, { - Name: "bond0", - Type: "lag", - Units: []*octopuspb.InterfaceUnit{}, + Name: "bond0", + Type: "lag", }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, }, }, @@ -652,40 +616,30 @@ func TestEnrichment(t *testing.T) { Name: "SiteA", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), Devices: []*octopuspb.Device{ { Name: "devA", SiteName: "SiteA", Interfaces: []*octopuspb.Interface{ { - Name: "ifaA", - Units: make([]*octopuspb.InterfaceUnit, 0), + Name: "ifaA", }, { - Name: "ifaX", - Units: make([]*octopuspb.InterfaceUnit, 0), + Name: "ifaX", }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, { Name: "devB", SiteName: "SiteA", Interfaces: []*octopuspb.Interface{ { - Name: "ifaB", - Units: make([]*octopuspb.InterfaceUnit, 0), + Name: "ifaB", }, { - Name: "ifaY", - Units: make([]*octopuspb.InterfaceUnit, 0), + Name: "ifaY", }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, }, Cables: []*octopuspb.Cable{ @@ -796,10 +750,6 @@ func TestEnrichment(t *testing.T) { Name: "DUS01", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), Devices: []*octopuspb.Device{ { Name: "ccr01.dus01", @@ -827,13 +777,10 @@ func TestEnrichment(t *testing.T) { IP: bnet.NewPfx(bnet.IPv4FromOctets(192, 0, 2, 2), 32).ToProto(), }, }, - Ipv6Addresses: []*octopuspb.IPAddress{}, }, }, }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, }, }, @@ -886,16 +833,11 @@ func TestEnrichment(t *testing.T) { Name: "DUS01", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), Devices: []*octopuspb.Device{ { - Name: "pp01.dus01", - Role: "pp", - SiteName: "DUS01", - Interfaces: []*octopuspb.Interface{}, + Name: "pp01.dus01", + Role: "pp", + SiteName: "DUS01", FrontPorts: []*octopuspb.FrontPort{ { Name: "FP-A", @@ -976,10 +918,7 @@ func TestEnrichment(t *testing.T) { Name: "DUS01", }, }, - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), + Devices: []*octopuspb.Device{ { Name: "ccr01.dus01", @@ -998,13 +937,10 @@ func TestEnrichment(t *testing.T) { IP: bnet.NewPfx(bnet.IPv4FromOctets(169, 254, 0, 0), 31).Ptr().ToProto(), }, }, - Ipv6Addresses: make([]*octopuspb.IPAddress, 0), }, }, }, }, - FrontPorts: []*octopuspb.FrontPort{}, - RearPorts: []*octopuspb.RearPort{}, }, }, }, @@ -1039,12 +975,7 @@ func TestEnrichment(t *testing.T) { Prefixes: make(map[int64]*model.Prefix), }, expected: &octopuspb.Topology{ - Sites: make([]*octopuspb.Site, 0), - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), - Devices: make([]*octopuspb.Device, 0), + Devices: make([]*octopuspb.Device, 0), Prefixes: []*octopuspb.Prefix{ { Prefix: bnet.NewPfx(bnet.IPv4FromOctets(100, 64, 0, 0), 26).ToProto(), diff --git a/pkg/model/device.go b/pkg/model/device.go index 71baebc..425e51a 100644 --- a/pkg/model/device.go +++ b/pkg/model/device.go @@ -64,9 +64,9 @@ func (d *Device) ToProto() *octopuspb.Device { Platform: d.Platform, DeviceType: d.DeviceType, - Interfaces: make([]*octopuspb.Interface, 0), - FrontPorts: make([]*octopuspb.FrontPort, 0), - RearPorts: make([]*octopuspb.RearPort, 0), + Interfaces: nil, + FrontPorts: nil, + RearPorts: nil, MetaData: d.MetaData.ToProto(), } @@ -78,16 +78,25 @@ func (d *Device) ToProto() *octopuspb.Device { protoDev.SiteName = d.Site.Name } - for _, iface := range d.Interfaces { - protoDev.Interfaces = append(protoDev.Interfaces, iface.ToProto()) + if len(d.Interfaces) > 0 { + protoDev.Interfaces = make([]*octopuspb.Interface, 0) + for _, iface := range d.Interfaces { + protoDev.Interfaces = append(protoDev.Interfaces, iface.ToProto()) + } } - for _, fp := range d.FrontPorts { - protoDev.FrontPorts = append(protoDev.FrontPorts, fp.ToProto()) + if len(d.FrontPorts) > 0 { + protoDev.FrontPorts = make([]*octopuspb.FrontPort, 0) + for _, fp := range d.FrontPorts { + protoDev.FrontPorts = append(protoDev.FrontPorts, fp.ToProto()) + } } - for _, rp := range d.RearPorts { - protoDev.RearPorts = append(protoDev.RearPorts, rp.ToProto()) + if len(d.RearPorts) > 0 { + protoDev.RearPorts = make([]*octopuspb.RearPort, 0) + for _, rp := range d.RearPorts { + protoDev.RearPorts = append(protoDev.RearPorts, rp.ToProto()) + } } return protoDev diff --git a/pkg/model/device_test.go b/pkg/model/device_test.go index 67b11a3..2c8701f 100644 --- a/pkg/model/device_test.go +++ b/pkg/model/device_test.go @@ -129,10 +129,7 @@ func TestDeviceToProto(t *testing.T) { name: "Empty device", device: NewDevice("foo"), protoDevice: &octopuspb.Device{ - Name: "foo", - Interfaces: []*octopuspb.Interface{}, - FrontPorts: make([]*octopuspb.FrontPort, 0), - RearPorts: make([]*octopuspb.RearPort, 0), + Name: "foo", }, }, { @@ -154,13 +151,10 @@ func TestDeviceToProto(t *testing.T) { }, }, }, - Ipv6Addresses: []*octopuspb.IPAddress{}, }, }, }, }, - FrontPorts: make([]*octopuspb.FrontPort, 0), - RearPorts: make([]*octopuspb.RearPort, 0), }, }, } diff --git a/pkg/model/interface.go b/pkg/model/interface.go index 7c1bb00..9c16fb1 100644 --- a/pkg/model/interface.go +++ b/pkg/model/interface.go @@ -85,14 +85,17 @@ func (iface *Interface) ToProto() *octopuspb.Interface { protoIface := &octopuspb.Interface{ Name: iface.Name, - Units: make([]*octopuspb.InterfaceUnit, 0), + Units: nil, Type: iface.Type, LagMemberOf: iface.LAGMemberOf, MetaData: iface.MetaData.ToProto(), } - for _, unit := range iface.Units { - protoIface.Units = append(protoIface.Units, unit.ToProto()) + if len(iface.Units) > 0 { + protoIface.Units = make([]*octopuspb.InterfaceUnit, 0) + for _, unit := range iface.Units { + protoIface.Units = append(protoIface.Units, unit.ToProto()) + } } return protoIface @@ -105,19 +108,25 @@ func (unit *InterfaceUnit) ToProto() *octopuspb.InterfaceUnit { protoUnit := &octopuspb.InterfaceUnit{ Id: unit.ID, - Ipv4Addresses: make([]*octopuspb.IPAddress, 0), - Ipv6Addresses: make([]*octopuspb.IPAddress, 0), + Ipv4Addresses: nil, + Ipv6Addresses: nil, MetaData: unit.MetaData.ToProto(), OuterTag: uint32(unit.OuterTag), InnerTag: uint32(unit.InnerTag), } - for _, IP := range unit.IPv4Addresses { - protoUnit.Ipv4Addresses = append(protoUnit.Ipv4Addresses, IP.ToProto()) + if len(unit.IPv4Addresses) > 0 { + protoUnit.Ipv4Addresses = make([]*octopuspb.IPAddress, 0) + for _, IP := range unit.IPv4Addresses { + protoUnit.Ipv4Addresses = append(protoUnit.Ipv4Addresses, IP.ToProto()) + } } - for _, IP := range unit.IPv6Addresses { - protoUnit.Ipv6Addresses = append(protoUnit.Ipv6Addresses, IP.ToProto()) + if len(unit.IPv6Addresses) > 0 { + protoUnit.Ipv6Addresses = make([]*octopuspb.IPAddress, 0) + for _, IP := range unit.IPv6Addresses { + protoUnit.Ipv6Addresses = append(protoUnit.Ipv6Addresses, IP.ToProto()) + } } return protoUnit diff --git a/pkg/model/interface_test.go b/pkg/model/interface_test.go index e5538dc..2863106 100644 --- a/pkg/model/interface_test.go +++ b/pkg/model/interface_test.go @@ -138,8 +138,7 @@ func TestInterfaceToProto(t *testing.T) { name: "Empty interface", iface: newInterface("foo"), protoIface: &octopuspb.Interface{ - Name: "foo", - Units: []*octopuspb.InterfaceUnit{}, + Name: "foo", }, }, diff --git a/pkg/model/topology.go b/pkg/model/topology.go index 08e32de..04319ec 100644 --- a/pkg/model/topology.go +++ b/pkg/model/topology.go @@ -101,52 +101,65 @@ func (t *Topology) ToProto() *octopuspb.Topology { return nil } - topology := &octopuspb.Topology{ + protoTopology := &octopuspb.Topology{ Timestamp: uint64(t.Timestamp.Unix()), - Sites: make([]*octopuspb.Site, 0), - Pops: make([]*octopuspb.Pop, 0), - Colos: make([]*octopuspb.Colo, 0), Devices: make([]*octopuspb.Device, 0), - Cables: make([]*octopuspb.Cable, 0), - Circuits: make([]*octopuspb.Circuit, 0), } for _, dev := range t.Nodes { - topology.Devices = append(topology.Devices, dev.ToProto()) + protoTopology.Devices = append(protoTopology.Devices, dev.ToProto()) } - for _, site := range t.Sites { - topology.Sites = append(topology.Sites, site.ToProto()) + if len(t.Sites) > 0 { + protoTopology.Sites = make([]*octopuspb.Site, 0) + for _, site := range t.Sites { + protoTopology.Sites = append(protoTopology.Sites, site.ToProto()) + } } - for _, pop := range t.Pops { - topology.Pops = append(topology.Pops, pop.ToProto()) + if len(t.Pops) > 0 { + protoTopology.Pops = make([]*octopuspb.Pop, 0) + for _, pop := range t.Pops { + protoTopology.Pops = append(protoTopology.Pops, pop.ToProto()) + } } - for _, colo := range t.Colos { - topology.Colos = append(topology.Colos, colo.ToProto()) + if len(t.Colos) > 0 { + protoTopology.Colos = make([]*octopuspb.Colo, 0) + for _, colo := range t.Colos { + protoTopology.Colos = append(protoTopology.Colos, colo.ToProto()) + } } - for _, cable := range t.Cables { - topology.Cables = append(topology.Cables, cable.ToProto()) + if len(t.Cables) > 0 { + protoTopology.Cables = make([]*octopuspb.Cable, 0) + for _, cable := range t.Cables { + protoTopology.Cables = append(protoTopology.Cables, cable.ToProto()) + } } - for _, prefix := range t.Prefixes { - topology.Prefixes = append(topology.Prefixes, prefix.ToProto()) + if len(t.Circuits) > 0 { + for _, ckt := range t.Circuits { + protoTopology.Circuits = append(protoTopology.Circuits, ckt.ToProto()) + } } - for _, ckt := range t.Circuits { - topology.Circuits = append(topology.Circuits, ckt.ToProto()) + if len(t.Prefixes) > 0 { + protoTopology.Prefixes = make([]*octopuspb.Prefix, 0) + for _, prefix := range t.Prefixes { + protoTopology.Prefixes = append(protoTopology.Prefixes, prefix.ToProto()) + } } - sortTopology(topology) - return topology + sortTopology(protoTopology) + return protoTopology } func sortTopology(topology *octopuspb.Topology) { sort.Slice(topology.Devices, func(i, j int) bool { return topology.Devices[i].Name < topology.Devices[j].Name }) + for _, d := range topology.Devices { sort.Slice(d.Interfaces, func(i, j int) bool { return d.Interfaces[i].Name < d.Interfaces[j].Name