add support for AWS Organizations
endpoints covers so far: - create_organization - describe_organization - create_account - describe_account - list_accounts all tests passing. could use some advise from maintaners.
This commit is contained in:
parent
a1d095c14b
commit
edbc57e00d
11 changed files with 475 additions and 0 deletions
|
|
@ -27,6 +27,7 @@ from .glacier import mock_glacier, mock_glacier_deprecated # flake8: noqa
|
|||
from .iam import mock_iam, mock_iam_deprecated # flake8: noqa
|
||||
from .kinesis import mock_kinesis, mock_kinesis_deprecated # flake8: noqa
|
||||
from .kms import mock_kms, mock_kms_deprecated # flake8: noqa
|
||||
from .organizations import mock_organizations # flake8: noqa
|
||||
from .opsworks import mock_opsworks, mock_opsworks_deprecated # flake8: noqa
|
||||
from .polly import mock_polly # flake8: noqa
|
||||
from .rds import mock_rds, mock_rds_deprecated # flake8: noqa
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ from moto.kinesis import kinesis_backends
|
|||
from moto.kms import kms_backends
|
||||
from moto.logs import logs_backends
|
||||
from moto.opsworks import opsworks_backends
|
||||
from moto.organizations import organizations_backends
|
||||
from moto.polly import polly_backends
|
||||
from moto.rds2 import rds2_backends
|
||||
from moto.redshift import redshift_backends
|
||||
|
|
@ -72,6 +73,7 @@ BACKENDS = {
|
|||
'kinesis': kinesis_backends,
|
||||
'kms': kms_backends,
|
||||
'opsworks': opsworks_backends,
|
||||
'organizations': organizations_backends,
|
||||
'polly': polly_backends,
|
||||
'redshift': redshift_backends,
|
||||
'rds': rds2_backends,
|
||||
|
|
|
|||
6
moto/organizations/__init__.py
Normal file
6
moto/organizations/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from __future__ import unicode_literals
|
||||
from .models import organizations_backend
|
||||
from ..core.models import base_decorator
|
||||
|
||||
organizations_backends = {"global": organizations_backend}
|
||||
mock_organizations = base_decorator(organizations_backends)
|
||||
131
moto/organizations/models.py
Normal file
131
moto/organizations/models.py
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
import time
|
||||
|
||||
from moto.core import BaseBackend, BaseModel
|
||||
from moto.core.utils import unix_time
|
||||
from moto.organizations import utils
|
||||
|
||||
MASTER_ACCOUNT_ID = '123456789012'
|
||||
MASTER_ACCOUNT_EMAIL = 'fakeorg@moto-example.com'
|
||||
ORGANIZATION_ARN_FORMAT = 'arn:aws:organizations::{0}:organization/{1}'
|
||||
MASTER_ACCOUNT_ARN_FORMAT = 'arn:aws:organizations::{0}:account/{1}/{0}'
|
||||
ACCOUNT_ARN_FORMAT = 'arn:aws:organizations::{0}:account/{1}/{2}'
|
||||
|
||||
|
||||
class FakeOrganization(BaseModel):
|
||||
|
||||
def __init__(self, feature_set):
|
||||
self.id = utils.make_random_org_id()
|
||||
self.feature_set = feature_set
|
||||
self.master_account_id = MASTER_ACCOUNT_ID
|
||||
self.master_account_email = MASTER_ACCOUNT_EMAIL
|
||||
self.available_policy_types = [{
|
||||
'Type': 'SERVICE_CONTROL_POLICY',
|
||||
'Status': 'ENABLED'
|
||||
}]
|
||||
|
||||
@property
|
||||
def arn(self):
|
||||
return ORGANIZATION_ARN_FORMAT.format(self.master_account_id, self.id)
|
||||
|
||||
@property
|
||||
def master_account_arn(self):
|
||||
return MASTER_ACCOUNT_ARN_FORMAT.format(self.master_account_id, self.id)
|
||||
|
||||
def _describe(self):
|
||||
return {
|
||||
'Organization': {
|
||||
'Id': self.id,
|
||||
'Arn': self.arn,
|
||||
'FeatureSet': self.feature_set,
|
||||
'MasterAccountArn': self.master_account_arn,
|
||||
'MasterAccountId': self.master_account_id,
|
||||
'MasterAccountEmail': self.master_account_email,
|
||||
'AvailablePolicyTypes': self.available_policy_types,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class FakeAccount(BaseModel):
|
||||
|
||||
def __init__(self, organization, **kwargs):
|
||||
self.organization_id = organization.id
|
||||
self.master_account_id = organization.master_account_id
|
||||
self.create_account_status_id = utils.make_random_create_account_status_id()
|
||||
self.account_id = utils.make_random_account_id()
|
||||
self.account_name = kwargs['AccountName']
|
||||
self.email = kwargs['Email']
|
||||
self.create_time = datetime.datetime.utcnow()
|
||||
self.status = 'ACTIVE'
|
||||
self.joined_method = 'CREATED'
|
||||
|
||||
@property
|
||||
def arn(self):
|
||||
return ACCOUNT_ARN_FORMAT.format(
|
||||
self.master_account_id,
|
||||
self.organization_id,
|
||||
self.account_id
|
||||
)
|
||||
|
||||
@property
|
||||
def create_account_status(self):
|
||||
return {
|
||||
'CreateAccountStatus': {
|
||||
'Id': self.create_account_status_id,
|
||||
'AccountName': self.account_name,
|
||||
'State': 'SUCCEEDED',
|
||||
'RequestedTimestamp': unix_time(self.create_time),
|
||||
'CompletedTimestamp': unix_time(self.create_time),
|
||||
'AccountId': self.account_id,
|
||||
}
|
||||
}
|
||||
|
||||
def describe(self):
|
||||
return {
|
||||
'Account': {
|
||||
'Id': self.account_id,
|
||||
'Arn': self.arn,
|
||||
'Email': self.email,
|
||||
'Name': self.account_name,
|
||||
'Status': self.status,
|
||||
'JoinedMethod': self.joined_method,
|
||||
'JoinedTimestamp': unix_time(self.create_time),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class OrganizationsBackend(BaseBackend):
|
||||
|
||||
def __init__(self):
|
||||
self.org = None
|
||||
self.accounts = []
|
||||
|
||||
def create_organization(self, **kwargs):
|
||||
self.org = FakeOrganization(kwargs['FeatureSet'])
|
||||
return self.org._describe()
|
||||
|
||||
def describe_organization(self):
|
||||
return self.org._describe()
|
||||
|
||||
def create_account(self, **kwargs):
|
||||
new_account = FakeAccount(self.org, **kwargs)
|
||||
self.accounts.append(new_account)
|
||||
return new_account.create_account_status
|
||||
|
||||
def describe_account(self, **kwargs):
|
||||
account = [account for account in self.accounts
|
||||
if account.account_id == kwargs['AccountId']][0]
|
||||
return account.describe()
|
||||
|
||||
def list_accounts(self):
|
||||
return dict(
|
||||
Accounts=[account.describe()['Account'] for account in self.accounts]
|
||||
)
|
||||
|
||||
|
||||
organizations_backend = OrganizationsBackend()
|
||||
|
||||
|
||||
|
||||
48
moto/organizations/responses.py
Normal file
48
moto/organizations/responses.py
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
from __future__ import unicode_literals
|
||||
import json
|
||||
|
||||
from moto.core.responses import BaseResponse
|
||||
from .models import organizations_backend
|
||||
|
||||
|
||||
class OrganizationsResponse(BaseResponse):
|
||||
|
||||
@property
|
||||
def organizations_backend(self):
|
||||
return organizations_backend
|
||||
|
||||
@property
|
||||
def request_params(self):
|
||||
try:
|
||||
return json.loads(self.body)
|
||||
except ValueError:
|
||||
return {}
|
||||
|
||||
def _get_param(self, param, default=None):
|
||||
return self.request_params.get(param, default)
|
||||
|
||||
def create_organization(self):
|
||||
return json.dumps(
|
||||
self.organizations_backend.create_organization(**self.request_params)
|
||||
)
|
||||
|
||||
def describe_organization(self):
|
||||
return json.dumps(
|
||||
self.organizations_backend.describe_organization()
|
||||
)
|
||||
|
||||
def create_account(self):
|
||||
return json.dumps(
|
||||
self.organizations_backend.create_account(**self.request_params)
|
||||
)
|
||||
|
||||
def describe_account(self):
|
||||
return json.dumps(
|
||||
self.organizations_backend.describe_account(**self.request_params)
|
||||
)
|
||||
|
||||
def list_accounts(self):
|
||||
return json.dumps(
|
||||
self.organizations_backend.list_accounts()
|
||||
)
|
||||
|
||||
10
moto/organizations/urls.py
Normal file
10
moto/organizations/urls.py
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
from __future__ import unicode_literals
|
||||
from .responses import OrganizationsResponse
|
||||
|
||||
url_bases = [
|
||||
"https?://organizations.(.+).amazonaws.com",
|
||||
]
|
||||
|
||||
url_paths = {
|
||||
'{0}/$': OrganizationsResponse.dispatch,
|
||||
}
|
||||
34
moto/organizations/utils.py
Normal file
34
moto/organizations/utils.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import random
|
||||
import string
|
||||
|
||||
CHARSET=string.ascii_lowercase + string.digits
|
||||
ORG_ID_SIZE = 10
|
||||
ROOT_ID_SIZE = 4
|
||||
ACCOUNT_ID_SIZE = 12
|
||||
CREATE_ACCOUNT_STATUS_ID_SIZE = 8
|
||||
|
||||
|
||||
def make_random_org_id():
|
||||
# The regex pattern for an organization ID string requires "o-"
|
||||
# followed by from 10 to 32 lower-case letters or digits.
|
||||
# e.g. 'o-vipjnq5z86'
|
||||
return 'o-' + ''.join(random.choice(CHARSET) for x in range(ORG_ID_SIZE))
|
||||
|
||||
def make_random_root_id():
|
||||
# The regex pattern for a root ID string requires "r-" followed by
|
||||
# from 4 to 32 lower-case letters or digits.
|
||||
# e.g. 'r-3zwx'
|
||||
return 'r-' + ''.join(random.choice(CHARSET) for x in range(ROOT_ID_SIZE))
|
||||
|
||||
def make_random_account_id():
|
||||
# The regex pattern for an account ID string requires exactly 12 digits.
|
||||
# e.g. '488633172133'
|
||||
return ''.join([random.choice(string.digits) for n in range(ACCOUNT_ID_SIZE)])
|
||||
|
||||
def make_random_create_account_status_id():
|
||||
# The regex pattern for an create account request ID string requires
|
||||
# "car-" followed by from 8 to 32 lower-case letters or digits.
|
||||
# e.g. 'car-35gxzwrp'
|
||||
return 'car-' + ''.join(random.choice(CHARSET) for x in range(CREATE_ACCOUNT_STATUS_ID_SIZE))
|
||||
Loading…
Add table
Add a link
Reference in a new issue