1
0
Fork 0
mirror of https://github.com/fog/fog-aws.git synced 2022-11-09 13:50:52 -05:00

paged collection with tests working

This commit is contained in:
Josh Lane 2015-09-01 14:31:13 -07:00
parent 039c497e41
commit edbec30380
5 changed files with 66 additions and 37 deletions

View file

@ -3,31 +3,25 @@ require 'fog/aws/models/iam/role'
module Fog
module AWS
class IAM
class Roles < Fog::Collection
class Roles < Fog::AWS::IAM::PagedCollection
model Fog::AWS::IAM::Role
def initialize(attributes = {})
super
end
def all
data = service.list_roles.body['Roles']
load(data)
load(service.list_roles.body['Roles'])
end
def get(identity)
role = nil
begin
role = service.roles.new( service.get_role( identity ).data[:body]["Role"] )
rescue Excon::Errors::NotFound, Fog::AWS::IAM::NotFound # ignore not found error
end
role
new(service.get_role(identity).body["Role"])
rescue Excon::Errors::NotFound, Fog::AWS::IAM::NotFound # ignore not found error
nil
end
def new(attributes = {})
if not attributes.key?(:assume_role_policy_document)
unless attributes.key?(:assume_role_policy_document)
attributes[:assume_role_policy_document] = Fog::AWS::IAM::EC2_ASSUME_ROLE_POLICY.to_s
end
super
end
end

View file

@ -63,7 +63,7 @@ module Fog
'Arn' => data[:roles][role_name][:arn].strip,
'AssumeRolePolicyDocument' => Fog::JSON.encode(data[:roles][role_name][:assume_role_policy_document]),
'CreateDate' => data[:roles][role_name][:create_date],
'Path' => path,
'Path' => path || "/",
'RoleId' => data[:roles][role_name][:role_id].strip,
'RoleName' => role_name,
},

View file

@ -34,9 +34,12 @@ module Fog
class Mock
def get_role(role_name)
role = self.data[:roles][role_name]
raise Fog::AWS::IAM::NotFound.new("The role with name #{role_name} cannot be found") unless role
unless self.data[:roles].key?(role_name)
raise Fog::AWS::IAM::NotFound.new("The role with name #{role_name} cannot be found")
end
role = self.data[:roles][role_name]
Excon::Response.new.tap do |response|
response.body = {

View file

@ -39,23 +39,57 @@ module Fog
class Mock
def list_roles(options={})
Excon::Response.new.tap do |response|
response.body = {
'Roles' => data[:roles].map do |role, data|
{
'Arn' => data[:arn].strip,
'AssumeRolePolicyDocument' => Fog::JSON.encode(data[:assume_role_policy_document]),
'RoleId' => data[:role_id],
'Path' => data[:path],
'RoleName' => role,
'CreateDate' => data[:create_date],
}
end,
'RequestId' => Fog::AWS::Mock.request_id,
'IsTruncated' => false,
}
response.status = 200
limit = options['MaxItems']
marker = options['Marker']
if limit
if limit > 1_000
raise Fog::AWS::IAM::Error.new(
"ValidationError => 1 validation error detected: Value '#{limit}' at 'limit' failed to satisfy constraint: Member must have value less than or equal to 1000"
)
elsif limit < 1
raise Fog::AWS::IAM::Error.new(
"ValidationError => 1 validation error detected: Value '#{limit}' at 'limit' failed to satisfy constraint: Member must have value greater than or equal to 1"
)
end
end
data_set = if marker
self.data[:markers][marker] || []
else
data[:roles].map { |role, data|
{
'Arn' => data[:arn].strip,
'AssumeRolePolicyDocument' => Fog::JSON.encode(data[:assume_role_policy_document]),
'RoleId' => data[:role_id],
'Path' => data[:path],
'RoleName' => role,
'CreateDate' => data[:create_date],
}
}
end
data = data_set.slice!(0, limit || 100)
truncated = data_set.size > 0
marker = truncated && Base64.encode64("metadata/l/#{account_id}/#{UUID.uuid}")
response = Excon::Response.new
body = {
'Roles' => data,
'IsTruncated' => truncated,
'RequestId' => Fog::AWS::Mock.request_id
}
if marker
self.data[:markers][marker] = data_set
body.merge!('Marker' => marker)
end
response.body = body
response.status = 200
response
end
end
end

View file

@ -1,14 +1,12 @@
Shindo.tests("Fog::Compute[:iam] | roles", ['aws','iam']) do
pending if Fog.mocking?
@iam = Fog::AWS[:iam]
@role_one_name = 'fake_role_one'
@role_two_name = 'fake_role_two'
@role_three_name = 'fake_role_three'
@role_three_path = '/path/to/fake_role_three/'
@role_four_name = 'fake_role_four'
@role_four_name = 'fake_role_four'
tests('#create').succeeds do
@role_one = @iam.roles.create(:rolename => @role_one_name)
@ -37,7 +35,7 @@ Shindo.tests("Fog::Compute[:iam] | roles", ['aws','iam']) do
end
tests('#get',"returns nil if the role doesn't exists").succeeds do
@iam.roles.get('non-exists') == nil
@iam.roles.get('blah').nil?
end
tests('#create', 'assigns path').succeeds do