organizations: add endpoint list_roots

This commit is contained in:
Ashley Gould 2018-07-15 10:31:16 -07:00
commit 6c0c6148f1
6 changed files with 122 additions and 8 deletions

View file

@ -11,7 +11,7 @@ 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}'
ROOT_ARN_FORMAT = 'arn:aws:organizations::{0}:root/{1}/{2}'
class FakeOrganization(BaseModel):
@ -33,7 +33,7 @@ class FakeOrganization(BaseModel):
def master_account_arn(self):
return MASTER_ACCOUNT_ARN_FORMAT.format(self.master_account_id, self.id)
def _describe(self):
def describe(self):
return {
'Organization': {
'Id': self.id,
@ -95,18 +95,80 @@ class FakeAccount(BaseModel):
}
class FakeOrganizationalUnit(BaseModel):
def __init__(self, organization, **kwargs):
self.organization_id = organization.id
self.master_account_id = organization.master_account_id
self.id = utils.make_random_ou_id()
self.name = kwargs['Name']
@property
def arn(self):
return OU_ARN_FORMAT.format(
self.master_account_id,
self.organization_id,
self.id
)
def describe(self):
return {
'OrganizationalUnit': {
'Id': self.id,
'Arn': self.arn,
'Name': self.name,
}
}
class FakeRoot(BaseModel):
def __init__(self, organization, **kwargs):
self.organization_id = organization.id
self.master_account_id = organization.master_account_id
self.id = utils.make_random_root_id()
self.name = 'Root'
self.policy_types = [{
'Type': 'SERVICE_CONTROL_POLICY',
'Status': 'ENABLED'
}]
@property
def arn(self):
return ROOT_ARN_FORMAT.format(
self.master_account_id,
self.organization_id,
self.id
)
def describe(self):
return {
'Id': self.id,
'Arn': self.arn,
'Name': self.name,
'PolicyTypes': self.policy_types
}
class OrganizationsBackend(BaseBackend):
def __init__(self):
self.org = None
self.accounts = []
self.roots = []
def create_organization(self, **kwargs):
self.org = FakeOrganization(kwargs['FeatureSet'])
return self.org._describe()
self.roots.append(FakeRoot(self.org))
return self.org.describe()
def describe_organization(self):
return self.org._describe()
return self.org.describe()
def list_roots(self):
return dict(
Roots=[root.describe() for root in self.roots]
)
def create_account(self, **kwargs):
new_account = FakeAccount(self.org, **kwargs)

View file

@ -31,6 +31,11 @@ class OrganizationsResponse(BaseResponse):
self.organizations_backend.describe_organization()
)
def list_roots(self):
return json.dumps(
self.organizations_backend.list_roots()
)
def create_account(self):
return json.dumps(
self.organizations_backend.create_account(**self.request_params)

View file

@ -7,6 +7,7 @@ CHARSET = string.ascii_lowercase + string.digits
ORG_ID_SIZE = 10
ROOT_ID_SIZE = 4
ACCOUNT_ID_SIZE = 12
OU_ID_SUFFIX_SIZE = 8
CREATE_ACCOUNT_STATUS_ID_SIZE = 8
@ -24,6 +25,19 @@ def make_random_root_id():
return 'r-' + ''.join(random.choice(CHARSET) for x in range(ROOT_ID_SIZE))
def make_random_ou_id(root_id):
# The regex pattern for an organizational unit ID string requires "ou-"
# followed by from 4 to 32 lower-case letters or digits (the ID of the root
# that contains the OU) followed by a second "-" dash and from 8 to 32
# additional lower-case letters or digits.
# e.g. ou-g8sd-5oe3bjaw
return '-'.join([
'ou',
root_id.partition('-')[2],
''.join(random.choice(CHARSET) for x in range(OU_ID_SUFFIX_SIZE)),
])
def make_random_account_id():
# The regex pattern for an account ID string requires exactly 12 digits.
# e.g. '488633172133'