lib/fog/aws/iam.rb in fog-0.9.0 vs lib/fog/aws/iam.rb in fog-0.10.0

- old
+ new

@@ -1,19 +1,27 @@ module Fog module AWS class IAM < Fog::Service + class EntityAlreadyExists < Fog::AWS::IAM::Error; end + class KeyPairMismatch < Fog::AWS::IAM::Error; end + class LimitExceeded < Fog::AWS::IAM::Error; end + class MalformedCertificate < Fog::AWS::IAM::Error; end + class ValidationError < Fog::AWS::IAM::Error; end + requires :aws_access_key_id, :aws_secret_access_key recognizes :host, :path, :port, :scheme, :persistent request_path 'fog/aws/requests/iam' request :add_user_to_group request :create_access_key + request :create_account_alias request :create_group request :create_user request :create_login_profile request :delete_access_key + request :delete_account_alias request :delete_group request :delete_group_policy request :delete_login_profile request :delete_server_certificate request :delete_signing_certificate @@ -21,11 +29,14 @@ request :delete_user_policy request :get_login_profile request :get_user request :get_user_policy request :get_group + request :get_group_policy + request :get_server_certificate request :list_access_keys + request :list_account_aliases request :list_groups request :list_groups_for_user request :list_group_policies request :list_server_certificates request :list_signing_certificates @@ -41,15 +52,38 @@ request :update_signing_certificate request :upload_server_certificate request :upload_signing_certificate class Mock + def self.data + @data ||= Hash.new do |hash, key| + hash[key] = { + :owner_id => Fog::AWS::Mock.owner_id, + :server_certificates => {} + } + end + end + def self.reset + @data = nil + end + + def self.server_certificate_id + Fog::Mock.random_hex(16) + end + def initialize(options={}) - Fog::Mock.not_implemented + @aws_access_key_id = options[:aws_access_key_id] end + def data + self.class.data[@aws_access_key_id] + end + + def reset_data + self.class.data.delete(@aws_access_key_id) + end end class Real # Initialize connection to IAM @@ -69,11 +103,11 @@ # # ==== Returns # * IAM object with connection to AWS. def initialize(options={}) require 'fog/core/parser' - require 'json' + require 'multi_json' @aws_access_key_id = options[:aws_access_key_id] @aws_secret_access_key = options[:aws_secret_access_key] @hmac = Fog::HMAC.new('sha256', @aws_secret_access_key) @host = options[:host] || 'iam.amazonaws.com' @@ -103,20 +137,38 @@ :port => @port, :version => '2010-05-08' } ) - response = @connection.request({ - :body => body, - :expects => 200, - :idempotent => idempotent, - :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, - :host => @host, - :method => 'POST', - :parser => parser - }) + begin + response = @connection.request({ + :body => body, + :expects => 200, + :idempotent => idempotent, + :headers => { 'Content-Type' => 'application/x-www-form-urlencoded' }, + :host => @host, + :method => 'POST', + :parser => parser + }) - response + response + rescue Excon::Errors::HTTPStatusError => error + if match = error.message.match(/<Code>(.*)<\/Code>(?:.*<Message>(.*)<\/Message>)?/m) + case match[1] + when 'CertificateNotFound', 'NoSuchEntity' + raise Fog::AWS::IAM::NotFound.slurp(error, match[2]) + when 'EntityAlreadyExists', 'KeyPairMismatch', 'LimitExceeded', 'MalformedCertificate', 'ValidationError' + raise Fog::AWS::IAM.const_get(match[1]).slurp(error, match[2]) + else + raise Fog::AWS::IAM::Error.slurp(error, "#{match[1]} => #{match[2]}") if match[1] + raise + end + else + raise + end + end + + end end end end