Skip to content

Commit

Permalink
Merge pull request juju#11607 from manadart/2.8-interface-infos-hwaddr
Browse files Browse the repository at this point in the history
juju#11607

## Description of change

This patch adds a simple utility method for retrieving a device by MAC address from `InterfaceInfos`.

## QA steps

Covered by unit tests. QA to follow with a patch using this method.

## Documentation changes

None.

## Bug reference

N/A
  • Loading branch information
jujubot authored May 20, 2020
2 parents 43b96c2 + b344551 commit d84e4ec
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 12 additions & 0 deletions core/network/nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,18 @@ func (s InterfaceInfos) Children(parentName string) InterfaceInfos {
return children
}

// GetByHardwareAddress returns a reference to the interface with the input
// hardware address (such as a MAC address) if it exists in the collection,
// otherwise nil is returned.
func (s InterfaceInfos) GetByHardwareAddress(hwAddr string) *InterfaceInfo {
for _, dev := range s {
if dev.MACAddress == hwAddr {
return &dev
}
}
return nil
}

// ProviderInterfaceInfo holds enough information to identify an
// interface or link layer device to a provider so that it can be
// queried or manipulated. Its initial purpose is to pass to
Expand Down
10 changes: 9 additions & 1 deletion core/network/nic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var _ = gc.Suite(&nicSuite{})

func (s *nicSuite) SetUpTest(_ *gc.C) {
s.info = network.InterfaceInfos{
{VLANTag: 1, DeviceIndex: 0, InterfaceName: "eth0"},
{VLANTag: 1, DeviceIndex: 0, InterfaceName: "eth0", MACAddress: "00:16:3e:aa:bb:cc"},
{VLANTag: 0, DeviceIndex: 1, InterfaceName: "eth1"},
{VLANTag: 42, DeviceIndex: 2, InterfaceName: "br2"},
{ConfigType: network.ConfigDHCP, NoAutoStart: true},
Expand Down Expand Up @@ -133,6 +133,14 @@ func (*nicSuite) TestInterfaceInfosIterHierarchy(c *gc.C) {
})
}

func (s *nicSuite) TestInterfaceInfosGetByHardwareAddress(c *gc.C) {
dev := s.info.GetByHardwareAddress("not-there")
c.Assert(dev, gc.IsNil)

dev = s.info.GetByHardwareAddress("00:16:3e:aa:bb:cc")
c.Assert(dev.InterfaceName, gc.Equals, "eth0")
}

func getInterFaceInfos() network.InterfaceInfos {
return network.InterfaceInfos{
{
Expand Down

0 comments on commit d84e4ec

Please sign in to comment.