Skip to content

Commit

Permalink
Merge pull request RedHatInsights#1024 from petracihalova/sa-principa…
Browse files Browse the repository at this point in the history
…ls-endpoint

Service Accounts principals/ endpoint - fix for "count"
  • Loading branch information
petracihalova authored Feb 8, 2024
2 parents 67a29c6 + 311e1ff commit 33cedd3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 9 additions & 9 deletions rbac/management/principal/it_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,6 @@ def get_service_accounts(self, user: User, bearer_token: str, options: dict = {}
else:
service_account_principals = service_account_principals.order_by("-username")

# We always set a default offset and a limit if the user doesn't specify them, so it is safe to simply put the
# two parameters in the query to slice it.
offset = options.get("offset")
limit = options.get("limit")
limit_offset_validation(offset, limit)

count = len(service_account_principals)
service_account_principals = service_account_principals[offset : offset + limit]

# If we are in an ephemeral or test environment, we will take all the service accounts of the user that are
# stored in the database and generate a mocked response for them, simulating that IT has the corresponding
# service account to complement the information.
Expand All @@ -235,6 +226,15 @@ def get_service_accounts(self, user: User, bearer_token: str, options: dict = {}
service_account_principals=sap_dict, it_service_accounts=it_service_accounts, options=options
)

# We always set a default offset and a limit if the user doesn't specify them, so it is safe to simply put the
# two parameters in the query to slice it.
offset = options.get("offset")
limit = options.get("limit")
limit_offset_validation(offset, limit)

count = len(service_accounts)
service_accounts = service_accounts[offset : offset + limit] # type: ignore

return service_accounts, count

def get_service_accounts_group(self, group: Group, bearer_token: str, options: dict = {}) -> list[dict]:
Expand Down
4 changes: 3 additions & 1 deletion rbac/management/principal/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def get(self, request):
try:
limit = int(query_params.get("limit", default_limit))
offset = int(query_params.get("offset", 0))
if limit < 0 or offset < 0:
raise ValueError
options["limit"] = limit
options["offset"] = offset
options["sort_order"] = validate_and_get_key(query_params, SORTORDER_KEY, VALID_SORTORDER_VALUE, "asc")
Expand Down Expand Up @@ -222,7 +224,7 @@ def get(self, request):
count = len(data)
else:
count = None
response_data["meta"] = {"count": count}
response_data["meta"] = {"count": count, "limit": limit, "offset": offset}
response_data["links"] = {
"first": f"{path}?limit={limit}&offset=0{usernames_filter}",
"next": f"{path}?limit={limit}&offset={offset + limit}{usernames_filter}",
Expand Down

0 comments on commit 33cedd3

Please sign in to comment.