add windows support to network adapter

This commit is contained in:
Christoph Hartmann 2015-10-08 13:01:09 +02:00
parent 153c670952
commit 9d92abf524
4 changed files with 89 additions and 0 deletions

View file

@ -20,6 +20,8 @@ class NetworkInterface < Vulcano.resource(1)
@interface_provider = nil
if vulcano.os.linux?
@interface_provider = LinuxInterface.new(vulcano)
elsif vulcano.os.windows?
@interface_provider = WindowsInterface.new(vulcano)
else
return skip_resource 'The `interface` resource is not supported on your OS yet.'
end
@ -86,3 +88,35 @@ class LinuxInterface < InterfaceInfo
}
end
end
class WindowsInterface < InterfaceInfo
def interface_info(iface)
# gather all network interfaces
cmd = @vulcano.command('Get-NetAdapter | Select-Object -Property Name, InterfaceDescription, Status, State, MacAddress, LinkSpeed, ReceiveLinkSpeed, TransmitLinkSpeed, Virtual | ConvertTo-Json')
# filter network interface
begin
net_adapter = JSON.parse(cmd.stdout)
rescue JSON::ParserError => _e
return nil
end
# ensure we have an array of groups
net_adapter = [net_adapter] if !net_adapter.is_a?(Array)
# select the requested interface
adapters = net_adapter.each_with_object([]) do |adapter, adapter_collection|
# map object
info = {
name: adapter['Name'],
up: adapter['State'] == 2,
speed: adapter['ReceiveLinkSpeed'] / 1000,
}
adapter_collection.push(info) if info[:name].casecmp(iface) == 0
end
return nil if adapters.size == 0
warn "[Possible Error] detected multiple network interfaces with the name #{iface}" if adapters.size > 1
adapters[0]
end
end

View file

@ -147,6 +147,7 @@ class MockLoader
# network interface
'9e80f048a1af5a0f6ab8a465e46ea5ed5ba6587e9b5e54a7a0c0a1a02bb6f663' => cmd.call('find-net-interface'),
'c33821dece09c8b334e03a5bb9daefdf622007f73af4932605e758506584ec3f' => empty.call,
'Get-NetAdapter | Select-Object -Property Name, InterfaceDescription, Status, State, MacAddress, LinkSpeed, ReceiveLinkSpeed, TransmitLinkSpeed, Virtual | ConvertTo-Json' => cmd.call('Get-NetAdapter'),
}
# set os emulation

View file

@ -0,0 +1,24 @@
[
{
"Name": "vEthernet (Intel(R) PRO 1000 MT Network Connection - Virtual Switch)",
"InterfaceDescription": "Hyper-V Virtual Ethernet Adapter #2",
"Status": "Up",
"State": 2,
"MacAddress": "00-0C-29-E3-48-9B",
"LinkSpeed": "10 Gbps",
"ReceiveLinkSpeed": 10000000000,
"TransmitLinkSpeed": 10000000000,
"Virtual": true
},
{
"Name": "Ethernet0",
"InterfaceDescription": "Intel(R) PRO/1000 MT Network Connection",
"Status": "Not Present",
"State": 3,
"MacAddress": "00-0C-29-E3-48-9B",
"LinkSpeed": "0 bps",
"ReceiveLinkSpeed": 0,
"TransmitLinkSpeed": 0,
"Virtual": false
}
]

View file

@ -21,4 +21,34 @@ describe 'Vulcano::Resources::Group' do
_(resource.up?).must_equal false
_(resource.speed).must_equal nil
end
it 'verify interface on windows' do
resource = MockLoader.new(:windows).load_resource('interface', 'ethernet0')
_(resource.exists?).must_equal true
_(resource.up?).must_equal false
_(resource.speed).must_equal 0
end
it 'verify interface on windows' do
resource = MockLoader.new(:windows).load_resource('interface', 'vEthernet (Intel(R) PRO 1000 MT Network Connection - Virtual Switch)')
_(resource.exists?).must_equal true
_(resource.up?).must_equal true
_(resource.speed).must_equal 10000000
end
it 'verify invalid interface on windows' do
resource = MockLoader.new(:windows).load_resource('interface', 'eth1')
_(resource.exists?).must_equal false
_(resource.up?).must_equal false
_(resource.speed).must_equal nil
end
# undefined
it 'verify interface on unsupported os' do
resource = MockLoader.new(:undefined).load_resource('interface', 'eth0')
_(resource.exists?).must_equal false
_(resource.up?).must_equal false
_(resource.speed).must_equal nil
end
end