Skip to content

Commit

Permalink
feat(storage): config from model
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Dec 2, 2024
1 parent a55a65e commit bb85ecc
Show file tree
Hide file tree
Showing 17 changed files with 1,778 additions and 1 deletion.
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/from_model"
require "agama/storage/config_conversions/to_json"
require "agama/storage/config_conversions/to_model"

Expand Down
50 changes: 50 additions & 0 deletions service/lib/agama/storage/config_conversions/from_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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/config"
require "agama/storage/config_conversions/from_model_conversions/config"

module Agama
module Storage
module ConfigConversions
# Config conversion from model according to the JSON schema.
class FromModel
# @param model_json [Hash]
def initialize(model_json)
@model_json = model_json
end

# Performs the conversion from model according to the JSON schema.
#
# @return [Storage::Config]
def convert
# TODO: Raise error if model_json does not match the JSON schema.
FromModelConversions::Config.new(model_json).convert
end

private

# @return [Hash]
attr_reader :model_json
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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_model_conversions/config"
require "agama/storage/config_conversions/from_model_conversions/drive"
require "agama/storage/config_conversions/from_model_conversions/filesystem"
require "agama/storage/config_conversions/from_model_conversions/filesystem_type"
require "agama/storage/config_conversions/from_model_conversions/partition"
require "agama/storage/config_conversions/from_model_conversions/search"
require "agama/storage/config_conversions/from_model_conversions/size"

module Agama
module Storage
module ConfigConversions
# Conversions from model according to the JSON schema.
module FromModelConversions
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# 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 FromModelConversions
# Base class for conversions from model according to the JSON schema.
class Base
# @param model_json [Hash]
def initialize(model_json)
@model_json = model_json
end

# Performs the conversion from model according to the JSON schema.
#
# @return [Object] A {Config} or any its configs from {Storage::Configs}.
def convert
config = default_config

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

config.public_send("#{property}=", value)
end

config
end

private

# @return [Hash]
attr_reader :model_json

# Default config object (defined by derived classes).
#
# @return [Object]
def default_config
raise "Undefined default config"
end

# Values to apply to the config.
#
# @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,65 @@
# 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_model_conversions/base"
require "agama/storage/config_conversions/from_model_conversions/drive"
require "agama/storage/config"

module Agama
module Storage
module ConfigConversions
module FromModelConversions
# Config conversion from model according to the JSON schema.
class Config < Base
private

# @see Base
# @return [Storage::Config]
def default_config
Storage::Config.new
end

# @see Base#conversions
# @return [Hash]
def conversions
{
drives: convert_drives
}
end

# @return [Array<Configs::Drive>, nil]
def convert_drives
drive_models = model_json[:drives]
return unless drive_models

drive_models.map { |d| convert_drive(d) }
end

# @param drive_model [Hash]
# @return [Configs::Drive]
def convert_drive(drive_model)
FromModelConversions::Drive.new(drive_model).convert
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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_model_conversions/base"
require "agama/storage/config_conversions/from_model_conversions/with_filesystem"
require "agama/storage/config_conversions/from_model_conversions/with_partitions"
require "agama/storage/config_conversions/from_model_conversions/with_ptable_type"
require "agama/storage/config_conversions/from_model_conversions/with_search"
require "agama/storage/configs/drive"

module Agama
module Storage
module ConfigConversions
module FromModelConversions
# Drive conversion from model according to the JSON schema.
class Drive < Base
private

include WithFilesystem
include WithPtableType
include WithPartitions
include WithSearch

alias_method :drive_model, :model_json

# @see Base
# @return [Configs::Drive]
def default_config
Configs::Drive.new
end

# @see Base#conversions
# @return [Hash]
def conversions
{
search: convert_search,
alias: drive_model[:alias],
filesystem: convert_filesystem,
ptable_type: convert_ptable_type,
partitions: convert_partitions
}
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 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_model_conversions/base"
require "agama/storage/config_conversions/from_model_conversions/filesystem_type"
require "agama/storage/configs/filesystem"

module Agama
module Storage
module ConfigConversions
module FromModelConversions
# Filesystem conversion from model according to the JSON schema.
class Filesystem < Base
private

# @see Base
# @return [Configs::Filesystem]
def default_config
Configs::Filesystem.new
end

# @see Base#conversions
# @return [Hash]
def conversions
{
path: model_json[:mountPath],
type: convert_type
}
end

# @return [Configs::FilesystemType, nil]
def convert_type
filesystem_model = model_json[:filesystem]
return if filesystem_model.nil?

FromModelConversions::FilesystemType.new(filesystem_model).convert
end
end
end
end
end
end
Loading

0 comments on commit bb85ecc

Please sign in to comment.