Skip to content

Commit

Permalink
useradd: add 'user' arg to getent
Browse files Browse the repository at this point in the history
Allows retrieval of user info for a single user, so it is not necessary
to iterate through the return data to get that user's info.

Also some pep8 fixes.
  • Loading branch information
terminalmage committed Mar 17, 2013
1 parent 449b1e2 commit a558d84
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions salt/modules/useradd.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def _build_gecos(gecos_dict):
Accepts a dictionary entry containing GECOS field names and their values,
and returns a full GECOS comment string, to be used with usermod.
'''
return '{0},{1},{2},{3}'.format(gecos_dict.get('fullname',''),
gecos_dict.get('roomnumber',''),
gecos_dict.get('workphone',''),
gecos_dict.get('homephone',''))
return '{0},{1},{2},{3}'.format(gecos_dict.get('fullname', ''),
gecos_dict.get('roomnumber', ''),
gecos_dict.get('workphone', ''),
gecos_dict.get('homephone', ''))


def add(name,
Expand Down Expand Up @@ -163,7 +163,7 @@ def delete(name, remove=False, force=False):
return not ret['retcode']


def getent():
def getent(user=None):
'''
Return the list of all info for all users
Expand All @@ -172,13 +172,16 @@ def getent():
salt '*' user.getent
'''
if 'useradd_getent' in __context__:
return __context__['useradd_getent']
return __context__['useradd_getent']

ret = []
for data in pwd.getpwall():
ret.append(_format_info(data))
__context__['useradd_getent'] = ret

if user:
try:
ret = [x for x in ret if x.get('name', '') == user][0]
except IndexError:
ret = {}
return ret


Expand Down Expand Up @@ -293,7 +296,8 @@ def chfullname(name, fullname):
'''
fullname = str(fullname)
pre_info = _get_gecos(name)
if not pre_info: return False
if not pre_info:
return False
if fullname == pre_info['fullname']:
return True
gecos_field = copy.deepcopy(pre_info)
Expand All @@ -316,7 +320,8 @@ def chroomnumber(name, roomnumber):
'''
roomnumber = str(roomnumber)
pre_info = _get_gecos(name)
if not pre_info: return False
if not pre_info:
return False
if roomnumber == pre_info['roomnumber']:
return True
gecos_field = copy.deepcopy(pre_info)
Expand All @@ -339,7 +344,8 @@ def chworkphone(name, workphone):
'''
workphone = str(workphone)
pre_info = _get_gecos(name)
if not pre_info: return False
if not pre_info:
return False
if workphone == pre_info['workphone']:
return True
gecos_field = copy.deepcopy(pre_info)
Expand All @@ -362,7 +368,8 @@ def chhomephone(name, homephone):
'''
homephone = str(homephone)
pre_info = _get_gecos(name)
if not pre_info: return False
if not pre_info:
return False
if homephone == pre_info['homephone']:
return True
gecos_field = copy.deepcopy(pre_info)
Expand Down Expand Up @@ -402,6 +409,7 @@ def info(name):
else:
return _format_info(data)


def _format_info(data):
'''
Return user information in a pretty way
Expand Down Expand Up @@ -457,6 +465,7 @@ def list_groups(name):

return sorted(list(ugrp))


def list_users():
'''
Return a list of all users
Expand Down

0 comments on commit a558d84

Please sign in to comment.