diff --git a/moto/iam/models.py b/moto/iam/models.py index c7ee70ca2..da11d58b2 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -569,6 +569,13 @@ class IAMBackend(BaseBackend): return role raise IAMNotFoundException("Role {0} not found".format(role_name)) + def delete_role(self, role_name): + for role in self.get_roles(): + if role.name == role_name: + del self.roles[role.id] + return + raise IAMNotFoundException("Role {0} not found".format(role_name)) + def get_roles(self): return self.roles.values() diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 3c40a323f..138c08d23 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -62,6 +62,12 @@ class IamResponse(BaseResponse): template = self.response_template(GET_ROLE_TEMPLATE) return template.render(role=role) + def delete_role(self): + role_name = self._get_param('RoleName') + iam_backend.delete_role(role_name) + template = self.response_template(GENERIC_EMPTY_TEMPLATE) + return template.render(name="DeleteRoleResponse") + def list_role_policies(self): role_name = self._get_param('RoleName') role_policies_names = iam_backend.list_role_policies(role_name) diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index f2c77685f..46b727360 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -8,7 +8,7 @@ from boto.exception import BotoServerError from botocore.exceptions import ClientError from moto import mock_iam, mock_iam_deprecated from moto.iam.models import aws_managed_policies -from nose.tools import assert_raises, assert_equals, assert_not_equals +from nose.tools import assert_raises, assert_equals from nose.tools import raises from tests.helpers import requires_boto_gte @@ -114,6 +114,23 @@ def test_remove_role_from_instance_profile(): dict(profile.roles).should.be.empty +@mock_iam() +def test_delete_role(): + conn = boto3.client('iam', region_name='us-east-1') + + with assert_raises(ClientError): + conn.delete_role(RoleName="my-role") + + conn.create_role(RoleName="my-role", AssumeRolePolicyDocument="some policy", Path="/my-path/") + role = conn.get_role(RoleName="my-role") + role.get('Role').get('Arn').should.equal('arn:aws:iam::123456789012:role/my-path/my-role') + + conn.delete_role(RoleName="my-role") + + with assert_raises(ClientError): + conn.get_role(RoleName="my-role") + + @mock_iam_deprecated() def test_list_instance_profiles(): conn = boto.connect_iam()