Return a more meaningful error on unmapped accounts

Fixes an issue in win_groupadd where an unmapped account would throw a
double stacktrace. One for the error and another when it tried to look
up the error code to get the message. Since pywintypes.com_error returns
a good text string, we'll just use that instead of trying to look it up
again.

This code is being covered by an existing test.
This commit is contained in:
twangboy 2020-04-27 15:55:59 -06:00 committed by Daniel Wozniak
parent 1ba615f306
commit dc37eaea01
2 changed files with 9 additions and 7 deletions

View file

@ -36,9 +36,11 @@ def __virtual__():
"""
Set the group module if the kernel is Windows
"""
if salt.utils.platform.is_windows() and HAS_DEPENDENCIES:
return __virtualname__
return (False, "Module win_groupadd: module only works on Windows systems")
if not salt.utils.platform.is_windows():
return False, "win_groupadd: only works on Windows systems"
if not HAS_DEPENDENCIES:
return False, "win_groupadd: missing dependencies"
return __virtualname__
def _get_computer_object():
@ -283,7 +285,7 @@ def adduser(name, username, **kwargs):
return False
except pywintypes.com_error as exc:
msg = "Failed to add {0} to group {1}. {2}".format(
username, name, win32api.FormatMessage(exc.excepinfo[5])
username, name, exc.excepinfo[2]
)
log.error(msg)
return False

View file

@ -180,9 +180,9 @@ def present(name, gid=None, system=False, addusers=None, delusers=None, members=
# -- if trying to add and delete the same user(s) at the same time.
if not set(addusers).isdisjoint(set(delusers)):
ret["result"] = None
ret["comment"] = (
"Error. Same user(s) can not be added and deleted" " simultaneously"
)
ret[
"comment"
] = "Error. Same user(s) can not be added and deleted simultaneously"
return ret
changes = _changes(name, gid, addusers, delusers, members)