Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature createorganizationalunit #693

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -479,22 +479,34 @@ def get_ou_id(self, ou_path, parent_ou_id=None):
parent_ou_id = self.root_id

# Parse ou_path and find the ID
ou_hierarchy = ou_path.strip("/").split("/")
hierarchy_index = 0
ou_path_as_list = ou_path.strip('/').split('/')

while hierarchy_index < len(ou_hierarchy):
for ou in ou_path_as_list:
org_units = self.list_organizational_units_for_parent(parent_ou_id)
for ou in org_units:
if ou["Name"] == ou_hierarchy[hierarchy_index]:
parent_ou_id = ou["Id"]
hierarchy_index += 1

for org_unit in org_units:
if org_unit["Name"] == ou:
parent_ou_id = org_unit["Id"]
break
else:
raise ValueError(
f"Could not find ou with name {ou_hierarchy} in OU list {org_units}.",
)
LOGGER.info(f'No OU found with name {ou} and parent {parent_ou_id}, will create.')
ethanBaird marked this conversation as resolved.
Show resolved Hide resolved
new_ou = self.create_ou(parent_ou_id, ou)
ethanBaird marked this conversation as resolved.
Show resolved Hide resolved
parent_ou_id = new_ou['OrganizationalUnit']['Id']

return parent_ou_id

ethanBaird marked this conversation as resolved.
Show resolved Hide resolved

def create_ou(self, parent_ou_id, name):
try:
ethanBaird marked this conversation as resolved.
Show resolved Hide resolved
ou = self.client.create_organizational_unit(
ParentId=parent_ou_id,
Name=name
)
except ClientError as error:
ethanBaird marked this conversation as resolved.
Show resolved Hide resolved
LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}')
raise error
ethanBaird marked this conversation as resolved.
Show resolved Hide resolved
return ou


def move_account(self, account_id, ou_path):
self.root_id = self.get_ou_root_id()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,24 @@
# adding dependency on datetime
}
}

create_organizational_unit = {
'OrganizationalUnit': {
'Id': 'new_ou_id',
'Arn': 'new_ou_arn',
'Name': 'new_ou_name'
}
}

list_organizational_units_for_parent = [
{
'OrganizationalUnits': [
{
'Id': 'existing_id',
'Arn': 'some_ou_arn',
'Name': 'existing'
},
],
'NextToken': 'string'
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ def cls():
return Organizations(boto3, "123456789012")


def test_create_ou(cls):
cls.client = Mock()
cls.client.create_organizational_unit.return_value = stub_organizations.create_organizational_unit

ou = cls.create_ou("some_parent_id", "some_ou_name")

assert ou['OrganizationalUnit']["Id"] == "new_ou_id"
assert ou['OrganizationalUnit']["Name"] == "new_ou_name"


def test_get_ou_id_can_create_ou_one_layer(cls):
cls.client = Mock()
cls.client.create_organizational_unit.return_value = stub_organizations.create_organizational_unit
cls.client.get_paginator("list_organizational_units_for_parent").paginate.return_value = stub_organizations.list_organizational_units_for_parent

ou_id = cls.get_ou_id("/existing/new")

assert ou_id == "new_ou_id"


def test_get_parent_info(cls):
cls.client = Mock()
cls.client.list_parents.return_value = stub_organizations.list_parents
Expand Down
1 change: 1 addition & 0 deletions src/template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,7 @@ Resources:
- "logs:PutLogEvents"
- "organizations:AttachPolicy"
- "organizations:CreatePolicy"
- "organizations:CreateOrganizationalUnit"
- "organizations:DeletePolicy"
- "organizations:DescribeAccount"
- "organizations:DescribeOrganization"
Expand Down
Loading