-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
storage: add config conversion to JSON
- Loading branch information
1 parent
4cefd9b
commit 62a51f9
Showing
23 changed files
with
1,248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
45 changes: 45 additions & 0 deletions
45
service/lib/agama/storage/config_conversions/to_json_conversions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
74 changes: 74 additions & 0 deletions
74
service/lib/agama/storage/config_conversions/to_json_conversions/base.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
49 changes: 49 additions & 0 deletions
49
service/lib/agama/storage/config_conversions/to_json_conversions/boot.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
68 changes: 68 additions & 0 deletions
68
service/lib/agama/storage/config_conversions/to_json_conversions/config.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
64 changes: 64 additions & 0 deletions
64
service/lib/agama/storage/config_conversions/to_json_conversions/drive.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.