Skip to content

Commit

Permalink
view-user: add --list-banks option
Browse files Browse the repository at this point in the history
Problem: There is no concise way to view the banks that a user belongs
to.

Add a --list-banks optional argument to view-user, which will just print
the banks that a user belongs to in the flux-accounting database.
  • Loading branch information
cmoussa1 committed Jan 16, 2025
1 parent 0ffd06e commit cd04cf9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/bindings/python/fluxacct/accounting/formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,21 @@ def __init__(self, cursor, username):
super().__init__(
cursor, error_msg=f"user {self.username} not found in association_table"
)

def list_banks(self):
"""
Return all of the banks that the user belongs to with each bank
on its own line.
Args:
username: the name of the user.
"""
self.cursor.execute(
"SELECT bank FROM association_table WHERE username=?", (self.username,)
)
result = self.cursor.fetchall()
banks = ""
for bank in result:
banks += f"{str(bank[0])}\n"

return banks
4 changes: 3 additions & 1 deletion src/bindings/python/fluxacct/accounting/user_subcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def clear_projects(conn, username, bank=None):
# Subcommand Functions #
# #
###############################################################
def view_user(conn, user, parsable=False, cols=None):
def view_user(conn, user, parsable=False, cols=None, list_banks=False):
# use all column names if none are passed in
cols = cols or fluxacct.accounting.ASSOCIATION_TABLE

Expand All @@ -250,6 +250,8 @@ def view_user(conn, user, parsable=False, cols=None):
# initialize AssociationFormatter object
formatter = fmt.AssociationFormatter(cur, user)

if list_banks:
return formatter.list_banks()
if parsable:
return formatter.as_table()
return formatter.as_json()
Expand Down
1 change: 1 addition & 0 deletions src/cmd/flux-account-service.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def view_user(self, handle, watcher, msg, arg):
msg.payload["username"],
msg.payload["parsable"],
msg.payload["fields"].split(",") if msg.payload.get("fields") else None,
msg.payload["list_banks"],
)

payload = {"view_user": val}
Expand Down
7 changes: 7 additions & 0 deletions src/cmd/flux-account.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ def add_view_user_arg(subparsers):
"MAX_CORES,QUEUES,PROJECTS,DEFAULT_PROJECT"
),
)
subparser_view_user.add_argument(
"--list-banks",
action="store_const",
const=True,
help="list all of the banks a user belongs to",
metavar="LIST_BANKS",
)


def add_add_user_arg(subparsers):
Expand Down

0 comments on commit cd04cf9

Please sign in to comment.