From def46b513067251a8b1e7702ff7b1a4619c97959 Mon Sep 17 00:00:00 2001 From: chrisw-dev <68233262+chrisw-dev@users.noreply.github.com> Date: Mon, 15 Feb 2021 03:39:23 -0800 Subject: [PATCH] adding list_create_account_status to organization #3691 (#3692) * adding list_create_account_status to organization #3691 * removing todo comment Co-authored-by: Chris Walters --- IMPLEMENTATION_COVERAGE.md | 4 +- moto/organizations/models.py | 21 +++++++ moto/organizations/responses.py | 5 ++ .../test_organizations_boto3.py | 63 +++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/IMPLEMENTATION_COVERAGE.md b/IMPLEMENTATION_COVERAGE.md index 9d181361b..fc9454664 100644 --- a/IMPLEMENTATION_COVERAGE.md +++ b/IMPLEMENTATION_COVERAGE.md @@ -6416,7 +6416,7 @@ ## organizations
-70% implemented +72% implemented - [ ] accept_handshake - [X] attach_policy @@ -6450,7 +6450,7 @@ - [X] list_accounts_for_parent - [X] list_aws_service_access_for_organization - [X] list_children -- [ ] list_create_account_status +- [X] list_create_account_status - [X] list_delegated_administrators - [X] list_delegated_services_for_account - [ ] list_handshakes_for_account diff --git a/moto/organizations/models.py b/moto/organizations/models.py index 5655326c0..eeda66453 100644 --- a/moto/organizations/models.py +++ b/moto/organizations/models.py @@ -459,6 +459,27 @@ class OrganizationsBackend(BaseBackend): ) return account.create_account_status + def list_create_account_status(self, **kwargs): + requested_states = kwargs.get("States") + if not requested_states: + requested_states = ["IN_PROGRESS", "SUCCEEDED", "FAILED"] + accountStatuses = [] + for account in self.accounts: + create_account_status = account.create_account_status["CreateAccountStatus"] + if create_account_status["State"] in requested_states: + accountStatuses.append(create_account_status) + token = kwargs.get("NextToken") + if token: + start = int(token) + else: + start = 0 + max_results = int(kwargs.get("MaxResults", 123)) + accounts_resp = accountStatuses[start : start + max_results] + next_token = None + if max_results and len(accountStatuses) > (start + max_results): + next_token = str(len(accounts_resp)) + return dict(CreateAccountStatuses=accounts_resp, NextToken=next_token) + def list_accounts(self): return dict(Accounts=[account.describe() for account in self.accounts]) diff --git a/moto/organizations/responses.py b/moto/organizations/responses.py index 73e25178a..609260e9a 100644 --- a/moto/organizations/responses.py +++ b/moto/organizations/responses.py @@ -77,6 +77,11 @@ class OrganizationsResponse(BaseResponse): ) ) + def list_create_account_status(self): + return json.dumps( + self.organizations_backend.list_create_account_status(**self.request_params) + ) + def list_accounts(self): return json.dumps(self.organizations_backend.list_accounts()) diff --git a/tests/test_organizations/test_organizations_boto3.py b/tests/test_organizations/test_organizations_boto3.py index 07cd3afa6..073fea1ed 100644 --- a/tests/test_organizations/test_organizations_boto3.py +++ b/tests/test_organizations/test_organizations_boto3.py @@ -352,6 +352,69 @@ def test_list_children_exception(): ex.response["Error"]["Message"].should.equal("You specified an invalid value.") +@mock_organizations +def test_list_create_account_status(): + client = boto3.client("organizations", region_name="us-east-1") + client.create_organization(FeatureSet="ALL")["Organization"] + response = client.list_create_account_status() + createAccountStatuses = response["CreateAccountStatuses"] + createAccountStatuses.should.have.length_of(1) + validate_create_account_status(createAccountStatuses[0]) + + request_id = client.create_account(AccountName=mockname, Email=mockemail)[ + "CreateAccountStatus" + ]["Id"] + response = client.list_create_account_status() + createAccountStatuses = response["CreateAccountStatuses"] + createAccountStatuses.should.have.length_of(2) + for createAccountStatus in createAccountStatuses: + validate_create_account_status(createAccountStatus) + + +@mock_organizations +def test_list_create_account_status_succeeded(): + client = boto3.client("organizations", region_name="us-east-1") + client.create_organization(FeatureSet="ALL")["Organization"] + requiredStates = ["SUCCEEDED"] + response = client.list_create_account_status(States=requiredStates) + createAccountStatuses = response["CreateAccountStatuses"] + createAccountStatuses.should.have.length_of(1) + validate_create_account_status(createAccountStatuses[0]) + + +@mock_organizations +def test_list_create_account_status_in_progress(): + client = boto3.client("organizations", region_name="us-east-1") + client.create_organization(FeatureSet="ALL")["Organization"] + requiredStates = ["IN_PROGRESS"] + response = client.list_create_account_status(States=requiredStates) + createAccountStatuses = response["CreateAccountStatuses"] + createAccountStatuses.should.have.length_of(0) + + +@mock_organizations +def test_get_paginated_list_create_account_status(): + client = boto3.client("organizations", region_name="us-east-1") + client.create_organization(FeatureSet="ALL")["Organization"] + for i in range(5): + request_id = client.create_account(AccountName=mockname, Email=mockemail)[ + "CreateAccountStatus" + ]["Id"] + response = client.list_create_account_status(MaxResults=2) + createAccountStatuses = response["CreateAccountStatuses"] + createAccountStatuses.should.have.length_of(2) + for createAccountStatus in createAccountStatuses: + validate_create_account_status(createAccountStatus) + next_token = response["NextToken"] + next_token.should_not.be.none + response2 = client.list_create_account_status(NextToken=next_token) + createAccountStatuses.extend(response2["CreateAccountStatuses"]) + createAccountStatuses.should.have.length_of(6) + assert "NextToken" not in response2.keys() + for createAccountStatus in createAccountStatuses: + validate_create_account_status(createAccountStatus) + + # Service Control Policies policy_doc01 = dict( Version="2012-10-17",