7fa785486c04a5a560302801b84caed33897fe6a
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / lib / puppet / parser / functions / postgresql_acls_to_resources_hash.rb
1 module Puppet::Parser::Functions
2   newfunction(:postgresql_acls_to_resources_hash, :type => :rvalue, :doc => <<-EOS
3     This internal function translates the ipv(4|6)acls format into a resource
4     suitable for create_resources. It is not intended to be used outside of the
5     postgresql internal classes/defined resources.
6
7     This function accepts an array of strings that are pg_hba.conf rules. It
8     will return a hash that can be fed into create_resources to create multiple
9     individual pg_hba_rule resources.
10
11     The second parameter is an identifier that will be included in the namevar
12     to provide uniqueness. It must be a string.
13
14     The third parameter is an order offset, so you can start the order at an
15     arbitrary starting point.
16     EOS
17   ) do |args|
18     func_name = "postgresql_acls_to_resources_hash()"
19
20     raise(Puppet::ParseError, "#{func_name}: Wrong number of arguments " +
21       "given (#{args.size} for 3)") if args.size != 3
22
23     acls = args[0]
24     raise(Puppet::ParseError, "#{func_name}: first argument must be an array") \
25       unless acls.instance_of? Array
26
27     id = args[1]
28     raise(Puppet::ParseError, "#{func_name}: second argument must be a string") \
29       unless id.instance_of? String
30
31     offset = args[2].to_i
32     raise(Puppet::ParseError, "#{func_name}: third argument must be a number") \
33       unless offset.instance_of? Fixnum
34
35     resources = {}
36     acls.each do |acl|
37       index = acls.index(acl)
38
39       parts = acl.split
40
41       raise(Puppet::ParseError, "#{func_name}: acl line #{index} does not " +
42         "have enough parts") unless parts.length >= 4
43
44       resource = {
45         'type' => parts[0],
46         'database' => parts[1],
47         'user' => parts[2],
48         'order' => format('%03d', offset + index),
49       }
50       if parts[0] == 'local' then
51         resource['auth_method'] = parts[3]
52         if parts.length > 4 then
53           resource['auth_option'] = parts.last(parts.length - 4).join(" ")
54         end
55       else
56         if parts[4] =~ /^\d/
57           resource['address'] = parts[3] + ' ' + parts[4]
58           resource['auth_method'] = parts[5]
59
60           if parts.length > 6 then
61             resource['auth_option'] = parts.last(parts.length - 6).join(" ")
62           end
63         else
64           resource['address'] = parts[3]
65           resource['auth_method'] = parts[4]
66
67           if parts.length > 5 then
68             resource['auth_option'] = parts.last(parts.length - 5).join(" ")
69           end
70         end
71       end
72       resources["postgresql class generated rule #{id} #{index}"] = resource
73     end
74     resources
75   end
76 end