Skip to content

Commit

Permalink
feature(storage): config conversion to JSON (#1670)
Browse files Browse the repository at this point in the history
Add conversion of storage config to JSON format.

This will be needed for reporting the "solved" config once the *guided
proposal* is replaced by the *agama proposal*.
  • Loading branch information
joseivanlopez authored Oct 18, 2024
2 parents c64ad2f + 12fb25d commit ac56617
Show file tree
Hide file tree
Showing 25 changed files with 2,273 additions and 0 deletions.
1 change: 1 addition & 0 deletions service/lib/agama/storage/config_conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# find current contact information at www.suse.com.

require "agama/storage/config_conversions/from_json"
require "agama/storage/config_conversions/to_json"

module Agama
module Storage
Expand Down
48 changes: 48 additions & 0 deletions service/lib/agama/storage/config_conversions/to_json.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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/config_conversions/to_json_conversions/config"

module Agama
module Storage
module ConfigConversions
# Config conversion to JSON hash according to schema.
class ToJSON
# @param config [Storage::Config]
def initialize(config)
@config = config
end

# Performs the conversion to Hash according to the JSON schema.
#
# @return [Hash]
def convert
ToJSONConversions::Config.new(config).convert
end

private

# @return [Storage::Config]
attr_reader :config
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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/config_conversions/from_json_conversions/base"
require "agama/storage/config_conversions/from_json_conversions/boot"
require "agama/storage/config_conversions/from_json_conversions/config"
require "agama/storage/config_conversions/from_json_conversions/drive"
require "agama/storage/config_conversions/from_json_conversions/encryption"
require "agama/storage/config_conversions/from_json_conversions/filesystem"
require "agama/storage/config_conversions/from_json_conversions/logical_volume"
require "agama/storage/config_conversions/from_json_conversions/luks1"
require "agama/storage/config_conversions/from_json_conversions/luks2"
require "agama/storage/config_conversions/from_json_conversions/partition"
require "agama/storage/config_conversions/from_json_conversions/pervasive_luks2"
require "agama/storage/config_conversions/from_json_conversions/search"
require "agama/storage/config_conversions/from_json_conversions/size"
require "agama/storage/config_conversions/from_json_conversions/volume_group"

module Agama
module Storage
module ConfigConversions
# Conversions to JSON.
module ToJSONConversions
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# 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.

module Agama
module Storage
module ConfigConversions
module ToJSONConversions
# Base class for conversions to JSON hash according to schema.
class Base
# Defines the expected config type to perform the conversion.
#
# @raise If a subclass does not defines a type.
# @return [Class]
def self.config_type
raise "Undefined config type"
end

# @param config [Object] The config type is provided by the {.config_type} method.
def initialize(config)
type = self.class.config_type
raise "Invalid config (#{type} expected): #{config}" unless config.is_a?(type)

@config = config
end

# Performs the conversion to Hash according to the JSON schema.
#
# @return [Hash, nil]
def convert
config_json = {}

conversions.each do |property, value|
next if value.nil?

config_json[property] = value
end

config_json.empty? ? nil : config_json
end

private

# @return [Object] The config type is provided by the {.config_type} method.
attr_reader :config

# Values to generate the JSON.
#
# @return [Hash] e.g., { name: "/dev/vda" }.
def conversions
{}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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/config_conversions/to_json_conversions/base"
require "agama/storage/configs/logical_volume"

module Agama
module Storage
module ConfigConversions
module ToJSONConversions
# Boot conversion to JSON hash according to schema.
class Boot < Base
# @see Base
def self.config_type
Configs::Boot
end

private

# @see Base#conversions
def conversions
{
configure: config.configure?,
device: config.device
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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/config"
require "agama/storage/config_conversions/to_json_conversions/base"
require "agama/storage/config_conversions/to_json_conversions/boot"
require "agama/storage/config_conversions/to_json_conversions/drive"
require "agama/storage/config_conversions/to_json_conversions/volume_group"

module Agama
module Storage
module ConfigConversions
module ToJSONConversions
# Config conversion to JSON hash according to schema.
class Config < Base
# @see Base
def self.config_type
Storage::Config
end

private

# @see Base#conversions
def conversions
{
boot: convert_boot,
drives: convert_drives,
volumeGroups: convert_volume_groups
}
end

# @return [Hash]
def convert_boot
ToJSONConversions::Boot.new(config.boot).convert
end

# @return [Array<Hash>]
def convert_drives
config.drives.map { |d| ToJSONConversions::Drive.new(d).convert }
end

# @return [Array<Hash>]
def convert_volume_groups
config.volume_groups.map { |v| ToJSONConversions::VolumeGroup.new(v).convert }
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 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/config_conversions/to_json_conversions/base"
require "agama/storage/config_conversions/to_json_conversions/with_encryption"
require "agama/storage/config_conversions/to_json_conversions/with_filesystem"
require "agama/storage/config_conversions/to_json_conversions/with_partitions"
require "agama/storage/config_conversions/to_json_conversions/with_ptable_type"
require "agama/storage/config_conversions/to_json_conversions/with_search"
require "agama/storage/configs/drive"

module Agama
module Storage
module ConfigConversions
module ToJSONConversions
# Drive conversion to JSON hash according to schema.
class Drive < Base
include WithSearch
include WithEncryption
include WithFilesystem
include WithPtableType
include WithPartitions

# @see Base
def self.config_type
Configs::Drive
end

private

# @see Base#conversions
def conversions
{
search: convert_search,
alias: config.alias,
encryption: convert_encryption,
filesystem: convert_filesystem,
ptableType: convert_ptable_type,
partitions: convert_partitions
}
end
end
end
end
end
end
Loading

0 comments on commit ac56617

Please sign in to comment.