Skip to content

Commit

Permalink
YDA-5393: port uuUserExists rule to Python
Browse files Browse the repository at this point in the history
YDA-5393: port uuUserExists rule to Python
---------

Co-authored-by: FuHsinyu <[email protected]>
  • Loading branch information
FuHsinyu and FuHsinyu authored Oct 14, 2024
1 parent 39a4f97 commit 3355a65
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
18 changes: 16 additions & 2 deletions yclienttools/common_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def __init__(self, session, yoda_version):
"1.7", "1.8"] else "1.9"
self.default_rule_engine = 'irods_rule_engine_plugin-irods_rule_language-instance'

try:
self.client_hints_rules = self.session.client_hints.get("rules", {})
except Exception as e:
print("Error: {}. Hint: python-irodsclient needs to be version 2.1 or later to support client_hints.".format(e))

def call_rule(self, rulename, params, number_outputs,
rule_engine=None) -> List[str]:
"""Run a rule
Expand Down Expand Up @@ -153,14 +158,23 @@ def call_uuGroupExists(self, groupname: str) -> bool:
[out] = self.call_rule('uuGroupExists', parms, 1)
return out == 'true'

def call_uuUserExists(self, username: str) -> bool:
def call_rule_user_exists(self, username: str) -> bool:
"""Check whether user name exists on Yoda
:param username: name of user
:returns: false/true
"""
# Determine which rule to call
rule_to_call = 'rule_user_exists' if 'rule_user_exists' in self.client_hints_rules else 'uuUserExists'

# Prepare rule parameters
parms = OrderedDict([('username', username)])
[out] = self.call_rule('uuUserExists', parms, 1)
if rule_to_call == 'rule_user_exists':
parms['outparam1'] = ""

# Call the rule
[out] = self.call_rule(rule_to_call, parms, 1)

return out == 'true'

def call_uuGroupAdd(self, groupname: str, category: str,
Expand Down
2 changes: 1 addition & 1 deletion yclienttools/ensuremembers.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def validate_data(rule_interface, args, userdata, groupdata):

for user in userdata:
if not is_internal_user(user, args.internal_domains.split(",")):
if not rule_interface.call_uuUserExists(user):
if not rule_interface.call_rule_user_exists(user):
errors.append("External user {} does not exist.".format(user))

for group in groupdata:
Expand Down
2 changes: 1 addition & 1 deletion yclienttools/importgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def validate_data(rule_interface: RuleInterface, args: argparse.Namespace, data:
# ensure that external users already have an iRODS account
# we do not want to be the actor that creates them (unless
# we are creating them in the name of a creator user)
if not rule_interface.call_uuUserExists(user) and not args.creator_user:
if not rule_interface.call_rule_user_exists(user) and not args.creator_user:
errors.append(
'Group {} has nonexisting external user {}'.format(groupname, user))

Expand Down
2 changes: 1 addition & 1 deletion yclienttools/rmusers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def validate_data(session, rule_interface, args, userdata):
errors = []

for user in userdata:
if not rule_interface.call_uuUserExists(user):
if not rule_interface.call_rule_user_exists(user):
errors.append(f"User {user} does not exist.")
if home_exists(session, user) and not home_is_empty(session, user):
errors.append(f"Home directory of user {user} is not empty")
Expand Down

0 comments on commit 3355a65

Please sign in to comment.