Puppet Function: slice_network_constants

Defined in:
modules/network/lib/puppet/parser/functions/slice_network_constants.rb
Function type:
Ruby 3.x API

Overview

slice_network_constants()Any

Returns:

  • (Any)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'modules/network/lib/puppet/parser/functions/slice_network_constants.rb', line 40

newfunction(:slice_network_constants, :type => :rvalue, :arity => -2) do |args|
  if args.empty? || args.length > 2
    raise(ArgumentError, "slice_network_constants(): " +
          "Wrong number of arguments given (#{args.length})")
  end

  all_network_subnets = lookupvar('network::constants::all_network_subnets')

  realm = args[0]
  realm = 'cloud' if realm == 'labs'

  options = args[1] if args.length > 1
  requested_site = options['site'] if options
  requested_sphere = options['sphere'] if options
  af = options['af'] if options
  description = options['description'] if options

  unless all_network_subnets.key?(realm)
      raise(Puppet::ParseError, "slice_network_constants(): " +
            "realm non-existent in network::subnets")
  end
  if requested_site && (!all_network_subnets[realm].key? requested_site)
      raise(Puppet::ParseError, "slice_network_constants(): " +
            "site #{requested_site} not in network::subnets[#{realm}]")
  end
  if requested_sphere && (!['public', 'private'].include? requested_sphere)
      raise(Puppet::ParseError, "slice_network_constants(): " +
            "sphere #{requested_sphere} is not valid")
  end
  if af && (!['ipv4', 'ipv6'].include? af)
      raise(Puppet::ParseError, "slice_network_constants(): " +
            "address family #{af} is not valid")
  end

  # And let's get our data back
  result = all_network_subnets[realm].collect { |site, value_site|
      if requested_site && site != requested_site
          next
      end
      value_site.collect { |sphere, value_sphere|
          if requested_sphere && sphere != requested_sphere
              next
          end
          value_sphere.collect { |subnet, value_subnet|
              if description && !(/#{description}/ =~ subnet)
                  next
              end
              if af
                  value_subnet[af]
              else
                  # flatten will take care of this below
                  [value_subnet['ipv4'], value_subnet['ipv6']]
              end
          }
      }
  }
  result.flatten.compact.sort
end