From ff87935d4aa563ad3443e9ad8a45200a963a874f Mon Sep 17 00:00:00 2001 From: Lucas Ritzdorf <42657792+LRitzdorf@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:14:36 -0600 Subject: [PATCH] smdclient: implement xname lookup from IP address --- internal/smdclient/SMDclient.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/smdclient/SMDclient.go b/internal/smdclient/SMDclient.go index 33ae839..db5bf04 100644 --- a/internal/smdclient/SMDclient.go +++ b/internal/smdclient/SMDclient.go @@ -105,6 +105,22 @@ func (s *SMDClient) IDfromMAC(mac string) (string, error) { return "", errors.New("MAC " + mac + " not found for an xname in EthernetInterfaces") } +// IDfromMAC returns the ID of the xname that has the MAC address +func (s *SMDClient) IDfromIP(ipaddr string) (string, error) { + endpointData := new(CompEthInterfaceV2Array) + ep := "/hsm/v2/Inventory/EthernetInterfaces/" + s.getSMD(ep, endpointData) + + for _, ep := range endpointData.Array { + for _, v := range ep.IPAddrs { + if strings.EqualFold(ipaddr, v.IPAddr) { + return ep.CompID, nil + } + } + } + return "", errors.New("IP address " + ipaddr + " not found for an xname in EthernetInterfaces") +} + // GroupMembership returns the group labels for the xname with the given ID func (s *SMDClient) GroupMembership(id string) ([]string, error) { ml := new(sm.Membership)