Skip to content

Commit

Permalink
storage: read size current from JSON config
Browse files Browse the repository at this point in the history
  • Loading branch information
joseivanlopez committed Sep 23, 2024
1 parent e57abc2 commit be2c56e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
6 changes: 2 additions & 4 deletions service/lib/agama/storage/config_conversions/from_json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,19 @@ module Storage
module ConfigConversions
# Config conversion from JSON hash according to schema.
class FromJSON
# @todo Replace product_config param by a ProductDefinition.
#
# @param config_json [Hash]
# @param product_config [Agama::Config]
def initialize(config_json, product_config:)
# TODO: Replace product_config param by a ProductDefinition.
@config_json = config_json
@product_config = product_config
end

# Performs the conversion from Hash according to the JSON schema.
#
# @todo Raise error if config_json does not match the JSON schema.
#
# @return [Storage::Config]
def convert
# TODO: Raise error if config_json does not match the JSON schema.
config = FromJSONConversions::Config
.new(config_json, config_builder: config_builder)
.convert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,30 +55,48 @@ def convert(default = nil)
def conversions(_default)
{
default: false,
min: convert_size(:min),
max: convert_size(:max) || Y2Storage::DiskSize.unlimited
min: convert_min_size,
max: convert_max_size
}
end

# @return [Y2Storage::DiskSize, nil]
def convert_size(field)
def convert_min_size
value = case size_json
when Hash
size_json[field]
size_json[:min]
when Array
field == :max ? size_json[1] : size_json[0]
size_json[0]
else
size_json
end

return unless value
disk_size(value)
end

begin
# This parses without legacy_units, ie. "1 GiB" != "1 GB"
Y2Storage::DiskSize.new(value)
rescue TypeError
# JSON schema validations should prevent this from happening
# @return [Y2Storage::DiskSize, nil]
def convert_max_size
value = case size_json
when Hash
size_json[:max]
when Array
size_json[1]
else
size_json
end

return Y2Storage::DiskSize.unlimited unless value

disk_size(value)
end

# @param value [String, Integer] e.g., "2 GiB".
# @return [Y2Storage::DiskSize, nil] nil if value is "current".
def disk_size(value)
return if value == "current"

# This parses without legacy_units, ie. "1 GiB" != "1 GB".
Y2Storage::DiskSize.new(value)
end
end
end
Expand Down

0 comments on commit be2c56e

Please sign in to comment.