Method: Puppet::Provider::ElasticUserRoles.parse

Defined in:
lib/puppet/provider/elastic_user_roles.rb

.parse(text) ⇒ Object

Override the ancestor ‘parse` method to process a users/roles file managed by the Elasticsearch user tools.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/provider/elastic_user_roles.rb', line 10

def self.parse(text)
  lines = text.split("\n").map(&:strip).select do |line|
    # Strip comments
    (!line.start_with? '#') && !line.empty?
  end
  lines = lines.map do |line|
    # Turn array of roles into array of users that have the role
    role, users = line.split(':')
    users.split(',').map do |user|
      { user => [role] }
    end
  end
  lines = lines.flatten.reduce({}) do |hash, user|
    # Gather up user => role hashes by append-merging role lists
    hash.merge(user) { |_, o, n| o + n }
  end
  lines = lines.map do |user, roles|
    # Map those hashes into what the provider expects
    {
      name: user,
      roles: roles
    }
  end
  lines.to_a
end