diff --git a/moto/iam/models.py b/moto/iam/models.py index 05e440954..0f95d6453 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -299,6 +299,11 @@ class IAMBackend(BaseBackend): role = self.get_role(role_name) profile.roles.append(role) + def remove_role_from_instance_profile(self, profile_name, role_name): + profile = self.get_instance_profile(profile_name) + role = self.get_role(role_name) + profile.roles.remove(role) + def get_all_server_certs(self, marker=None): return self.certificates.values() diff --git a/moto/iam/responses.py b/moto/iam/responses.py index a225eccd6..6895aa45a 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -75,6 +75,14 @@ class IamResponse(BaseResponse): template = self.response_template(ADD_ROLE_TO_INSTANCE_PROFILE_TEMPLATE) return template.render() + def remove_role_from_instance_profile(self): + profile_name = self._get_param('InstanceProfileName') + role_name = self._get_param('RoleName') + + iam_backend.remove_role_from_instance_profile(profile_name, role_name) + template = self.response_template(REMOVE_ROLE_FROM_INSTANCE_PROFILE_TEMPLATE) + return template.render() + def list_roles(self): roles = iam_backend.get_roles() @@ -349,6 +357,12 @@ ADD_ROLE_TO_INSTANCE_PROFILE_TEMPLATE = """ """ +REMOVE_ROLE_FROM_INSTANCE_PROFILE_TEMPLATE = """ + + 12657608-99f2-11e1-a4c3-27EXAMPLE804 + +""" + LIST_ROLES_TEMPLATE = """ false diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index bb96b0569..39f16e836 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -40,6 +40,7 @@ def test_upload_server_cert(): cert.server_certificate_name.should.equal("certname") cert.arn.should.equal("arn:aws:iam::123456789012:server-certificate/certname") + @mock_iam() @raises(BotoServerError) def test_get_role__should_throw__when_role_does_not_exist(): @@ -47,6 +48,7 @@ def test_get_role__should_throw__when_role_does_not_exist(): conn.get_role('unexisting_role') + @mock_iam() def test_create_role_and_instance_profile(): conn = boto.connect_iam() @@ -68,6 +70,23 @@ def test_create_role_and_instance_profile(): conn.list_roles().roles[0].role_name.should.equal('my-role') +@mock_iam() +def test_remove_role_from_instance_profile(): + conn = boto.connect_iam() + conn.create_instance_profile("my-profile", path="my-path") + conn.create_role("my-role", assume_role_policy_document="some policy", path="my-path") + conn.add_role_to_instance_profile("my-profile", "my-role") + + profile = conn.get_instance_profile("my-profile") + role_from_profile = list(profile.roles.values())[0] + role_from_profile['role_name'].should.equal("my-role") + + conn.remove_role_from_instance_profile("my-profile", "my-role") + + profile = conn.get_instance_profile("my-profile") + dict(profile.roles).should.be.empty + + @mock_iam() def test_list_instance_profiles(): conn = boto.connect_iam()