Add get_default_gateways() parser function

for getting list of default gateways. This list ordered by
gateway_metric.

Change-Id: I36e6c63293992331df6e3f07c6738ceaa2cb4dd7
related-blueprint: templates-for-networking
This commit is contained in:
Sergey Vasilenko 2015-07-01 21:46:51 +03:00
parent f25d5da0a9
commit 96cfca637d
3 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,44 @@
require 'ipaddr'
begin
require 'puppet/parser/functions/lib/prepare_cidr.rb'
rescue LoadError => e
# puppet apply does not add module lib directories to the $LOAD_PATH (See
# #4248). It should (in the future) but for the time being we need to be
# defensive which is what this rescue block is doing.
rb_file = File.join(File.dirname(__FILE__),'lib','prepare_cidr.rb')
load rb_file if File.exists?(rb_file) or raise e
end
require 'puppetx/l23_network_scheme'
Puppet::Parser::Functions::newfunction(:get_default_gateways, :type => :rvalue, :doc => <<-EOS
Parse network_scheme and return list of default gateways,
ordered by its metrics
Returns [] if no gateways.
EOS
) do |argv|
cfg = L23network::Scheme.get_config(lookupvar('l3_fqdn_hostname'))
if cfg.nil?
raise(Puppet::ParseError, "get_default_gateways(): You must call prepare_network_config(...) first!")
end
endpoints = cfg[:endpoints]
if ! endpoints.is_a? Hash
Puppet::ParseError("get_default_gateways(): Section 'endpoints' should be a hash.")
end
rv = []
endpoints.each do |ep_name, ep_props|
next if ep_props[:gateway].nil?
rv << {
:m => (ep_props[:gateway_metric] or 0),
:g => ep_props[:gateway]
}
end
return [] if rv.empty?
rv.sort_by{|a| a[:m]}.map{|t| t[:g]}
end
# vim: set ts=2 sw=2 et :

View File

@ -0,0 +1,73 @@
require 'spec_helper'
require 'yaml'
require 'puppetx/l23_hash_tools'
describe Puppet::Parser::Functions.function(:get_default_gateways) do
let(:network_scheme) do
<<eof
---
version: 1.1
provider: lnx
interfaces:
eth0:
mtu: 2048
eth1:
mtu: 999
eth2: {}
eth3: {}
eth4: {}
eth5: {}
eth44: {}
endpoints:
eth0:
IP: 'none'
eth4:
IP: 'none'
br-ex:
gateway: 10.1.3.1
IP:
- '10.1.3.11/24'
br-mgmt:
gateway: 10.1.1.1
gateway_metric: 20
IP:
- '10.1.1.11/24'
br-storage:
gateway: 10.1.2.1
gateway_metric: 10
IP:
- '10.1.2.11/24'
br-floating:
IP: none
eof
end
let(:scope) { PuppetlabsSpec::PuppetInternals.scope }
subject do
function_name = Puppet::Parser::Functions.function(:get_default_gateways)
scope.method(function_name)
end
context ":get_default_gateways() usage" do
before(:each) do
scope.stubs(:lookupvar).with('l3_fqdn_hostname').returns('node1.tld')
L23network::Scheme.set_config(
scope.lookupvar('l3_fqdn_hostname'),
L23network.sanitize_keys_in_hash(YAML.load(network_scheme))
)
end
it 'should exist' do
subject == Puppet::Parser::Functions.function(:get_default_gateways)
end
it do
should run.with_params().and_return(['10.1.3.1', '10.1.2.1', '10.1.1.1'])
end
end
end