Support IAM account alias (#1088)

This commit is contained in:
Toshiya Kawasaki 2017-09-08 03:21:44 +09:00 committed by Jack Danger
parent 0c3708a8e7
commit ee4ca0c39a
3 changed files with 77 additions and 0 deletions

View File

@ -507,6 +507,7 @@ class IAMBackend(BaseBackend):
self.users = {}
self.credential_report = None
self.managed_policies = self._init_managed_policies()
self.account_aliases = []
super(IAMBackend, self).__init__()
def _init_managed_policies(self):
@ -957,5 +958,15 @@ class IAMBackend(BaseBackend):
report += self.users[user].to_csv()
return base64.b64encode(report.encode('ascii')).decode('ascii')
def list_account_aliases(self):
return self.account_aliases
def create_account_alias(self, alias):
# alias is force updated
self.account_aliases = [alias]
def delete_account_alias(self, alias):
self.account_aliases = []
iam_backend = IAMBackend()

View File

@ -478,6 +478,23 @@ class IamResponse(BaseResponse):
template = self.response_template(CREDENTIAL_REPORT)
return template.render(report=report)
def list_account_aliases(self):
aliases = iam_backend.list_account_aliases()
template = self.response_template(LIST_ACCOUNT_ALIASES_TEMPLATE)
return template.render(aliases=aliases)
def create_account_alias(self):
alias = self._get_param('AccountAlias')
iam_backend.create_account_alias(alias)
template = self.response_template(CREATE_ACCOUNT_ALIAS_TEMPLATE)
return template.render()
def delete_account_alias(self):
alias = self._get_param('AccountAlias')
iam_backend.delete_account_alias(alias)
template = self.response_template(DELETE_ACCOUNT_ALIAS_TEMPLATE)
return template.render()
ATTACH_ROLE_POLICY_TEMPLATE = """<AttachRolePolicyResponse>
<ResponseMetadata>
@ -1192,3 +1209,32 @@ LIST_MFA_DEVICES_TEMPLATE = """<ListMFADevicesResponse>
<RequestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestId>
</ResponseMetadata>
</ListMFADevicesResponse>"""
LIST_ACCOUNT_ALIASES_TEMPLATE = """<ListAccountAliasesResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
<ListAccountAliasesResult>
<IsTruncated>false</IsTruncated>
<AccountAliases>
{% for alias in aliases %}
<member>{{ alias }}</member>
{% endfor %}
</AccountAliases>
</ListAccountAliasesResult>
<ResponseMetadata>
<RequestId>c5a076e9-f1b0-11df-8fbe-45274EXAMPLE</RequestId>
</ResponseMetadata>
</ListAccountAliasesResponse>"""
CREATE_ACCOUNT_ALIAS_TEMPLATE = """<CreateAccountAliasResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
<ResponseMetadata>
<RequestId>36b5db08-f1b0-11df-8fbe-45274EXAMPLE</RequestId>
</ResponseMetadata>
</CreateAccountAliasResponse>"""
DELETE_ACCOUNT_ALIAS_TEMPLATE = """<DeleteAccountAliasResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
<ResponseMetadata>
<RequestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</RequestId>
</ResponseMetadata>
</DeleteAccountAliasResponse>"""

View File

@ -0,0 +1,20 @@
import boto3
import sure # noqa
from moto import mock_iam
@mock_iam()
def test_account_aliases():
client = boto3.client('iam', region_name='us-east-1')
alias = 'my-account-name'
aliases = client.list_account_aliases()
aliases.should.have.key('AccountAliases').which.should.equal([])
client.create_account_alias(AccountAlias=alias)
aliases = client.list_account_aliases()
aliases.should.have.key('AccountAliases').which.should.equal([alias])
client.delete_account_alias(AccountAlias=alias)
aliases = client.list_account_aliases()
aliases.should.have.key('AccountAliases').which.should.equal([])