diff options
-rw-r--r-- | lib/facter/util/vlans.rb | 24 | ||||
-rw-r--r-- | lib/facter/vlans.rb | 8 | ||||
-rw-r--r-- | spec/unit/data/linux_vlan_config | 6 | ||||
-rw-r--r-- | spec/unit/util/vlans.rb | 14 |
4 files changed, 52 insertions, 0 deletions
diff --git a/lib/facter/util/vlans.rb b/lib/facter/util/vlans.rb new file mode 100644 index 0000000..6d226ff --- /dev/null +++ b/lib/facter/util/vlans.rb @@ -0,0 +1,24 @@ +# A module to gather vlan facts +# +module Facter::Util::Vlans + def self.get_vlan_config + output = "" + if File.exists?('/proc/net/vlan/config') and File.readable?('/proc/net/vlan/config') + output = File.open('/proc/net/vlan/config').read + end + output + end + + def self.get_vlans + vlans = Array.new + if self.get_vlan_config + self.get_vlan_config.each do |line| + if line =~ /^([0-9A-Za-z]+)\.([0-9]+) / + vlans.insert(-1, $~[2]) if $~[2] + end + end + end + + vlans.join(',') + end +end diff --git a/lib/facter/vlans.rb b/lib/facter/vlans.rb new file mode 100644 index 0000000..d65bdd8 --- /dev/null +++ b/lib/facter/vlans.rb @@ -0,0 +1,8 @@ +require 'facter/util/vlans' + + Facter.add("vlans") do + confine :kernel => :linux + setcode do + Facter::Util::Vlans.get_vlans + end + end diff --git a/spec/unit/data/linux_vlan_config b/spec/unit/data/linux_vlan_config new file mode 100644 index 0000000..3aa7dbd --- /dev/null +++ b/spec/unit/data/linux_vlan_config @@ -0,0 +1,6 @@ +VLAN Dev name | VLAN ID +Name-Type: VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD +eth0.400 | 400 | eth0 +eth0.300 | 300 | eth0 +eth0.200 | 200 | eth0 +eth0.100 | 100 | eth0 diff --git a/spec/unit/util/vlans.rb b/spec/unit/util/vlans.rb new file mode 100644 index 0000000..e06a2af --- /dev/null +++ b/spec/unit/util/vlans.rb @@ -0,0 +1,14 @@ +#!/usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../spec_helper' + +require 'facter/util/vlans' + +describe Facter::Util::Vlans do + it "should return a list of vlans on Linux" do + sample_output_file = File.dirname(__FILE__) + '/../data/linux_vlan_config' + linux_vlanconfig = File.new(sample_output_file).read(); + Facter::Util::Vlans.stubs(:get_vlan_config).returns(linux_vlanconfig) + Facter::Util::Vlans.get_vlans().should == %{400,300,200,100} + end +end |