diff --git a/moto/organizations/models.py b/moto/organizations/models.py index 4ad630cec..2b51a8043 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -405,6 +405,13 @@ class OrganizationsBackend(BaseBackend): self.attach_policy(PolicyId=utils.DEFAULT_POLICY_ID, TargetId=new_ou.id) return new_ou.describe() + def delete_organizational_unit(self, **kwargs): + ou_to_delete = self.get_organizational_unit_by_id( + kwargs["OrganizationalUnitId"] + ) + self.ou.remove(ou_to_delete) + return {} + def update_organizational_unit(self, **kwargs): for ou in self.ou: if ou.name == kwargs["Name"]: diff --git a/moto/organizations/responses.py b/moto/organizations/responses.py index f30e2b4bf..321b7997c 100644 --- a/moto/organizations/responses.py +++ b/moto/organizations/responses.py @@ -41,6 +41,11 @@ class OrganizationsResponse(BaseResponse): self.organizations_backend.create_organizational_unit(**self.request_params) ) + def delete_organizational_unit(self): + return json.dumps( + self.organizations_backend.delete_organizational_unit(**self.request_params) + ) + def update_organizational_unit(self): return json.dumps( self.organizations_backend.update_organizational_unit(**self.request_params) diff --git a/tests/test_organizations/test_organizations_boto3.py b/tests/test_organizations/test_organizations_boto3.py index f4d273e62..4a04da617 100644 --- a/tests/test_organizations/test_organizations_boto3.py +++ b/tests/test_organizations/test_organizations_boto3.py @@ -110,6 +110,31 @@ def test_create_organizational_unit(): response["OrganizationalUnit"]["Name"].should.equal(ou_name) +@mock_organizations +def test_delete_organizational_unit(): + client = boto3.client("organizations", region_name="us-east-1") + org = client.create_organization(FeatureSet="ALL")["Organization"] + root_id = client.list_roots()["Roots"][0]["Id"] + ou_name = "ou01" + response = client.create_organizational_unit(ParentId=root_id, Name=ou_name) + validate_organizational_unit(org, response) + + # delete organizational unit + ou_id = response["OrganizationalUnit"]["Id"] + response = client.delete_organizational_unit(OrganizationalUnitId=ou_id) + response["ResponseMetadata"]["HTTPStatusCode"].should.equal(200) + + # verify the deletion + with pytest.raises(ClientError) as e: + client.describe_organizational_unit(OrganizationalUnitId=ou_id) + ex = e.value + ex.operation_name.should.equal("DescribeOrganizationalUnit") + ex.response["Error"]["Code"].should.equal("400") + ex.response["Error"]["Message"].should.contain( + "OrganizationalUnitNotFoundException" + ) + + @mock_organizations def test_describe_organizational_unit(): client = boto3.client("organizations", region_name="us-east-1")