diff --git a/app/models/ems_refresh/save_inventory_physical_infra.rb b/app/models/ems_refresh/save_inventory_physical_infra.rb index d8d5df4fd6e..85c4940273a 100644 --- a/app/models/ems_refresh/save_inventory_physical_infra.rb +++ b/app/models/ems_refresh/save_inventory_physical_infra.rb @@ -27,6 +27,7 @@ def save_ems_physical_infra_inventory(ems, hashes, target = nil) # Save and link other subsections save_child_inventory(ems, hashes, child_keys, target) + discover_ip_physical_infra(ems) ems.save! hashes[:id] = ems.id @@ -58,4 +59,35 @@ def save_asset_details_inventory(parent, hash) return if hash.nil? save_inventory_single(:asset_details, parent, hash) end + + def ipaddress?(hostname) + IPAddr.new(hostname) + return true + rescue + return false + end + + def resolve_hostname(ipaddress, ems) + ems.hostname = "https://#{Resolv.getname(ipaddress)}/" + _log.info("EMS ID: #{ems.id}" + " Resolved hostname successfully.") + rescue => err + _log.warn("EMS ID: #{ems.id}" + " It's not possible resolve hostname of the physical infra, #{err}.") + end + + def resolve_ip_address(hostname, ems) + ems.ipaddress = Resolv.getaddress(hostname) + _log.info("EMS ID: #{ems.id}" + " Resolved ip address successfully.") + rescue => err + _log.warn("EMS ID: #{ems.id}" + " It's not possible resolve ip address of the physical infra, #{err}.") + end + + def discover_ip_physical_infra(ems) + hostname = URI.parse(ems.hostname).host || URI.parse(ems.hostname).path + if ems.ipaddress.blank? + resolve_ip_address(hostname, ems) + end + if ipaddress?(hostname) + resolve_hostname(hostname, ems) + end + end end