From 3a357c0fe31f26e9a5d36280ab4f9469a958c97a Mon Sep 17 00:00:00 2001 From: Mike Grima Date: Thu, 26 Feb 2015 16:08:54 -0800 Subject: [PATCH] Added in test for the boto IAM method: list_instance_profiles_for_role() --- moto/core/responses.py | 5 ++++- moto/iam/models.py | 10 ++++++++++ moto/iam/responses.py | 40 +++++++++++++++++++++++++++++++++++++- tests/test_iam/test_iam.py | 28 ++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/moto/core/responses.py b/moto/core/responses.py index a99ac78c8..5558e9a02 100644 --- a/moto/core/responses.py +++ b/moto/core/responses.py @@ -137,7 +137,10 @@ class BaseResponse(_TemplateEnvironmentMixin): if action in method_names: method = getattr(self, action) try: - response = method() + if action != 'list_instance_profiles_for_role': + response = method() + else: + response = method(role_name=self.querystring['RoleName'][0]) except HTTPException as http_error: response = http_error.description, dict(status=http_error.code) if isinstance(response, six.string_types): diff --git a/moto/iam/models.py b/moto/iam/models.py index 5468e0805..388984f51 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -291,6 +291,16 @@ class IAMBackend(BaseBackend): def get_instance_profiles(self): return self.instance_profiles.values() + def get_instance_profiles_for_role(self, role_name): + found_profiles = [] + + for profile in self.get_instance_profiles(): + if len(profile.roles) > 0: + if profile.roles[0].name == role_name: + found_profiles.append(profile) + + return found_profiles + def add_role_to_instance_profile(self, profile_name, role_name): profile = self.get_instance_profile(profile_name) role = self.get_role(role_name) diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 78030f288..e1722162b 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -87,6 +87,12 @@ class IamResponse(BaseResponse): template = self.response_template(LIST_INSTANCE_PROFILES_TEMPLATE) return template.render(instance_profiles=profiles) + def list_instance_profiles_for_role(self, role_name=None): + profiles = iam_backend.get_instance_profiles_for_role(role_name=role_name) + + template = self.response_template(LIST_INSTANCE_PROFILES_FOR_ROLE_TEMPLATE) + return template.render(instance_profiles=profiles) + def upload_server_certificate(self): cert_name = self._get_param('ServerCertificateName') cert_body = self._get_param('CertificateBody') @@ -601,4 +607,36 @@ CREDENTIAL_REPORT = """ fa788a82-aa8a-11e4-a278-1786c418872b" -""" \ No newline at end of file +""" + +LIST_INSTANCE_PROFILES_FOR_ROLE_TEMPLATE = """ + + false + + {% for profile in instance_profiles %} + + {{ profile.id }} + + {% for role in profile.roles %} + + {{ role.path }} + arn:aws:iam::123456789012:role{{ role.path }}S3Access + {{ role.name }} + {{ role.assume_policy_document }} + 2012-05-09T15:45:35Z + {{ role.id }} + + {% endfor %} + + {{ profile.name }} + {{ profile.path }} + arn:aws:iam::123456789012:instance-profile{{ profile.path }}Webserver + 2012-05-09T16:27:11Z + + {% endfor %} + + + + 6a8c3992-99f4-11e1-a4c3-27EXAMPLE804 + +""" \ No newline at end of file diff --git a/tests/test_iam/test_iam.py b/tests/test_iam/test_iam.py index 454703fec..ee77297b0 100644 --- a/tests/test_iam/test_iam.py +++ b/tests/test_iam/test_iam.py @@ -62,6 +62,34 @@ def test_create_role_and_instance_profile(): conn.list_roles().roles[0].role_name.should.equal('my-role') conn.list_instance_profiles().instance_profiles[0].instance_profile_name.should.equal("my-profile") +@mock_iam() +def test_list_instance_profiles_for_role(): + conn = boto.connect_iam() + + conn.create_role(role_name="my-role", assume_role_policy_document="some policy", path="my-path") + conn.create_role(role_name="my-role2", assume_role_policy_document="some policy2", path="my-path2") + + profile_name_list = ['my-profile', 'my-profile2'] + profile_path_list = ['my-path', 'my-path2'] + for profile_count in range(0,2): + conn.create_instance_profile(profile_name_list[profile_count], path=profile_path_list[profile_count]) + + for profile_count in range(0,2): + conn.add_role_to_instance_profile(profile_name_list[profile_count], "my-role") + + profile_dump = conn.list_instance_profiles_for_role(role_name="my-role") + profile_list = profile_dump['list_instance_profiles_for_role_response']['list_instance_profiles_for_role_result']['instance_profiles'] + for profile_count in range(0,len(profile_list)): + profile_name_list.remove(profile_list[profile_count]["instance_profile_name"]) + profile_path_list.remove(profile_list[profile_count]["path"]) + profile_list[profile_count]["roles"]["member"]["role_name"].should.equal("my-role") + + len(profile_name_list).should.equal(0) + len(profile_path_list).should.equal(0) + + profile_dump2 = conn.list_instance_profiles_for_role(role_name="my-role2") + profile_list = profile_dump2['list_instance_profiles_for_role_response']['list_instance_profiles_for_role_result']['instance_profiles'] + len(profile_list).should.equal(0) @mock_iam() def test_list_role_policies():