diff --git a/lib/puppet/parser/functions/get_network_role_property.rb b/lib/puppet/parser/functions/get_network_role_property.rb index 70c6497..84290ee 100644 --- a/lib/puppet/parser/functions/get_network_role_property.rb +++ b/lib/puppet/parser/functions/get_network_role_property.rb @@ -22,6 +22,7 @@ cidr -- CIDR-notated IP addr and mask for the network_role netmask -- string, contains dotted nemmask ipaddr_netmask_pair -- list of ipaddr and netmask + phys_dev -- physical device name mapped to the network with the selected network_role Returns NIL if role not found. @@ -69,7 +70,11 @@ ipaddr_cidr = ep[:IP][0] ? ep[:IP][0] : nil when "String" Puppet::debug("get_network_role_property(...): Can't determine dynamic or empty IP address for endpoint '#{interface}' (#{ep[:IP]}).") - return nil + if mode != 'PHYS_DEV' + return nil + end + when "NilClass" + ipaddr_cidr = nil else Puppet::debug("get_network_role_property(...): invalid IP address for endpoint '#{interface}'.") return nil @@ -80,14 +85,26 @@ when 'CIDR' rv = ipaddr_cidr when 'NETMASK' - rv = IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s + rv = (ipaddr_cidr.nil? ? nil : IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s) when 'IPADDR' - rv = prepare_cidr(ipaddr_cidr)[0].to_s + rv = (ipaddr_cidr.nil? ? nil : prepare_cidr(ipaddr_cidr)[0].to_s) when 'IPADDR_NETMASK_PAIR' - rv = prepare_cidr(ipaddr_cidr)[0].to_s, IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s + rv = (ipaddr_cidr.nil? ? [nil,nil] : [prepare_cidr(ipaddr_cidr)[0].to_s, IPAddr.new('255.255.255.255').mask(prepare_cidr(ipaddr_cidr)[1]).to_s]) + when 'PHYS_DEV' + devices = L23network::Scheme.get_phys_dev_by_endpoint(interface, cfg[:interfaces], cfg[:transformations]) + if devices.any? { |dev| /^bond/ =~ dev } + for i in 0..cfg[:transformations].size-1 + transform = cfg[:transformations][i] + name = transform[:name] + if transform[:name] == devices[0] + devices.push(transform[:interfaces]) + end + end + end + rv = devices.flatten end rv end -# vim: set ts=2 sw=2 et : \ No newline at end of file +# vim: set ts=2 sw=2 et : diff --git a/lib/puppet/parser/functions/get_transformation_property.rb b/lib/puppet/parser/functions/get_transformation_property.rb new file mode 100644 index 0000000..d412375 --- /dev/null +++ b/lib/puppet/parser/functions/get_transformation_property.rb @@ -0,0 +1,65 @@ +require 'puppetx/l23_network_scheme' + +Puppet::Parser::Functions::newfunction(:get_transformation_property, :type => :rvalue, :doc => <<-EOS + This function gets an endpoint properties from transformations -- + and returns information about the selected property + + ex: get_transformation_property('mtu','eth0') + + You can use following modes: + mtu -- mtu value for the selected transformation. + + Returns NIL if a device is not found or mtu is not set + + EOS + ) do |argv| + if argv.size > 1 + mode = argv[0].to_s().upcase() + argv.shift + else + raise(Puppet::ParseError, "get_transformation_property(...): Wrong number of arguments.") + end + + cfg = L23network::Scheme.get_config(lookupvar('l3_fqdn_hostname')) + if cfg.nil? + raise(Puppet::ParseError, "get_transformation_property(...): You must call prepare_network_config(...) first!") + end + + if !cfg[:roles] || !cfg[:endpoints] || cfg[:roles].class.to_s() != "Hash" || cfg[:endpoints].class.to_s() != "Hash" + raise(Puppet::ParseError, "get_transformation_property(...): Invalid cfg_hash format.") + end + + transforms = cfg[:transformations] + ifaces = cfg[:interfaces] + all_ifaces = ifaces.keys.map(&:to_s) + devices = argv.flatten + mtu = [] + + rv = nil + + case mode + when 'MTU' + devices.each do |device| + if all_ifaces.include? device and ifaces[device.to_sym][:mtu] != nil + mtu.push(ifaces[device.to_sym][:mtu]) + else + for i in 0..transforms.size-1 do + transform = cfg[:transformations][i] + if transform[:name] == device and transform[:mtu] != nil + mtu.push(transform[:mtu]) + end + end + end + if mtu.empty? + Puppet::debug("get_transformation_property(...): MTU value is not set for interface '#{device}'.") + rv = nil + else + rv = mtu.min + end + end + end + + rv +end + +# vim: set ts=2 sw=2 et : diff --git a/lib/puppetx/l23_network_scheme.rb b/lib/puppetx/l23_network_scheme.rb index 37b8e69..ddc1bdb 100644 --- a/lib/puppetx/l23_network_scheme.rb +++ b/lib/puppetx/l23_network_scheme.rb @@ -7,6 +7,36 @@ def self.set_config(h, v) def self.get_config(h) @network_scheme_hash[h.to_sym] end + def self.get_phys_dev_by_endpoint(endpoint, interfaces, transformations) + rv = [] + all_ifaces = interfaces.keys.map(&:to_s) + ifaces = [] + + if all_ifaces.include? endpoint + ifaces.push(endpoint) + return ifaces + end + + for i in 0..transformations.size-1 do + transform = transformations[i] + if transform[:name] != nil and transform[:name].include? endpoint + if transform[:vlandev] != nil + ifaces.push(transform[:vlandev]) + else + endpoint = transform[:name].split(".")[0] + if all_ifaces.include? endpoint + ifaces.push(endpoint.to_s) + end + end + elsif transform[:bridge] == endpoint + ifaces.push(transform[:name].split(".")[0]) + elsif transform[:bridges] != nil and transform[:bridges][0] == endpoint + endpoint = transform[:bridges][1] + ifaces.push(get_phys_dev_by_endpoint(endpoint, interfaces, transformations)) + end + end + return ifaces.flatten.uniq + end end end -# vim: set ts=2 sw=2 et : \ No newline at end of file +# vim: set ts=2 sw=2 et : diff --git a/spec/functions/get_network_role_property__phys_dev__spec.rb b/spec/functions/get_network_role_property__phys_dev__spec.rb new file mode 100644 index 0000000..a3134af --- /dev/null +++ b/spec/functions/get_network_role_property__phys_dev__spec.rb @@ -0,0 +1,149 @@ +require 'spec_helper' +require 'yaml' +require 'puppetx/l23_hash_tools' + +describe Puppet::Parser::Functions.function(:get_network_role_property) do +let(:network_scheme) do +< [ { + :bridge => 'br-ex', + :name => 'bond0', + :mtu => 1450, + :interfaces => ["eth1", "eth2"], + }, ], + :interfaces => { + :eth0 => {}, + }, :endpoints => { :eth0 => {:IP => 'dhcp'}, :"br-ex" => { @@ -61,6 +70,10 @@ should run.with_params('management', 'ipaddr_netmask_pair').and_return(['10.20.1.11','255.255.255.128']) end + it 'should return physical device name for "ex" network role' do + should run.with_params('ex', 'phys_dev').and_return(["bond0", "eth1", "eth2"]) + end + it 'should return NIL for "admin" network role' do should run.with_params('admin', 'netmask').and_return(nil) end @@ -73,6 +86,9 @@ it 'should return NIL for "admin" network role' do should run.with_params('admin', 'ipaddr_netmask_pair').and_return(nil) end + it 'should return NIL for "admin" network role' do + should run.with_params('admin', 'phys_dev').and_return(nil) + end end -end \ No newline at end of file +end diff --git a/spec/functions/get_transformation_property__spec.rb b/spec/functions/get_transformation_property__spec.rb new file mode 100644 index 0000000..b05c065 --- /dev/null +++ b/spec/functions/get_transformation_property__spec.rb @@ -0,0 +1,125 @@ +require 'spec_helper' +require 'yaml' +require 'puppetx/l23_hash_tools' + +describe Puppet::Parser::Functions.function(:get_transformation_property) do +let(:network_scheme) do +<