Add Pagination for Organizations:ListOrganizationalUnitsForParent

This commit is contained in:
Brian Pandola 2022-05-13 19:40:29 -07:00 committed by Brian Pandola
parent f65d3970aa
commit f389d507e8
4 changed files with 45 additions and 12 deletions

View File

@ -434,15 +434,14 @@ class OrganizationsBackend(BaseBackend):
ou = self.get_organizational_unit_by_id(kwargs["OrganizationalUnitId"])
return ou.describe()
@paginate(pagination_model=PAGINATION_MODEL)
def list_organizational_units_for_parent(self, **kwargs):
parent_id = self.validate_parent_id(kwargs["ParentId"])
return dict(
OrganizationalUnits=[
{"Id": ou.id, "Arn": ou.arn, "Name": ou.name}
for ou in self.ou
if ou.parent_id == parent_id
]
)
parent_id = self.validate_parent_id(kwargs["parent_id"])
return [
{"Id": ou.id, "Arn": ou.arn, "Name": ou.name}
for ou in self.ou
if ou.parent_id == parent_id
]
def create_account(self, **kwargs):
new_account = FakeAccount(self.org, **kwargs)

View File

@ -51,11 +51,19 @@ class OrganizationsResponse(BaseResponse):
)
def list_organizational_units_for_parent(self):
return json.dumps(
self.organizations_backend.list_organizational_units_for_parent(
**self.request_params
)
max_results = self._get_int_param("MaxResults")
next_token = self._get_param("NextToken")
parent_id = self._get_param("ParentId")
(
ous,
next_token,
) = self.organizations_backend.list_organizational_units_for_parent(
max_results=max_results, next_token=next_token, parent_id=parent_id
)
response = {"OrganizationalUnits": ous}
if next_token:
response["NextToken"] = next_token
return json.dumps(response)
def list_parents(self):
return json.dumps(

View File

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

View File

@ -143,6 +143,25 @@ def test_list_organizational_units_for_parent():
validate_organizational_unit(org, dict(OrganizationalUnit=ou))
@mock_organizations
def test_list_organizational_units_pagination():
client = boto3.client("organizations", region_name="us-east-1")
client.create_organization(FeatureSet="ALL")
root_id = client.list_roots()["Roots"][0]["Id"]
for i in range(20):
name = "ou" + str(i)
client.create_organizational_unit(ParentId=root_id, Name=name)
response = client.list_organizational_units_for_parent(ParentId=root_id)
response.should_not.have.key("NextToken")
len(response["OrganizationalUnits"]).should.be.greater_than_or_equal_to(i)
paginator = client.get_paginator("list_organizational_units_for_parent")
page_iterator = paginator.paginate(MaxResults=5, ParentId=root_id)
for page in page_iterator:
len(page["OrganizationalUnits"]).should.be.lower_than_or_equal_to(5)
page["OrganizationalUnits"][-1]["Name"].should.contain("19")
@mock_organizations
def test_list_organizational_units_for_parent_exception():
client = boto3.client("organizations", region_name="us-east-1")