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

Use search and order params in keystone utility functions #275

Open
wants to merge 2 commits into
base: main
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
9 changes: 4 additions & 5 deletions apps/crc_proposal_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,17 @@ def app_logic(self, args: Namespace) -> None:
keystone_session.login(username=os.environ["USER"],
password=getpass("Please enter your CRCD login password:\n"))

team_id = get_team_id(keystone_session, args.account)
alloc_requests = get_active_requests(keystone_session, team_id)
alloc_requests = get_active_requests(keystone_session, args.account)

if not alloc_requests:
print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n")
print("Showing end date for most recently expired Resource Allocation Request:\033[0m \n")
try:
alloc_requests = [get_most_recent_expired_request(keystone_session, team_id)]
alloc_requests = [get_most_recent_expired_request(keystone_session, args.account)]
except IndexError:
print("\033[91m\033[1mNo allocation information found. Either the group does not have any allocations, "
"or you do not have permissions to view them. If you believe this to be a mistake, please submit a "
"help ticket to the CRCD team. \033[0m \n")
"or you do not have permissions to view them. If you believe this to be a mistake, please submit "
"a help ticket to the CRCD team. \033[0m \n")
exit()

for request in alloc_requests:
Expand Down
5 changes: 2 additions & 3 deletions apps/crc_sus.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,14 @@ def app_logic(self, args: Namespace) -> None:
keystone_session.login(username=os.environ["USER"],
password=getpass("Please enter your CRCD login password:\n"))

group_id = get_team_id(keystone_session, args.account)
alloc_requests = get_active_requests(keystone_session, group_id)
alloc_requests = get_active_requests(keystone_session, args.account)

if not alloc_requests:
print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n")
print("Showing remaining service unit amounts for most recently expired Resource Allocation Request:"
"\033[0m \n")
try:
alloc_requests = [get_most_recent_expired_request(keystone_session, group_id)]
alloc_requests = [get_most_recent_expired_request(keystone_session, args.account)]
except IndexError:
print("\033[91m\033[1mNo allocation information found. Either the group does not have any allocations, "
"or you do not have permissions to view them. If you believe this to be a mistake, please submit "
Expand Down
5 changes: 2 additions & 3 deletions apps/crc_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,13 @@ def app_logic(self, args: Namespace) -> None:
password=getpass("Please enter your CRCD login password:\n"))

# Gather AllocationRequests from Keystone
group_id = get_team_id(keystone_session, args.account)
alloc_requests = get_active_requests(keystone_session, group_id)
alloc_requests = get_active_requests(keystone_session, args.account)

if not alloc_requests:
print(f"\033[91m\033[1mNo active allocation information found in accounting system for '{args.account}'!\n")
print("Attempting to show the most recently expired Resource Allocation Request info: \033[0m \n")
try:
alloc_requests = [get_most_recent_expired_request(keystone_session, group_id)]
alloc_requests = [get_most_recent_expired_request(keystone_session, args.account)]
except IndexError:
print("\033[91m\033[1mNo allocation information found. Either the group does not have any allocations, "
"or you do not have permissions to view them. If you believe this to be a mistake, please submit "
Expand Down
33 changes: 11 additions & 22 deletions apps/utils/keystone.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,13 @@ def get_request_allocations(session: KeystoneClient, request_pk: int) -> dict:
return session.retrieve_allocation(filters={'request': request_pk})


def get_active_requests(session: KeystoneClient, team_pk: int) -> [dict]:
def get_active_requests(session: KeystoneClient, account_name: str) -> [dict]:
"""Get all active AllocationRequest information from keystone for a given team"""

today = date.today().isoformat()
return session.retrieve_request(
filters={'team': team_pk, 'status': 'AP', 'active__lte': today, 'expire__gt': today})


def get_team_id(session: KeystoneClient, account_name: str) -> int:
"""Get the Team ID from keystone for the specified Slurm account"""

# Attempt to get the primary key for the Team
try:
keystone_team_id = session.retrieve_team(filters={'name': account_name})[0]['id']
except IndexError:
print(f"No Slurm Account found in the accounting system for '{account_name}'. \n"
f"Please submit a ticket to the CRC team to ensure your allocation was properly configured")
exit()

return keystone_team_id
search=account_name,
filters={'status': 'AP', 'active__lte': today, 'expire__gt': today})


def get_earliest_startdate(alloc_requests: [dict]) -> date:
Expand All @@ -56,12 +43,14 @@ def get_earliest_startdate(alloc_requests: [dict]) -> date:
return max(earliest_date, RAWUSAGE_RESET_DATE)


def get_most_recent_expired_request(session: KeystoneClient, team_pk: int) -> [dict]:
def get_most_recent_expired_request(session: KeystoneClient, account_name: str) -> [dict]:
"""Get the single most recently expired AllocationRequest information from keystone for a given team"""

today = date.today().isoformat()
return session.retrieve_request(
filters={'team': team_pk, 'status': 'AP', 'ordering': '-expire', 'expire__lte': today})[0]
search=account_name,
order='-expire',
filters={'status': 'AP', 'expire__lte': today})[0]


def get_enabled_cluster_ids(session: KeystoneClient) -> dict():
Expand All @@ -75,10 +64,10 @@ def get_enabled_cluster_ids(session: KeystoneClient) -> dict():


def get_per_cluster_totals(session: KeystoneClient,
alloc_requests: [dict],
clusters: dict,
per_request: bool = False
) -> dict:
alloc_requests: [dict],
clusters: dict,
per_request: bool = False
) -> dict:
"""Gather the awarded totals across the given requests on each cluster into a dictionary"""

per_cluster_totals = {}
Expand Down
Loading