Added config option to specify VLanId for Network Adapter when using HyperV provider

This commit is contained in:
Tomas Srnka 2015-01-11 18:04:17 +01:00
parent 49d1a92bf6
commit 2a487af68d
6 changed files with 65 additions and 0 deletions

View File

@ -116,6 +116,7 @@ module VagrantPlugins
end end
b2.use Provision b2.use Provision
b2.use NetSetVLan
b2.use StartInstance b2.use StartInstance
b2.use WaitForIPAddress b2.use WaitForIPAddress
b2.use WaitForCommunicator, [:running] b2.use WaitForCommunicator, [:running]
@ -216,6 +217,7 @@ module VagrantPlugins
autoload :StopInstance, action_root.join('stop_instance') autoload :StopInstance, action_root.join('stop_instance')
autoload :SuspendVM, action_root.join("suspend_vm") autoload :SuspendVM, action_root.join("suspend_vm")
autoload :WaitForIPAddress, action_root.join("wait_for_ip_address") autoload :WaitForIPAddress, action_root.join("wait_for_ip_address")
autoload :NetSetVLan, action_root.join("net_set_vlan")
autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy") autoload :MessageWillNotDestroy, action_root.join("message_will_not_destroy")
end end
end end

View File

@ -0,0 +1,19 @@
module VagrantPlugins
module HyperV
module Action
class NetSetVLan
def initialize(app, env)
@app = app
end
def call(env)
vlan_id = env[:machine].provider_config.vlan_id
env[:ui].info("[Settings] [Network Adapter] Setting Vlan ID to: #{vlan_id}")
env[:machine].provider.driver.net_set_vlan(vlan_id)
@app.call(env)
end
end
end
end
end

View File

@ -9,14 +9,24 @@ module VagrantPlugins
# @return [Integer] # @return [Integer]
attr_accessor :ip_address_timeout attr_accessor :ip_address_timeout
# The default VLAN ID for network interface for the virtual machine.
#
# @return [Integer]
attr_accessor :vlan_id
def initialize def initialize
@ip_address_timeout = UNSET_VALUE @ip_address_timeout = UNSET_VALUE
@vlan_id = UNSET_VALUE
end end
def finalize! def finalize!
if @ip_address_timeout == UNSET_VALUE if @ip_address_timeout == UNSET_VALUE
@ip_address_timeout = 120 @ip_address_timeout = 120
end end
if @vlan_id == UNSET_VALUE
@vlan_id = 0
end
end end
def validate(machine) def validate(machine)

View File

@ -77,6 +77,10 @@ module VagrantPlugins
execute('import_vm.ps1', options) execute('import_vm.ps1', options)
end end
def net_set_vlan(vlan_id)
execute("set_network_vlan.ps1", { VmId: vm_id, VlanId: vlan_id })
end
protected protected
def execute_powershell(path, options, &block) def execute_powershell(path, options, &block)

View File

@ -0,0 +1,18 @@
param (
[string]$VmId = $(throw "-VmId is required."),
[int]$VlanId = $(throw "-VlanId ")
)
# Include the following modules
$presentDir = Split-Path -parent $PSCommandPath
$modules = @()
$modules += $presentDir + "\utils\write_messages.ps1"
forEach ($module in $modules) { . $module }
try {
$vm = Get-VM -Id $VmId -ErrorAction "stop"
Set-VMNetworkAdapterVlan $vm -Access -Vlanid $VlanId
}
catch {
Write-Error-Message "Failed to set VM's Vlan ID $_"
}

View File

@ -14,5 +14,17 @@ describe VagrantPlugins::HyperV::Config do
subject.finalize! subject.finalize!
expect(subject.ip_address_timeout).to eq(120) expect(subject.ip_address_timeout).to eq(120)
end end
describe "#vlan_id" do
it "can be set" do
subject.vlan_id = 100
subject.finalize!
expect(subject.vlan_id).to eq(100)
end
it "defaults to a number" do
subject.finalize!
expect(subject.vlan_id).to eq(0)
end
end end
end end