diff --git a/moto/organizations/models.py b/moto/organizations/models.py index 609ce3831..2e0d6b40d 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -195,28 +195,6 @@ class OrganizationsBackend(BaseBackend): ] ) - def list_parents(self, **kwargs): - if re.compile(r'[0-9]{12}').match(kwargs['ChildId']): - parent_id = [ - account.parent_id for account in self.accounts - if account.id == kwargs['ChildId'] - ].pop(0) - else: - parent_id = [ - ou.parent_id for ou in self.ou - if ou.id == kwargs['ChildId'] - ].pop(0) - return dict( - Parents=[ - { - 'Id': ou.id, - 'Type': ou.type, - } - for ou in self.ou - if ou.id == parent_id - ] - ) - def create_account(self, **kwargs): new_account = FakeAccount(self.org, **kwargs) self.accounts.append(new_account) @@ -255,5 +233,43 @@ class OrganizationsBackend(BaseBackend): index = self.accounts.index(account) self.accounts[index].parent_id = new_parent_id + def list_parents(self, **kwargs): + if re.compile(r'[0-9]{12}').match(kwargs['ChildId']): + obj_list = self.accounts + else: + obj_list = self.ou + parent_id = [ + obj.parent_id for obj in obj_list + if obj.id == kwargs['ChildId'] + ].pop(0) + return dict( + Parents=[ + { + 'Id': ou.id, + 'Type': ou.type, + } + for ou in self.ou + if ou.id == parent_id + ] + ) + + def list_children(self, **kwargs): + if kwargs['ChildType'] == 'ACCOUNT': + obj_list = self.accounts + elif kwargs['ChildType'] == 'ORGANIZATIONAL_UNIT': + obj_list = self.ou + else: + raise ValueError + return dict( + Children=[ + { + 'Id': obj.id, + 'Type': kwargs['ChildType'], + } + for obj in obj_list + if obj.parent_id == kwargs['ParentId'] + ] + ) + organizations_backend = OrganizationsBackend() diff --git a/moto/organizations/responses.py b/moto/organizations/responses.py index 6684ae685..966c3fbf3 100644 --- a/moto/organizations/responses.py +++ b/moto/organizations/responses.py @@ -80,3 +80,8 @@ class OrganizationsResponse(BaseResponse): return json.dumps( self.organizations_backend.move_account(**self.request_params) ) + + def list_children(self): + return json.dumps( + self.organizations_backend.list_children(**self.request_params) + ) diff --git a/tests/test_organizations/test_organizations_boto3.py b/tests/test_organizations/test_organizations_boto3.py index 2b692489f..496bcd74d 100644 --- a/tests/test_organizations/test_organizations_boto3.py +++ b/tests/test_organizations/test_organizations_boto3.py @@ -341,3 +341,44 @@ def test_list_parents_for_accounts(): response02['Parents'][0].should.have.key('Id').should.equal(ou01_id) response02['Parents'][0].should.have.key('Type').should.equal('ORGANIZATIONAL_UNIT') #assert False + + +@mock_organizations +def test_list_chidlren(): + client = boto3.client('organizations', region_name='us-east-1') + org = client.create_organization(FeatureSet='ALL')['Organization'] + root_id = client.list_roots()['Roots'][0]['Id'] + ou01 = client.create_organizational_unit(ParentId=root_id, Name='ou01') + ou01_id = ou01['OrganizationalUnit']['Id'] + ou02 = client.create_organizational_unit(ParentId=ou01_id, Name='ou02') + ou02_id = ou02['OrganizationalUnit']['Id'] + account01_id = client.create_account( + AccountName='account01', + Email='account01@moto-example.org' + )['CreateAccountStatus']['AccountId'] + account02_id = client.create_account( + AccountName='account02', + Email='account02@moto-example.org' + )['CreateAccountStatus']['AccountId'] + client.move_account( + AccountId=account02_id, + SourceParentId=root_id, + DestinationParentId=ou01_id, + ) + response01 = client.list_children(ParentId=root_id, ChildType='ACCOUNT') + response02 = client.list_children(ParentId=root_id, ChildType='ORGANIZATIONAL_UNIT') + response03 = client.list_children(ParentId=ou01_id, ChildType='ACCOUNT') + response04 = client.list_children(ParentId=ou01_id, ChildType='ORGANIZATIONAL_UNIT') + #print(yaml.dump(response01, default_flow_style=False)) + #print(yaml.dump(response02, default_flow_style=False)) + #print(yaml.dump(response03, default_flow_style=False)) + #print(yaml.dump(response04, default_flow_style=False)) + response01['Children'][0]['Id'].should.equal(account01_id) + response01['Children'][0]['Type'].should.equal('ACCOUNT') + response02['Children'][0]['Id'].should.equal(ou01_id) + response02['Children'][0]['Type'].should.equal('ORGANIZATIONAL_UNIT') + response03['Children'][0]['Id'].should.equal(account02_id) + response03['Children'][0]['Type'].should.equal('ACCOUNT') + response04['Children'][0]['Id'].should.equal(ou02_id) + response04['Children'][0]['Type'].should.equal('ORGANIZATIONAL_UNIT') + #assert False