Skip to content

Commit

Permalink
chore(storage): rename conversions (#1407)
Browse files Browse the repository at this point in the history
* Rename conversions from `ToSchema` and `FromSchema` to `ToJSON` and
`FromJSON`.
* The namespaces `ProposalSettingsConversion` and `VolumeConversion` are
renamed as `ProposalSettingsConversions` and `VolumeConversions`.
* Conversion methods are moved from the namespace modules to the
`ProposalSettings` and `Volume` classes.

Note: conversions under `Agama::DBus::Storage` namespace are not
modified because they are going to be removed in future iterations.
  • Loading branch information
joseivanlopez authored Jun 28, 2024
2 parents accf894 + a3bc41e commit fc04e35
Show file tree
Hide file tree
Showing 29 changed files with 361 additions and 434 deletions.
40 changes: 19 additions & 21 deletions service/lib/agama/dbus/storage/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
require "agama/dbus/interfaces/service_status"
require "agama/dbus/storage/devices_tree"
require "agama/dbus/storage/iscsi_nodes_tree"
require "agama/dbus/storage/proposal_settings_conversion"
require "agama/dbus/storage/proposal"
require "agama/dbus/storage/proposal_settings_conversion"
require "agama/dbus/storage/volume_conversion"
require "agama/dbus/storage/with_iscsi_auth"
require "agama/dbus/with_service_status"
require "agama/storage/encryption_settings"
require "agama/storage/proposal_settings_conversion"
require "agama/storage/proposal_settings"
require "agama/storage/volume_templates_builder"

Yast.import "Arch"
Expand Down Expand Up @@ -99,12 +99,12 @@ def probe
# AutoYaST settings: { "storage": ... } vs { "legacyAutoyastStorage": ... }.
def apply_storage_config(serialized_config)
@serialized_storage_config = serialized_config
storage_config = JSON.parse(serialized_config, symbolize_names: true)
config_json = JSON.parse(serialized_config, symbolize_names: true)

if (guided_settings = storage_config.dig(:storage, :guided))
calculate_guided_proposal(guided_settings)
elsif (autoyast_settings = storage_config[:legacyAutoyastStorage])
calculate_autoyast_proposal(autoyast_settings)
if (settings_json = config_json.dig(:storage, :guided))
calculate_guided_proposal(settings_json)
elsif (settings_json = config_json[:legacyAutoyastStorage])
calculate_autoyast_proposal(settings_json)
else
raise "Invalid config: #{serialized_config}"
end
Expand Down Expand Up @@ -259,11 +259,10 @@ def proposal_result
return {} unless proposal.calculated?

if proposal.strategy?(ProposalStrategy::GUIDED)
settings = Agama::Storage::ProposalSettingsConversion.to_schema(proposal.settings)
{
"success" => proposal.success?,
"strategy" => ProposalStrategy::GUIDED,
"settings" => settings.to_json
"settings" => proposal.settings.to_json_settings.to_json
}
else
{
Expand Down Expand Up @@ -400,16 +399,15 @@ def proposal

# Calculates a guided proposal.
#
# @param settings [Hash] Settings according to the JSON schema.
# @param settings_json [Hash] JSON settings according to schema.
# @return [Integer] 0 success; 1 error
def calculate_guided_proposal(settings)
proposal_settings = Agama::Storage::ProposalSettingsConversion.from_schema(
settings, config: config
)
def calculate_guided_proposal(settings_json)
proposal_settings = Agama::Storage::ProposalSettings
.new_from_json(settings_json, config: config)

logger.info(
"Calculating guided storage proposal from D-Bus.\n" \
"Input settings: #{settings}\n" \
"Input settings: #{settings_json}\n" \
"Agama settings: #{proposal_settings.inspect}"
)

Expand All @@ -419,15 +417,15 @@ def calculate_guided_proposal(settings)

# Calculates an AutoYaST proposal.
#
# @param settings [Hash] AutoYaST settings.
# @param settings_json [Hash] AutoYaST settings.
# @return [Integer] 0 success; 1 error
def calculate_autoyast_proposal(settings)
def calculate_autoyast_proposal(settings_json)
# Ensures keys are strings.
autoyast_settings = JSON.parse(settings.to_json)
autoyast_settings = JSON.parse(settings_json.to_json)

logger.info(
"Calculating AutoYaST storage proposal from D-Bus.\n" \
"Input settings: #{settings}\n" \
"Input settings: #{settings_json}\n" \
"AutoYaST settings: #{autoyast_settings}"
)

Expand All @@ -437,12 +435,12 @@ def calculate_autoyast_proposal(settings)

# Generates the storage config from the current proposal, if any.
#
# @return [Hash] Storage config according to the JSON schema.
# @return [Hash] Storage config according to JSON schema.
def generate_storage_config
if proposal.strategy?(ProposalStrategy::GUIDED)
{
storage: {
guided: Agama::Storage::ProposalSettingsConversion.to_schema(proposal.settings)
guided: proposal.settings.to_json_settings
}
}
elsif proposal.strategy?(ProposalStrategy::AUTOYAST)
Expand Down
26 changes: 26 additions & 0 deletions service/lib/agama/storage/proposal_settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require "agama/storage/boot_settings"
require "agama/storage/device_settings"
require "agama/storage/encryption_settings"
require "agama/storage/proposal_settings_conversions"
require "agama/storage/space_settings"

module Agama
Expand Down Expand Up @@ -85,6 +86,31 @@ def default_boot_device
end
end

# Creates a new proposal settings object from JSON hash according to schema.
#
# @param settings_json [Hash]
# @param config [Config]
#
# @return [ProposalSettings]
def self.new_from_json(settings_json, config:)
Storage::ProposalSettingsConversions::FromJSON.new(settings_json, config: config).convert
end

# Generates a JSON hash according to schema.
#
# @return [Hash]
def to_json_settings
Storage::ProposalSettingsConversions::ToJSON.new(self).convert
end

# Generates Y2Storage proposal settings.
#
# @param config [Config]
# @return [Y2Storage::ProposalSettings]
def to_y2storage(config:)
Storage::ProposalSettingsConversions::ToY2Storage.new(self, config: config).convert
end

private

# Device used for booting.
Expand Down
70 changes: 0 additions & 70 deletions service/lib/agama/storage/proposal_settings_conversion.rb

This file was deleted.

33 changes: 33 additions & 0 deletions service/lib/agama/storage/proposal_settings_conversions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# Copyright (c) [2024] 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 "agama/storage/proposal_settings_conversions/from_json"
require "agama/storage/proposal_settings_conversions/from_y2storage"
require "agama/storage/proposal_settings_conversions/to_json"
require "agama/storage/proposal_settings_conversions/to_y2storage"

module Agama
module Storage
# Conversions for the proposal settings.
module ProposalSettingsConversions
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
require "agama/storage/encryption_settings"
require "agama/storage/proposal_settings_reader"
require "agama/storage/space_settings"
require "agama/storage/volume_conversion"
require "agama/storage/volume"
require "y2storage/encryption_method"
require "y2storage/pbkd_function"

module Agama
module Storage
module ProposalSettingsConversion
# Proposal settings conversion from Hash according to the JSON schema.
class FromSchema
# @param schema_settings [Hash]
module ProposalSettingsConversions
# Proposal settings conversion from JSON hash according to schema.
class FromJSON
# @param settings_json [Hash]
# @param config [Config]
def initialize(schema_settings, config:)
# @todo Raise error if schema_settings does not match the JSON schema.
@schema_settings = schema_settings
def initialize(settings_json, config:)
@settings_json = settings_json
@config = config
end

# Performs the conversion from Hash according to the JSON schema.
#
# @return [ProposalSettings]
def convert
# @todo Raise error if settings_json does not match the JSON schema.
device_settings = target_conversion
boot_settings = boot_conversion
encryption_settings = encryption_conversion
Expand All @@ -63,63 +63,63 @@ def convert
private

# @return [Hash]
attr_reader :schema_settings
attr_reader :settings_json

# @return [Config]
attr_reader :config

def target_conversion
target_schema = schema_settings[:target]
return unless target_schema
target_json = settings_json[:target]
return unless target_json

if target_schema == "disk"
if target_json == "disk"
Agama::Storage::DeviceSettings::Disk.new
elsif target_schema == "newLvmVg"
elsif target_json == "newLvmVg"
Agama::Storage::DeviceSettings::NewLvmVg.new
elsif (device = target_schema[:disk])
elsif (device = target_json[:disk])
Agama::Storage::DeviceSettings::Disk.new(device)
elsif (devices = target_schema[:newLvmVg])
elsif (devices = target_json[:newLvmVg])
Agama::Storage::DeviceSettings::NewLvmVg.new(devices)
end
end

def boot_conversion
boot_schema = schema_settings[:boot]
return unless boot_schema
boot_json = settings_json[:boot]
return unless boot_json

Agama::Storage::BootSettings.new.tap do |boot_settings|
boot_settings.configure = boot_schema[:configure]
boot_settings.device = boot_schema[:device]
boot_settings.configure = boot_json[:configure]
boot_settings.device = boot_json[:device]
end
end

def encryption_conversion
encryption_schema = schema_settings[:encryption]
return unless encryption_schema
encryption_json = settings_json[:encryption]
return unless encryption_json

Agama::Storage::EncryptionSettings.new.tap do |encryption_settings|
encryption_settings.password = encryption_schema[:password]
encryption_settings.password = encryption_json[:password]

if (method_value = encryption_schema[:method])
if (method_value = encryption_json[:method])
method = Y2Storage::EncryptionMethod.find(method_value.to_sym)
encryption_settings.method = method
end

if (function_value = encryption_schema[:pbkdFunction])
if (function_value = encryption_json[:pbkdFunction])
function = Y2Storage::PbkdFunction.find(function_value)
encryption_settings.pbkd_function = function
end
end
end

def space_conversion
space_schema = schema_settings[:space]
return unless space_schema
space_json = settings_json[:space]
return unless space_json

Agama::Storage::SpaceSettings.new.tap do |space_settings|
space_settings.policy = space_schema[:policy].to_sym
space_settings.policy = space_json[:policy].to_sym

actions_value = space_schema[:actions] || []
actions_value = space_json[:actions] || []
space_settings.actions = actions_value.map { |a| action_conversion(a) }.inject(:merge)
end
end
Expand All @@ -132,11 +132,11 @@ def action_conversion(action)
end

def volumes_conversion
volumes_schema = schema_settings[:volumes]
return [] unless volumes_schema
volumes_json = settings_json[:volumes]
return [] unless volumes_json

volumes_schema.map do |volume_schema|
VolumeConversion.from_schema(volume_schema, config: config)
volumes_json.map do |volume_json|
Volume.new_from_json(volume_json, config: config)
end
end

Expand Down
Loading

0 comments on commit fc04e35

Please sign in to comment.