Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

view-user: add a new --list-banks optional argument #479

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions t/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ TESTSCRIPTS = \
t1042-issue508.t \
t1043-view-jobs-by-bank.t \
t1044-mf-priority-resource-limits.t \
t1045-issue478.t \
t5000-valgrind.t \
python/t1000-example.py \
python/t1001_db.py \
Expand Down
49 changes: 49 additions & 0 deletions t/t1045-issue478.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

test_description='test calling view-user with the --list-banks optional argument'

. `dirname $0`/sharness.sh

mkdir -p conf.d

ACCOUNTING_DB=$(pwd)/FluxAccountingTest.db

export TEST_UNDER_FLUX_SCHED_SIMPLE_MODE="limited=1"
test_under_flux 1 job -o,--config-path=$(pwd)/conf.d

flux setattr log-stderr-level 1

test_expect_success 'create flux-accounting DB, start flux-accounting service' '
flux account -p ${ACCOUNTING_DB} create-db &&
flux account-service -p ${ACCOUNTING_DB} -t
'

test_expect_success 'add some banks' '
flux account add-bank root 1 &&
flux account add-bank --parent-bank=root bankA 1 &&
flux account add-bank --parent-bank=root bankB 1 &&
flux account add-bank --parent-bank=root bankC 1
'

test_expect_success 'add a user' '
flux account add-user --username=testuser --bank=bankA &&
flux account add-user --username=testuser --bank=bankB &&
flux account add-user --username=testuser --bank=bankC
'

test_expect_success 'call view-user --list-banks' '
flux account view-user testuser --list-banks > banks.out &&
grep "bankA" banks.out &&
grep "bankB" banks.out &&
grep "bankC" banks.out
'

test_expect_success 'shut down flux-accounting service' '
flux python -c "import flux; flux.Flux().rpc(\"accounting.shutdown_service\").get()"
'

test_expect_success 'remove flux-accounting DB' '
rm ${ACCOUNTING_DB}
'

test_done
Loading