module IDRAC::Storage

def find_controller(name_pattern: "PERC", prefer_most_drives_by_count: false, prefer_most_drives_by_size: false)

Returns:
  • (Hash) - The selected controller

Parameters:
  • prefer_most_drives_by_size (Boolean) -- Prefer controllers with larger total drive capacity
  • prefer_most_drives_by_count (Boolean) -- Prefer controllers with more drives
  • name_pattern (String) -- Regex pattern to match controller name (defaults to "PERC")
def find_controller(name_pattern: "PERC", prefer_most_drives_by_count: false, prefer_most_drives_by_size: false)
  all_controllers = controllers
  return nil if all_controllers.empty?
  
  # Filter by name pattern if provided
  if name_pattern
    pattern_matches = all_controllers.select { |c| c["name"] && c["name"].include?(name_pattern) }
    return pattern_matches.first if pattern_matches.any?
  end
  
  selected_controller = nil
  
  # If we prefer controllers by drive count
  if prefer_most_drives_by_count
    selected_controller = all_controllers.max_by { |c| c["drives_count"] || 0 }
  end
  
  # If we prefer controllers by total drive size
  if prefer_most_drives_by_size && !selected_controller
    # We need to calculate total drive size for each controller
    controller_with_most_capacity = nil
    max_capacity = -1
    
    all_controllers.each do |controller|
      # Get the drives for this controller
      controller_drives = begin
        drives(controller["@odata.id"])
      rescue
        [] # If we can't get drives, assume empty
      end
      
      # Calculate total capacity
      total_capacity = controller_drives.sum { |d| d["capacity_bytes"] || 0 }
      
      if total_capacity > max_capacity
        max_capacity = total_capacity
        controller_with_most_capacity = controller
      end
    end
    
    selected_controller = controller_with_most_capacity if controller_with_most_capacity
  end
  
  # Default to first controller if no preferences matched
  selected_controller || all_controllers.first
end