Add Pagination for Organizations:ListAccountsForParent

Closes #5034
This commit is contained in:
Brian Pandola 2022-05-13 20:02:43 -07:00 committed by Brian Pandola
parent f389d507e8
commit eb3da8f91d
4 changed files with 47 additions and 10 deletions

View File

@ -514,15 +514,16 @@ class OrganizationsBackend(BaseBackend):
accounts = sorted(accounts, key=lambda x: x["JoinedTimestamp"])
return accounts
@paginate(pagination_model=PAGINATION_MODEL)
def list_accounts_for_parent(self, **kwargs):
parent_id = self.validate_parent_id(kwargs["ParentId"])
return dict(
Accounts=[
parent_id = self.validate_parent_id(kwargs["parent_id"])
accounts = [
account.describe()
for account in self.accounts
if account.parent_id == parent_id
]
)
accounts = sorted(accounts, key=lambda x: x["JoinedTimestamp"])
return accounts
def move_account(self, **kwargs):
new_parent_id = self.validate_parent_id(kwargs["DestinationParentId"])

View File

@ -109,9 +109,16 @@ class OrganizationsResponse(BaseResponse):
return json.dumps(response)
def list_accounts_for_parent(self):
return json.dumps(
self.organizations_backend.list_accounts_for_parent(**self.request_params)
max_results = self._get_int_param("MaxResults")
next_token = self._get_param("NextToken")
parent_id = self._get_param("ParentId")
accounts, next_token = self.organizations_backend.list_accounts_for_parent(
max_results=max_results, next_token=next_token, parent_id=parent_id
)
response = {"Accounts": accounts}
if next_token:
response["NextToken"] = next_token
return json.dumps(response)
def move_account(self):
return json.dumps(

View File

@ -41,6 +41,13 @@ PAGINATION_MODEL = {
"result_key": "Accounts",
"unique_attribute": "JoinedTimestamp",
},
"list_accounts_for_parent": {
"input_token": "next_token",
"limit_key": "max_results",
"limit_default": 20,
"result_key": "Accounts",
"unique_attribute": "JoinedTimestamp",
},
"list_organizational_units_for_parent": {
"input_token": "next_token",
"limit_key": "max_results",

View File

@ -327,6 +327,28 @@ def test_list_accounts_for_parent():
account_id.should.be.within([account["Id"] for account in response["Accounts"]])
@mock_organizations
def test_list_accounts_for_parent_pagination():
client = boto3.client("organizations", region_name="us-east-1")
client.create_organization(FeatureSet="ALL")
root_id = client.list_roots()["Roots"][0]["Id"]
response = client.list_accounts_for_parent(ParentId=root_id)
response.should_not.have.key("NextToken")
num_existing_accounts = len(response["Accounts"])
for i in range(num_existing_accounts, 21):
name = mockname + str(i)
email = name + "@" + mockdomain
client.create_account(AccountName=name, Email=email)
response = client.list_accounts_for_parent(ParentId=root_id)
len(response["Accounts"]).should.be.greater_than_or_equal_to(i)
paginator = client.get_paginator("list_accounts_for_parent")
page_iterator = paginator.paginate(MaxResults=5, ParentId=root_id)
for page in page_iterator:
len(page["Accounts"]).should.be.lower_than_or_equal_to(5)
page["Accounts"][-1]["Name"].should.contain("20")
@mock_organizations
def test_move_account():
client = boto3.client("organizations", region_name="us-east-1")