# frozen_string_literal: true # Copyright (c) [2024-2026] SUSE LLC # # All Rights Reserved. # # This program is free software; you can redistribute it and/or modify it # under the terms of version 2 of the GNU General Public License as published # by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, contact SUSE LLC. # # To contact SUSE LLC about this file by physical or electronic mail, you may # find current contact information at www.suse.com. require "y2storage/proposal_settings" module Y2Storage module Proposal # Class to encapsulate the conversion from Agama config to ProposalSpaceSettings class SpaceSettingsBuilder # @param config [Agama::Storage::Config] def initialize(config) @config = config end # ProposalSpaceSettings corresponding to the Agama configuration # # @return [ProposalSpaceSettings] def space_settings Y2Storage::ProposalSpaceSettings.new.tap do |target| target.strategy = :bigger_resize target.actions = space_actions(config) end end private # @return [Agama::Storage::Config] attr_reader :config # Space actions from the given config. # # @param config [Agama::Storage::Config] # @return [Hash] def space_actions(config) actions = force_delete_actions(config) actions.concat(delete_actions(config)) actions.concat(resize_actions(config)) end # Space actions for devices that must be deleted. # # @param config [Agama::Storage::Config] # @return [Array] def force_delete_actions(config) configs = config_devices(config).select(&:delete?) names = device_names(configs) names.map { |d| Y2Storage::SpaceActions::Delete.new(d, mandatory: true) } end # Space actions for devices that might be deleted. # # @note #delete? takes precedence over #delete_if_needed?. # # @param config [Agama::Storage::Config] # @return [Array] def delete_actions(config) configs = config_devices(config).select(&:delete_if_needed?).reject(&:delete?) names = device_names(configs) names.map { |d| Y2Storage::SpaceActions::Delete.new(d) } end # Space actions for devices that might be resized # # @param config [Agama::Storage::Config] # @return [Array] def resize_actions(config) configs = config_devices(config).select(&:found_device).select(&:size) # Resize actions contain information that is potentially useful for the SpaceMaker even # when they are only about growing and not shrinking configs.map { |c| resize_action(c) }.compact end # @see #resize_actions # # @param dev [Agama::Storage::Configs::Partition, Agama::Storage::Configs::LogicalVolume] # @return [Y2Storage::SpaceActions::Resize, nil] def resize_action(dev) min = current_size?(dev, :min) ? nil : dev.size.min max = current_size?(dev, :max) ? nil : dev.size.max # If both min and max are equal to the current device size, there is nothing to do return unless min || max Y2Storage::SpaceActions::Resize.new(dev.found_device.name, min_size: min, max_size: max) end # @see #resize_actions def current_size?(dev, attr) dev.found_device.size == dev.size.public_send(attr) end # All partition and logical volume configs from the given config. # # @param config [Agama::Storage::Config] # @return [Array] def config_devices(config) config.partitions + config.logical_volumes end # Device names from the given configs. # # @param configs [Array<#found_device>] # @return [Array] def device_names(configs) configs .map(&:found_device) .compact .map(&:name) end end end end