Skip to content

Commit

Permalink
merge group info with primary group declaration #39
Browse files Browse the repository at this point in the history
  • Loading branch information
deric committed Mar 15, 2016
1 parent 1bc9d10 commit 902ec24
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
8 changes: 5 additions & 3 deletions lib/puppet/parser/functions/accounts_primary_groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module Puppet::Parser::Functions
EOS
) do |args|

if args.size != 1
raise(Puppet::ParseError, "accounts_primary_groups(): Wrong number of args, given #{args.size}, accepts only 1")
if args.size != 2
raise(Puppet::ParseError, "accounts_primary_groups(): Wrong number of args, given #{args.size}, accepts exactly 2 Hashes")
end

if args[0].class != Hash
raise(Puppet::ParseError, "accounts_primary_groups(): argument must be a Hash, you passed a " + args[0].class.to_s)
raise(Puppet::ParseError, "accounts_primary_groups(): argument must be a Hash, you passed a " + args[0].class.to_s + " and "+ args[1].class.to_s)
end

# assign `user` to group `g`
Expand All @@ -23,13 +23,15 @@ module Puppet::Parser::Functions
end

res = {}
groups = args[1]
args[0].each do |user, val|
# don't assign users marked for removal to groups
next if val.key? 'ensure' and val['ensure'] == 'absent'
val['primary_group'] = user.to_s unless val.key? 'primary_group'
val['manage_group'] = true unless val.key? 'manage_group'
if val['manage_group']
g = val['primary_group']
res[g] = groups[g] if groups.key? g
assign_helper.call(res, g, user)
if val.key? 'gid'
res[g]['gid'] = val['gid'] # manually override GID
Expand Down
2 changes: 1 addition & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

anchor { 'accounts::primary_groups_created': }
# create primary groups first
$primary_groups = accounts_primary_groups($merged_users)
$primary_groups = accounts_primary_groups($merged_users, $merged_groups)
notify{"primary_groups: ${primary_groups}": }
create_resources(accounts::group, $primary_groups,
{'before' => Anchor['accounts::primary_groups_created']}
Expand Down
18 changes: 12 additions & 6 deletions spec/functions/accounts_primary_groups_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
john: { 'groups' => ['bar', 'users'], 'gid' => 500},
}

is_expected.to run.with_params(users).and_return(
is_expected.to run.with_params(users, {}).and_return(
{
'foo' => {'members' => [:foo] },
'john' => {'members' => [:john], 'gid' => 500},
Expand All @@ -45,7 +45,7 @@
tracy: { 'groups' => ['sudo', 'users'], 'ensure' => 'absent'},
}

is_expected.to run.with_params(users).and_return(
is_expected.to run.with_params(users, {}).and_return(
{
'bob' => {'members' => [:bob]},
'alice' => {'members' => [:alice]},
Expand All @@ -57,13 +57,19 @@
describe 'extract also primary groups' do
it 'finds group specified by primary_group' do
users = {
foo: { 'primary_group' => 'testgroup', 'manage_group' => true},
bar: { 'primary_group' => 'testgroup', 'manage_group' => false},
'foo' => { 'primary_group' => 'testgroup', 'manage_group' => true},
'bar' => { 'primary_group' => 'testgroup', 'manage_group' => false},
}
groups = {
'testgroup' => {
'members' => [ 'www-data', 'testuser' ],
'gid' => 500,
}
}

is_expected.to run.with_params(users).and_return(
is_expected.to run.with_params(users, groups).and_return(
{
'testgroup' => {'members' => [:foo]},
'testgroup' => {'members' => ['www-data', 'testuser', 'foo'], 'gid' => 500},
}
)
end
Expand Down

0 comments on commit 902ec24

Please sign in to comment.