- //Read the dhcp lease file
- dhcpFile, err := ioutil.ReadFile(dhcpLeaseFilePath)
- if err != nil {
- fmt.Printf("Failed to read lease file\n")
- return "", err
- }
-
- dhcpLeases := string(dhcpFile)
-
- //Regex to use to search dhcpLeases
- reg := "lease.*{|ethernet.*|\n. binding state.*"
- re, err := regexp.Compile(reg)
- if err != nil {
- fmt.Printf("Could not create Regexp object, Error %v occured\n", err)
- return "", err
- }
-
- //Get String containing leased Ip addresses and Corressponding MAC addresses
- out := re.FindAllString(dhcpLeases, -1)
- outString := strings.Join(out, " ")
- stringReplacer := strings.NewReplacer("lease", "", "ethernet ", "", ";", "",
- " binding state", "", "{", "")
- replaced := stringReplacer.Replace(outString)
- ipMacList := strings.Fields(replaced)
-
- //Get IP addresses corresponding to Input MAC Address
- for idx := len(ipMacList) - 1; idx >= 0; idx-- {
- item := ipMacList[idx]
- if item == macAddress {
-
- leaseState := ipMacList[idx-1]
- if leaseState != "active" {
- err := fmt.Errorf("No active ip address lease found for MAC address %s \n", macAddress)
- fmt.Printf("%v\n", err)
- return "", err
+ for _, bareMetalHost := range bareMetalHostList.Items {
+ status, ok := bareMetalHost.Object["status"].(map[string]interface{})
+ if !ok {
+ continue
+ }
+ hardware, ok := status["hardware"].(map[string]interface{})
+ if !ok {
+ continue
+ }
+ nics, ok := hardware["nics"].([]interface{})
+ if !ok {
+ continue
+ }
+ for _, nic := range nics {
+ n, ok := nic.(map[string]interface{})
+ if !ok {
+ continue
+ }
+ ip, ok := n["ip"].(string)
+ if !ok {
+ continue
+ }
+ if macAddress == n["mac"] {
+ return ip, nil