organizations: add endpoint list_chilren

This commit is contained in:
Ashley Gould 2018-07-15 22:19:42 -07:00
parent 30a9aa33e5
commit 8f400b7110
3 changed files with 84 additions and 22 deletions

View File

@ -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()

View File

@ -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)
)

View File

@ -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