diff --git a/lib/fog/aws/models/iam/role.rb b/lib/fog/aws/models/iam/role.rb index ee093d052..22399fa22 100644 --- a/lib/fog/aws/models/iam/role.rb +++ b/lib/fog/aws/models/iam/role.rb @@ -50,6 +50,11 @@ module Fog service.managed_policies(:role_name => self.rolename) end + def instance_profiles + requires :rolename + service.instance_profiles.load(service.list_instance_profiles_for_role(self.rolename).body["InstanceProfiles"]) + end + def destroy requires :rolename diff --git a/lib/fog/aws/requests/iam/list_instance_profiles.rb b/lib/fog/aws/requests/iam/list_instance_profiles.rb index 95d876cfc..af4e53eaf 100644 --- a/lib/fog/aws/requests/iam/list_instance_profiles.rb +++ b/lib/fog/aws/requests/iam/list_instance_profiles.rb @@ -43,6 +43,15 @@ module Fog }.merge!(options)) end end + + class Mock + def list_instance_profiles(options={}) + response = Excon::Response.new + profiles = self.data[:instance_profiles].values + response.body = { "InstanceProfiles" => profiles, "IsTruncated" => false, "RequestId" => Fog::AWS::Mock.request_id } + response + end + end end end end diff --git a/lib/fog/aws/requests/iam/list_instance_profiles_for_role.rb b/lib/fog/aws/requests/iam/list_instance_profiles_for_role.rb index 45a8af69b..67c1233a4 100644 --- a/lib/fog/aws/requests/iam/list_instance_profiles_for_role.rb +++ b/lib/fog/aws/requests/iam/list_instance_profiles_for_role.rb @@ -44,6 +44,16 @@ module Fog }.merge!(options)) end end + + class Mock + def list_instance_profiles_for_role(role_name, options={}) + response = Excon::Response.new + + profiles = self.data[:instance_profiles].values.select { |p| p["Roles"].include?(role_name) } + response.body = { "InstanceProfiles" => profiles, "IsTruncated" => false, "RequestId" => Fog::AWS::Mock.request_id } + response + end + end end end end diff --git a/tests/models/iam/instance_profile_tests.rb b/tests/models/iam/instance_profile_tests.rb index 1fb30f126..497dad648 100644 --- a/tests/models/iam/instance_profile_tests.rb +++ b/tests/models/iam/instance_profile_tests.rb @@ -6,6 +6,9 @@ Shindo.tests("Fog::AWS[:iam] | instance_profiles", ['aws', 'iam']) do returns(true) { @instance.add_role(@role.rolename) } end + returns(1) { @role.instance_profiles.count } + returns(@instance) { @role.instance_profiles.first } + tests("#remove_role('#{@role.rolename}')") do returns(true) { @instance.remove_role(@role.rolename) } end diff --git a/tests/requests/iam/helper.rb b/tests/requests/iam/helper.rb index 51c636d8a..625f40634 100644 --- a/tests/requests/iam/helper.rb +++ b/tests/requests/iam/helper.rb @@ -144,6 +144,11 @@ Xb9WSr07saxZQbxBPQyTlb0Q9Tu2djAq2/o/nYD1/50/fXUTuWMB INSTANCE_PROFILE_RESULT = BASIC.merge( 'InstanceProfile' => INSTANCE_PROFILE ) + + LIST_INSTANCE_PROFILE_RESULT = BASIC.merge( + "IsTruncated" => Fog::Boolean, + "InstanceProfiles" => [INSTANCE_PROFILE] + ) end end end diff --git a/tests/requests/iam/instance_profile_tests.rb b/tests/requests/iam/instance_profile_tests.rb index b5d7f1cee..fc7fc4e33 100644 --- a/tests/requests/iam/instance_profile_tests.rb +++ b/tests/requests/iam/instance_profile_tests.rb @@ -3,10 +3,18 @@ include AWS::IAM::Formats Shindo.tests("AWS::IAM | instance profile requests", ['aws']) do tests('success') do profile_name = uniq_id('fog-instance-profile') + @instance_profile_count = Fog::AWS[:iam].list_instance_profiles.body["InstanceProfiles"].count + tests("#create_instance_profile('#{profile_name}')").formats(INSTANCE_PROFILE_RESULT) do Fog::AWS[:iam].create_instance_profile(profile_name).body end + tests("#list_instance_profiles").formats(LIST_INSTANCE_PROFILE_RESULT) do + body = Fog::AWS[:iam].list_instance_profiles.body + returns(@instance_profile_count + 1) { body["InstanceProfiles"].count } + body + end + tests("#get_instance_profile('#{profile_name}')").formats(INSTANCE_PROFILE_RESULT) do Fog::AWS[:iam].get_instance_profile(profile_name).body end @@ -17,6 +25,12 @@ Shindo.tests("AWS::IAM | instance profile requests", ['aws']) do Fog::AWS[:iam].add_role_to_instance_profile(@role.rolename, profile_name).body end + tests("#list_instance_profiles_for_role('#{@role.rolename}')").formats(LIST_INSTANCE_PROFILE_RESULT) do + body = Fog::AWS[:iam].list_instance_profiles_for_role(@role.rolename).body + returns(1) { body["InstanceProfiles"].count } + body + end + tests("#remove_role_from_instance_profile('#{@role.rolename}', '#{profile_name}')").formats(BASIC) do Fog::AWS[:iam].remove_role_from_instance_profile(@role.rolename, profile_name).body end