Add provider specific disk options under provider_config hash

This commit is contained in:
Brian Cain 2019-10-31 11:41:51 -07:00
parent 6c54bf6ad9
commit e598007237
No known key found for this signature in database
GPG Key ID: 9FC4639B2E4510A0
2 changed files with 32 additions and 44 deletions

View File

@ -48,59 +48,39 @@ module VagrantPlugins
# Provider specific options
#
# This should work similar to how a "Provisioner" class works:
#
# - This class is the base class where as this options value is actually a
# provider specific class for the given options for that provider, if required
#
# - Hopefully in general the top-scope disk options are enough for the general
# case that most people won't need provider specific options except for very specific cases
#
# @return [Object]
attr_accessor :config
# @return [Hash]
attr_accessor :provider_config
def initialize(type)
@logger = Log4r::Logger.new("vagrant::config::vm::disk")
@type = type
@provider_config = {}
@name = UNSET_VALUE
@provider_type = UNSET_VALUE
@size = UNSET_VALUE
@primary = UNSET_VALUE
@config = nil
@invalid = false
@file = UNSET_VALUE
# Internal options
@id = SecureRandom.uuid
end
# find disk provider plugin
# Need to pass in provider or figure out provider here
@config_class = nil
# @invalid = true if provider not found
if !@config_class
@logger.info(
"Disk config for '#{@provider_type}' not found. Ignoring config.")
@config_class = Vagrant::Config::V2::DummyConfig
def add_provider_config(**options, &block)
current = {}
options.each do |k,v|
opts = k.to_s.split("__")
if opts.size == 2
current[opts[0]] = opts[1]
else
@logger.warn("Disk option '#{k}' found that does not match expected schema")
end
end
end
def add_config(**options, &block)
return if invalid?
current = @config_class.new
current.set_options(options) if options
block.call(current) if block_given?
current = @config.merge(current) if @config
@config = current
end
# Returns true or false if disk provider is found
#
# @return [Bool]
def invalid?
@invalid
#block.call(current) if block
current = @provider_config.merge(current) if !@provider_config.empty?
@provider_config = current
end
def finalize!
@ -120,7 +100,7 @@ module VagrantPlugins
# TODO: Name not required if primary?
@name = "vagrant_#{@type.to_s}_#{@id.split("-").last}" if @name == UNSET_VALUE
@config = nil if @config == UNSET_VALUE
@provider_config = nil if @provider_config == {}
end
# @return [Array] array of strings of error messages from config option validation

View File

@ -410,18 +410,26 @@ module VagrantPlugins
end
def disk(type, **options, &block)
disk = VagrantConfigDisk.new(type)
disk_config = VagrantConfigDisk.new(type)
disk.set_options(options)
# Remove provider__option options before set_options, otherwise will
# show up as missing setting
#
# We can probably combine this into a single method call...?
provider_options = options.select { |k,v| k.to_s.include?("__") }
options.delete_if { |k,v| k.to_s.include?("__") }
if block_given?
block.call(disk, VagrantConfigDisk)
end
disk_config.set_options(options)
# Can't use blocks if we use provider__option
#if block_given?
# block.call(disk, VagrantConfigDisk)
#end
# Add provider config
disk.add_config(options, &block)
disk_config.add_provider_config(provider_options, &block)
@disks << disk
@disks << disk_config
end
#-------------------------------------------------------------------