Add validation for disk configs

This commit is contained in:
Brian Cain 2019-10-23 14:07:40 -07:00
parent a18ce4f732
commit 995c4bbc60
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 33 additions and 6 deletions

View File

@ -8,6 +8,8 @@ module VagrantPlugins
# Config class for a given Disk
#-------------------------------------------------------------------
DEFAULT_DISK_TYPES = [:disk, :dvd, :floppy].freeze
# Note: This value is for internal use only
#
# @return [String]
@ -29,17 +31,20 @@ module VagrantPlugins
# @return [String]
attr_accessor :name
# Type of disk to create
# Type of disk to create. Defaults to `:disk`
#
# @return [Symbol]
attr_accessor :type
# Size of disk to create
#
# TODO: Should we have shortcuts for GB???
#
# @return [Integer]
attr_accessor :size
# Determines if this disk is the _main_ disk, or an attachment. Defaults to true
# Determines if this disk is the _main_ disk, or an attachment.
# Defaults to true.
#
# @return [Boolean]
attr_accessor :primary
@ -60,8 +65,9 @@ module VagrantPlugins
def initialize(type)
@logger = Log4r::Logger.new("vagrant::config::vm::trigger::config")
@type = type
@name = UNSET_VALUE
@type = UNSET_VALUE
@provider_type = UNSET_VALUE
@size = UNSET_VALUE
@primary = UNSET_VALUE
@ -102,11 +108,13 @@ module VagrantPlugins
def finalize!
# Ensure all config options are set to nil or default value if untouched
# by user
@name = nil if @name == UNSET_VALUE
@type = nil if @type == UNSET_VALUE
@type = :disk if @type == UNSET_VALUE
@size = nil if @size == UNSET_VALUE
@primary = true if @primary == UNSET_VALUE
# generate name instead of nil if unset_value
@name = nil if @name == UNSET_VALUE
@config = nil if @config == UNSET_VALUE
end
@ -116,6 +124,15 @@ module VagrantPlugins
# validate type with list of known disk types
if !DEFAULT_DISK_TYPES.include?(@type)
errors << "Disk type '#{@type}' is not a valid type. Please pick one of the following supported disk types: #{DEFAULT_DISK_TYPES.join(', ')}"
end
# TODO: Convert a string to int here?
if !@size.is_a?(Integer)
errors << "Config option size for disk is not an integer"
end
errors
end

View File

@ -580,7 +580,6 @@ module VagrantPlugins
end
end
# TODO: This might need to be more complicated
@__disks.each do |d|
d.finalize!
end
@ -786,6 +785,17 @@ module VagrantPlugins
end
end
# Validate disks
# Check if there is more than one primrary disk defined and throw an error
if @__disks.select { |d| d.primary && d.type == :disk }.size > 1
errors << "There is more than one disk defined for guest '#{machine.name}'. Please pick a `primary` disk."
end
@__disks.each do |d|
error = d.validate(machine)
errors.concat error if !error.empty?
end
# We're done with VM level errors so prepare the section
errors = { "vm" => errors }