Skip to content

Commit

Permalink
fix(utils): reverse lookup should also work on an ip address input wi…
Browse files Browse the repository at this point in the history
…th mask
  • Loading branch information
bl4ko committed Jul 31, 2024
1 parent 959cd34 commit 960b453
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions internal/source/vmware/vmware_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,10 @@ func (vc *VmwareSource) syncVMInterfaces(nbi *inventory.NetboxInventory, vmwareV
return fmt.Errorf("adding VmInterface: %s", err)
}

if netboxVM.Name == "csm-dev.src.si" {
print("hello")
}

vmIPv4Addresses, vmIPv6Addresses = vc.addVMInterfaceIPs(nbi, nbVMInterface, nicIPv4Addresses, nicIPv6Addresses, vmIPv4Addresses, vmIPv6Addresses)
}
}
Expand Down
12 changes: 11 additions & 1 deletion internal/utils/networking.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@ import (
// Function that receives ipAddress and performs a reverse lookup
// to get the hostname. If the reverse lookup fails, it returns an empty string.
func ReverseLookup(ipAddress string) string {
names, err := net.LookupAddr(ipAddress)
// Parse the IP address to handle cases where a subnet mask is provided
ip, _, err := net.ParseCIDR(ipAddress)
if err != nil {
// If parsing as CIDR fails, try to parse it as a plain IP address
ip = net.ParseIP(ipAddress)
if ip == nil {
return ""
}
}

names, err := net.LookupAddr(ip.String())
if err != nil || len(names) == 0 {
return ""
}
Expand Down
21 changes: 21 additions & 0 deletions internal/utils/networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,34 @@ func TestReverseLookup(t *testing.T) {
},
want: "",
},
{
name: "Valid rerse lookup",
args: args{
ipAddress: "1.1.1.1",
},
want: "one.one.one.one",
},
{
name: "Valid reverse lookup",
args: args{
ipAddress: "8.8.8.8",
},
want: "dns.google",
},
{
name: "Lookup IP with mask",
args: args{
ipAddress: "8.8.8.8/24",
},
want: "dns.google",
},
{
name: "Lookup IP with wrong mask should fail",
args: args{
ipAddress: "8.8.8.8/36",
},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 960b453

Please sign in to comment.