diff --git a/moto/organizations/exceptions.py b/moto/organizations/exceptions.py index 10fb8ecbedb3..4ca0fcf64931 100644 --- a/moto/organizations/exceptions.py +++ b/moto/organizations/exceptions.py @@ -40,6 +40,14 @@ def __init__(self) -> None: ) +class AlreadyInOrganizationException(JsonRESTError): + code = 400 + + def __init__(self) -> None: + super().__init__( + "AlreadyInOrganizationException", "The provided account is already a member of an organization." + ) + class AWSOrganizationsNotInUseException(JsonRESTError): code = 400 @@ -83,6 +91,15 @@ def __init__(self) -> None: ) +class OrganizationNotEmptyException(JsonRESTError): + code = 400 + + def __init__(self) -> None: + super().__init__( + "OrganizationNotEmptyException", "To delete an organization you must first remove all member accounts (except the master).", + ) + + class PolicyTypeAlreadyEnabledException(JsonRESTError): code = 400 diff --git a/moto/organizations/models.py b/moto/organizations/models.py index d84f0ca2e1e9..29bbb5e98683 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -21,7 +21,7 @@ PolicyTypeAlreadyEnabledException, PolicyTypeNotEnabledException, RootNotFoundException, - TargetNotFoundException, + TargetNotFoundException, AlreadyInOrganizationException, OrganizationNotEmptyException, ) from moto.utilities.paginator import paginate from moto.utilities.utils import PARTITION_NAMES, get_partition @@ -422,6 +422,9 @@ def _get_root_by_id(self, root_id: str) -> FakeRoot: return root # type: ignore[return-value] def create_organization(self, region: str, **kwargs: Any) -> Dict[str, Any]: + if self.org or self.account_id in organizations_backends.master_accounts: + raise AlreadyInOrganizationException + self.org = FakeOrganization( self.account_id, region_name=region, @@ -468,11 +471,12 @@ def describe_organization(self) -> Dict[str, Any]: raise AWSOrganizationsNotInUseException def delete_organization(self) -> None: + if self.org is None: + raise AWSOrganizationsNotInUseException + if [account for account in self.accounts if account.name != "master"]: - raise RESTError( - "OrganizationNotEmptyException", - "To delete an organization you must first remove all member accounts (except the master).", - ) + raise OrganizationNotEmptyException + self._reset() def list_roots(self) -> Dict[str, Any]: @@ -532,6 +536,9 @@ def list_organizational_units_for_parent( ] def create_account(self, **kwargs: Any) -> Dict[str, Any]: + if self.org is None or self.account_id not in organizations_backends.master_accounts: + raise AWSOrganizationsNotInUseException + new_account = FakeAccount(self.org, **kwargs) # type: ignore self.accounts.append(new_account) self.attach_policy(PolicyId=utils.DEFAULT_POLICY_ID, TargetId=new_account.id) @@ -542,6 +549,9 @@ def create_account(self, **kwargs: Any) -> Dict[str, Any]: return new_account.create_account_status def close_account(self, **kwargs: Any) -> None: + if self.org is None or self.account_id not in organizations_backends.master_accounts: + raise AWSOrganizationsNotInUseException + for account in self.accounts: if account.id == kwargs["AccountId"]: account.close()