Skip to content

Commit 8007aef

Browse files
authored
Organizations: return organization roots for requests made by other accounts within the organization (#8531)
1 parent 6ad9786 commit 8007aef

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

moto/organizations/models.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,18 @@ def delete_organization(self) -> None:
482482
self._reset()
483483

484484
def list_roots(self) -> Dict[str, Any]:
485-
return dict(Roots=[ou.describe() for ou in self.ou if isinstance(ou, FakeRoot)])
485+
if self.org:
486+
return dict(
487+
Roots=[ou.describe() for ou in self.ou if isinstance(ou, FakeRoot)]
488+
)
489+
490+
if self.account_id in organizations_backends.master_accounts:
491+
master_account_id, partition = organizations_backends.master_accounts[
492+
self.account_id
493+
]
494+
return organizations_backends[master_account_id][partition].list_roots()
495+
496+
raise AWSOrganizationsNotInUseException
486497

487498
def create_organizational_unit(self, **kwargs: Any) -> Dict[str, Any]:
488499
new_ou = FakeOrganizationalUnit(self.org, **kwargs) # type: ignore

tests/test_organizations/test_organizations_boto3.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,42 @@ def test_list_roots():
189189
validate_roots(org, response)
190190

191191

192+
@mock_aws
193+
def test_list_roots_for_new_account_in_organization():
194+
if not settings.TEST_DECORATOR_MODE:
195+
raise SkipTest("Involves changing account using env variable")
196+
client = boto3.client("organizations", region_name="us-east-1")
197+
org = client.create_organization()["Organization"]
198+
org_roots = client.list_roots()
199+
new_account_id = client.create_account(
200+
AccountName="test_account",
201+
202+
RoleName="CustomOrganizationRole",
203+
)["CreateAccountStatus"]["AccountId"]
204+
205+
with mock.patch.dict(os.environ, {"MOTO_ACCOUNT_ID": new_account_id}):
206+
client_for_new_account = boto3.client("organizations", "us-east-1")
207+
new_account_roots = client_for_new_account.list_roots()
208+
validate_roots(org, new_account_roots)
209+
assert len(new_account_roots["Roots"]) == 1
210+
assert len(org_roots["Roots"]) == len(new_account_roots["Roots"])
211+
assert org_roots["Roots"][0] == new_account_roots["Roots"][0]
212+
213+
214+
@mock_aws
215+
def test_list_roots_for_account_without_organization_exception():
216+
client = boto3.client("organizations", region_name="us-east-1")
217+
with pytest.raises(ClientError) as e:
218+
client.list_roots()
219+
ex = e.value
220+
assert ex.operation_name == "ListRoots"
221+
assert ex.response["ResponseMetadata"]["HTTPStatusCode"] == 400
222+
assert "AWSOrganizationsNotInUseException" in ex.response["Error"]["Code"]
223+
assert ex.response["Error"]["Message"] == (
224+
"Your account is not a member of an organization."
225+
)
226+
227+
192228
@mock_aws
193229
def test_create_organizational_unit():
194230
client = boto3.client("organizations", region_name="us-east-1")

0 commit comments

Comments
 (0)