diff --git a/lib/fog/aws/iam.rb b/lib/fog/aws/iam.rb index c5004e7ce..8814d5846 100644 --- a/lib/fog/aws/iam.rb +++ b/lib/fog/aws/iam.rb @@ -60,7 +60,9 @@ module Fog model :user collection :users model :policy - collection :policies + collection :policies + model :access_key + collection :access_keys class Mock diff --git a/lib/fog/aws/models/iam/access_key.rb b/lib/fog/aws/models/iam/access_key.rb new file mode 100644 index 000000000..96ae35bbb --- /dev/null +++ b/lib/fog/aws/models/iam/access_key.rb @@ -0,0 +1,38 @@ +require 'fog/core/model' + +module Fog + module AWS + class IAM + + class AccessKey < Fog::Model + + identity :id, :aliases => 'AccessKeyId' + attribute :username, :aliases => 'UserName' + attribute :secret_access_key, :aliases => 'SecretAccessKey' + attribute :status, :aliases => 'Status' + + def save + requires :username + + data = connection.create_access_key('UserName'=> username).body["AccessKey"] + merge_attributes(data) + true + end + + def destroy + requires :id + requires :username + + connection.delete_access_key(id,'UserName'=> username) + true + end + + def user + requires :username + connection.users.get(username) + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/iam/access_keys.rb b/lib/fog/aws/models/iam/access_keys.rb new file mode 100644 index 000000000..8cab07ae9 --- /dev/null +++ b/lib/fog/aws/models/iam/access_keys.rb @@ -0,0 +1,48 @@ +require 'fog/core/collection' +require 'fog/aws/models/iam/access_key' + +module Fog + module AWS + class IAM + + class AccessKeys < Fog::Collection + attribute :user + attribute :filters + + + model Fog::AWS::IAM::AccessKey + + def initialize(attributes) + self.filters ||= {} + if attributes[:user] + filters[:identifier] = attributes[:user].id + else + raise ArgumentError.new("Can't get a user's access_key without a user.id") + end + super + end + + + def all + data = connection.list_access_keys('UserName'=> filters[:identifier]).body['AccessKeys'] + # AWS response doesn't contain the UserName, this injects it + data.each {|access_key| access_key['UserName'] = filters[:identifier] } + load(data) + end + + def get(identity) + self.all.select {|access_key| access_key.id == identity}.first + end + + def new(attributes = {}) + if user + super({ :username => user.id }.merge!(attributes)) + else + super + end + end + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/aws/models/iam/policy.rb b/lib/fog/aws/models/iam/policy.rb index 4a83f09c9..666d57629 100644 --- a/lib/fog/aws/models/iam/policy.rb +++ b/lib/fog/aws/models/iam/policy.rb @@ -20,21 +20,29 @@ module Fog true end + def destroy + requires :id + requires :username + + connection.delete_user_policy(username, id) + true + end + def user requires :username connection.users.get(username) end # Converts attributes to a parameter hash suitable for requests - def attributes_to_params - options = { - 'PolicyName' => id, - 'UserName' => username, - 'PolicyDocument' => document - } - - options.delete_if {|key, value| value.nil?} - end +# def attributes_to_params +# options = { +# 'PolicyName' => id, +# 'UserName' => username, +# 'PolicyDocument' => document +# } +# +# options.delete_if {|key, value| value.nil?} +# end end end diff --git a/lib/fog/aws/models/iam/user.rb b/lib/fog/aws/models/iam/user.rb index 8bf9a7bd7..0fee23f22 100644 --- a/lib/fog/aws/models/iam/user.rb +++ b/lib/fog/aws/models/iam/user.rb @@ -30,6 +30,10 @@ module Fog connection.policies(:user => self) end + def access_keys + requires :id + connection.access_keys(:user => self) + end # # Converts attributes to a parameter hash suitable for requests # def attributes_to_params # options = {