From cd74d54f914e2578bbe30afc46f5d6327f0c04e0 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:08:22 +0000 Subject: [PATCH 01/20] adds permission to adf-codebuild-role this is the service role for aws-deployment-framework-base-templates this is the codebuild project where the provisioner runs which will be responsible for creating the new OUs --- src/template.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/template.yml b/src/template.yml index 84034c32b..8e0878956 100644 --- a/src/template.yml +++ b/src/template.yml @@ -1245,6 +1245,7 @@ Resources: - "logs:PutLogEvents" - "organizations:AttachPolicy" - "organizations:CreatePolicy" + - "organizations:CreateOrganizationalUnit" - "organizations:DeletePolicy" - "organizations:DescribeAccount" - "organizations:DescribeOrganization" From b940f394559aa773d4b3d28c98f9a9ac7f1b5381 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:10:23 +0000 Subject: [PATCH 02/20] add tests and stubs test for new method create_ou test for ammended get_ou_id for/else --- .../python/tests/stubs/stub_organizations.py | 21 +++++++++++++++++++ .../shared/python/tests/test_organizations.py | 20 ++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py index c72a11a3b..7e8b0a0b1 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py @@ -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' + } +] \ No newline at end of file diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py index 5b5c5d4cc..125dfeee6 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py @@ -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 From 96d503dea20e13a951f87c91568c4fb187393fc1 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:11:51 +0000 Subject: [PATCH 03/20] adds method for create_ou --- .../adf-build/shared/python/organizations.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 1f823b89e..230e2c730 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -495,6 +495,19 @@ def get_ou_id(self, ou_path, parent_ou_id=None): ) return parent_ou_id + + + def create_ou(self, parent_ou_id, name): + try: + ou = self.client.create_organizational_unit( + ParentId=parent_ou_id, + Name=name + ) + except ClientError as error: + LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}') + raise error + return ou + def move_account(self, account_id, ou_path): self.root_id = self.get_ou_root_id() From b8dfec6d7cb8ec54a04951872966395430d5e98a Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:12:58 +0000 Subject: [PATCH 04/20] ammends get_ou_id refactors while loop to for loop changes logic in for/else to create ou if not found --- .../adf-build/shared/python/organizations.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 230e2c730..6fef506fd 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -479,20 +479,19 @@ 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.') + new_ou = self.create_ou(parent_ou_id, ou) + parent_ou_id = new_ou['OrganizationalUnit']['Id'] return parent_ou_id From 7928acc94f344abed6bb6560cb87692048a445e5 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:52:00 +0100 Subject: [PATCH 05/20] whitespace fixes --- .../adf-build/shared/python/organizations.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 6fef506fd..a52ccf384 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -490,14 +490,13 @@ def get_ou_id(self, ou_path, parent_ou_id=None): break else: LOGGER.info(f'No OU found with name {ou} and parent {parent_ou_id}, will create.') - new_ou = self.create_ou(parent_ou_id, ou) + new_ou = self.create_ou(parent_ou_id, ou) parent_ou_id = new_ou['OrganizationalUnit']['Id'] return parent_ou_id - def create_ou(self, parent_ou_id, name): - try: + try: ou = self.client.create_organizational_unit( ParentId=parent_ou_id, Name=name From 62a4791dbdc0883a1fa534bb07447fd38d098877 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:55:29 +0100 Subject: [PATCH 06/20] newline --- .../adf-build/shared/python/tests/stubs/stub_organizations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py index 58b558ee5..f7230a605 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py @@ -82,4 +82,4 @@ ], 'NextToken': 'string' } -] \ No newline at end of file +] From 7c94a5cfaf1c2bba3cd8b90bfe248e88e68af313 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:59:13 +0100 Subject: [PATCH 07/20] raise organizations excpetion --- .../adf-build/shared/python/organizations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 5a1111ddf..67089017c 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -501,9 +501,9 @@ def create_ou(self, parent_ou_id, name): ParentId=parent_ou_id, Name=name ) - except ClientError as error: + except: LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}') - raise error + raise OrganizationsException() return ou From 60532cf4a50efe8323080f6fcd0cc3a34d39ef6f Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:59:47 +0100 Subject: [PATCH 08/20] logger.exception --- .../adf-build/shared/python/organizations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 67089017c..e3f765585 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -502,7 +502,7 @@ def create_ou(self, parent_ou_id, name): Name=name ) except: - LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}') + LOGGER.excpetion(f'Failed to create OU called {name}, with parent {parent_ou_id}') raise OrganizationsException() return ou From 4798b26724e75254bdcc8f9d6845016f7ddb1fd2 Mon Sep 17 00:00:00 2001 From: ethan-baird Date: Wed, 10 Apr 2024 17:06:15 +0100 Subject: [PATCH 09/20] fix linting errors --- .../adf-build/shared/python/organizations.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index e3f765585..b41b9da9f 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -489,7 +489,9 @@ def get_ou_id(self, ou_path, parent_ou_id=None): parent_ou_id = org_unit["Id"] break else: - LOGGER.info(f'No OU found with name {ou} and parent {parent_ou_id}, will create.') + LOGGER.info( + 'No OU found with name {%s} and parent {%s}, will create.', ou, parent_ou_id + ) new_ou = self.create_ou(parent_ou_id, ou) parent_ou_id = new_ou['OrganizationalUnit']['Id'] @@ -501,9 +503,11 @@ def create_ou(self, parent_ou_id, name): ParentId=parent_ou_id, Name=name ) - except: - LOGGER.excpetion(f'Failed to create OU called {name}, with parent {parent_ou_id}') - raise OrganizationsException() + except Exception as exc: + LOGGER.exception( + 'Failed to create OU called {%s}, with parent {%s}', name, parent_ou_id + ) + raise OrganizationsException() from exc return ou From cf233774014c857745f4bbf592a8c88948dd413f Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:08:22 +0000 Subject: [PATCH 10/20] adds permission to adf-codebuild-role this is the service role for aws-deployment-framework-base-templates this is the codebuild project where the provisioner runs which will be responsible for creating the new OUs --- src/template.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/template.yml b/src/template.yml index f772b75dd..fcbab4c96 100644 --- a/src/template.yml +++ b/src/template.yml @@ -1256,6 +1256,7 @@ Resources: - "logs:PutLogEvents" - "organizations:AttachPolicy" - "organizations:CreatePolicy" + - "organizations:CreateOrganizationalUnit" - "organizations:DeletePolicy" - "organizations:DescribeAccount" - "organizations:DescribeOrganization" From 67db32344a9953710d1e7fd1eeab3b244b7054b4 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:10:23 +0000 Subject: [PATCH 11/20] add tests and stubs test for new method create_ou test for ammended get_ou_id for/else --- .../python/tests/stubs/stub_organizations.py | 21 +++++++++++++++++++ .../shared/python/tests/test_organizations.py | 20 ++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py index 371941cb6..61fb98a80 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py @@ -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' + } +] \ No newline at end of file diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py index 65dc72b83..ed48531d2 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py @@ -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 From d66b571db4afb3792fc43c2b2f1dbf7b1a37f643 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:11:51 +0000 Subject: [PATCH 12/20] adds method for create_ou --- .../adf-build/shared/python/organizations.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index c6afb6d20..ed4230c4a 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -495,6 +495,19 @@ def get_ou_id(self, ou_path, parent_ou_id=None): ) return parent_ou_id + + + def create_ou(self, parent_ou_id, name): + try: + ou = self.client.create_organizational_unit( + ParentId=parent_ou_id, + Name=name + ) + except ClientError as error: + LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}') + raise error + return ou + def move_account(self, account_id, ou_path): self.root_id = self.get_ou_root_id() From 9242803900ed2a3d70f48b8968e175073b12772a Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Thu, 14 Mar 2024 12:12:58 +0000 Subject: [PATCH 13/20] ammends get_ou_id refactors while loop to for loop changes logic in for/else to create ou if not found --- .../adf-build/shared/python/organizations.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index ed4230c4a..54f57847c 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -479,20 +479,19 @@ 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.') + new_ou = self.create_ou(parent_ou_id, ou) + parent_ou_id = new_ou['OrganizationalUnit']['Id'] return parent_ou_id From 911aeef99d4e10f4150e3481977cc7794db52ae5 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:52:00 +0100 Subject: [PATCH 14/20] whitespace fixes --- .../adf-build/shared/python/organizations.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 54f57847c..2350aed13 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -490,14 +490,13 @@ def get_ou_id(self, ou_path, parent_ou_id=None): break else: LOGGER.info(f'No OU found with name {ou} and parent {parent_ou_id}, will create.') - new_ou = self.create_ou(parent_ou_id, ou) + new_ou = self.create_ou(parent_ou_id, ou) parent_ou_id = new_ou['OrganizationalUnit']['Id'] return parent_ou_id - def create_ou(self, parent_ou_id, name): - try: + try: ou = self.client.create_organizational_unit( ParentId=parent_ou_id, Name=name From 2cf545c31ff67a713b4659735fe120375cd80d4f Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:55:29 +0100 Subject: [PATCH 15/20] newline --- .../adf-build/shared/python/tests/stubs/stub_organizations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py index 61fb98a80..e2508e004 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/stubs/stub_organizations.py @@ -82,4 +82,4 @@ ], 'NextToken': 'string' } -] \ No newline at end of file +] From 2bc03c93bdb8267a20b849329c469808b10fb3aa Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:59:13 +0100 Subject: [PATCH 16/20] raise organizations excpetion --- .../adf-build/shared/python/organizations.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 2350aed13..76c1a40cc 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -501,9 +501,9 @@ def create_ou(self, parent_ou_id, name): ParentId=parent_ou_id, Name=name ) - except ClientError as error: + except: LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}') - raise error + raise OrganizationsException() return ou From e93c3e7acf55718d3ebb1b08a8dace8b154c0551 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Tue, 9 Apr 2024 21:59:47 +0100 Subject: [PATCH 17/20] logger.exception --- .../adf-build/shared/python/organizations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index 76c1a40cc..b09f1cc69 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -502,7 +502,7 @@ def create_ou(self, parent_ou_id, name): Name=name ) except: - LOGGER.error(f'Failed to create OU called {name}, with parent {parent_ou_id}') + LOGGER.excpetion(f'Failed to create OU called {name}, with parent {parent_ou_id}') raise OrganizationsException() return ou From 8a596062e1640444f3ddfc394db14263078895e6 Mon Sep 17 00:00:00 2001 From: ethan-baird Date: Wed, 10 Apr 2024 17:06:15 +0100 Subject: [PATCH 18/20] fix linting errors --- .../adf-build/shared/python/organizations.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index b09f1cc69..fb216d4e5 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -489,7 +489,9 @@ def get_ou_id(self, ou_path, parent_ou_id=None): parent_ou_id = org_unit["Id"] break else: - LOGGER.info(f'No OU found with name {ou} and parent {parent_ou_id}, will create.') + LOGGER.info( + 'No OU found with name {%s} and parent {%s}, will create.', ou, parent_ou_id + ) new_ou = self.create_ou(parent_ou_id, ou) parent_ou_id = new_ou['OrganizationalUnit']['Id'] @@ -501,9 +503,11 @@ def create_ou(self, parent_ou_id, name): ParentId=parent_ou_id, Name=name ) - except: - LOGGER.excpetion(f'Failed to create OU called {name}, with parent {parent_ou_id}') - raise OrganizationsException() + except Exception as exc: + LOGGER.exception( + 'Failed to create OU called {%s}, with parent {%s}', name, parent_ou_id + ) + raise OrganizationsException() from exc return ou From f2a8b749967b1c921ad2b8811830fbca4e52b3bb Mon Sep 17 00:00:00 2001 From: ethan-baird Date: Sat, 8 Jun 2024 10:53:12 +0100 Subject: [PATCH 19/20] improve exception --- .../adf-build/shared/python/organizations.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py index fb216d4e5..7a03cb7be 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/organizations.py @@ -503,11 +503,10 @@ def create_ou(self, parent_ou_id, name): ParentId=parent_ou_id, Name=name ) - except Exception as exc: - LOGGER.exception( - 'Failed to create OU called {%s}, with parent {%s}', name, parent_ou_id - ) - raise OrganizationsException() from exc + except ClientError as client_err: + message = f'Failed to create OU called {name}, with parent {parent_ou_id}' + LOGGER.exception(message, client_err) + raise OrganizationsException(message) from client_err return ou From fa59ef27731bc97d57995f9f18927a143ba23dd4 Mon Sep 17 00:00:00 2001 From: ethanBaird Date: Sat, 8 Jun 2024 11:28:48 +0100 Subject: [PATCH 20/20] add test for create_ou raising OrganizationsException --- .../adf-build/shared/python/tests/test_organizations.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py index ed48531d2..ba775b0fd 100644 --- a/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py +++ b/src/lambda_codebase/initial_commit/bootstrap_repository/adf-build/shared/python/tests/test_organizations.py @@ -7,12 +7,13 @@ import os import boto3 -from pytest import fixture +from pytest import fixture, raises from stubs import stub_organizations from mock import Mock, patch from cache import Cache from organizations import Organizations, OrganizationsException from botocore.stub import Stubber +from botocore.exceptions import ClientError import unittest @@ -30,6 +31,12 @@ def test_create_ou(cls): assert ou['OrganizationalUnit']["Id"] == "new_ou_id" assert ou['OrganizationalUnit']["Name"] == "new_ou_name" +def test_create_ou_throws_client_error(cls): + cls.client = Mock() + cls.client.create_organizational_unit.side_effect = ClientError(operation_name='test', error_response={'Error': {'Code': 'Test', 'Message': 'Test Message'}}) + with raises(OrganizationsException): + cls.create_ou("some_parent_id", "some_ou_name") + def test_get_ou_id_can_create_ou_one_layer(cls): cls.client = Mock()