diff --git a/lib/fog/openstack/identity.rb b/lib/fog/openstack/identity.rb index ab8a73de4..61cfbf07e 100644 --- a/lib/fog/openstack/identity.rb +++ b/lib/fog/openstack/identity.rb @@ -53,6 +53,10 @@ module Fog request :set_tenant + request :create_ec2_credential + request :delete_ec2_credential + request :get_ec2_credential + request :list_ec2_credentials class Mock attr_reader :auth_token @@ -63,24 +67,27 @@ module Fog attr_reader :unscoped_token def self.data - @users ||= {} - @roles ||= {} - @tenants ||= {} + @users ||= {} + @roles ||= {} + @tenants ||= {} + @ec2_credentials ||= Hash.new { |hash, key| hash[key] = {} } @data ||= Hash.new do |hash, key| hash[key] = { - :users => @users, - :roles => @roles, - :tenants => @tenants + :users => @users, + :roles => @roles, + :tenants => @tenants, + :ec2_credentials => @ec2_credentials, } end end def self.reset! - @data = nil - @users = nil - @roles = nil - @tenants = nil + @data = nil + @users = nil + @roles = nil + @tenants = nil + @ec2_credentials = nil end def initialize(options={}) diff --git a/lib/fog/openstack/requests/identity/create_ec2_credential.rb b/lib/fog/openstack/requests/identity/create_ec2_credential.rb new file mode 100644 index 000000000..952a007fa --- /dev/null +++ b/lib/fog/openstack/requests/identity/create_ec2_credential.rb @@ -0,0 +1,38 @@ +module Fog + module Identity + class OpenStack + class Real + def create_ec2_credential(user_id, tenant_id) + data = { 'tenant_id' => tenant_id } + + request( + :body => MultiJson.encode(data), + :expects => [200, 202], + :method => 'POST', + :path => "/users/#{user_id}/credentials/OS-EC2", + ) + end + end + + class Mock + def create_ec2_credential(user_id, tenant_id) + response = Excon::Response.new + response.status = 200 + + data = { + 'access' => Fog::Mock.random_hex(32), + 'secret' => Fog::Mock.random_hex(32), + 'tenant_id' => tenant_id, + 'user_id' => user_id, + } + + self.data[:ec2_credentials][user_id][data['access']] = data + + response.body = { 'credential' => data } + + response + end + end + end + end +end diff --git a/lib/fog/openstack/requests/identity/delete_ec2_credential.rb b/lib/fog/openstack/requests/identity/delete_ec2_credential.rb new file mode 100644 index 000000000..d177b585c --- /dev/null +++ b/lib/fog/openstack/requests/identity/delete_ec2_credential.rb @@ -0,0 +1,30 @@ +module Fog + module Identity + class OpenStack + class Real + def delete_ec2_credential(user_id, access) + request( + :expects => [200, 204], + :method => 'DELETE', + :path => "/users/#{user_id}/credentials/OS-EC2/#{access}", + ) + end + end + + class Mock + def delete_ec2_credential(user_id, access) + raise Fog::Identity::OpenStack::NotFound unless + self.data[:ec2_credentials][user_id][access] + + self.data[:ec2_credentials][user_id].delete access + + response = Excon::Response.new + response.status = 204 + response + rescue + end + end + end + end +end + diff --git a/lib/fog/openstack/requests/identity/get_ec2_credential.rb b/lib/fog/openstack/requests/identity/get_ec2_credential.rb new file mode 100644 index 000000000..e222318aa --- /dev/null +++ b/lib/fog/openstack/requests/identity/get_ec2_credential.rb @@ -0,0 +1,28 @@ +module Fog + module Identity + class OpenStack + class Real + def get_ec2_credential(user_id, access) + request( + :expects => [200, 202], + :method => 'GET', + :path => "/users/#{user_id}/credentials/OS-EC2/#{access}", + ) + end + end + + class Mock + def get_ec2_credential(user_id, access) + ec2_credential = self.data[:ec2_credentials][user_id][access] + + raise Fog::OpenStack::Identity::NotFound unless ec2_credential + + response = Excon::Response.new + response.status = 200 + response.body = { 'credential' => ec2_credential } + response + end + end + end + end +end diff --git a/lib/fog/openstack/requests/identity/list_ec2_credentials.rb b/lib/fog/openstack/requests/identity/list_ec2_credentials.rb new file mode 100644 index 000000000..e42f3bc47 --- /dev/null +++ b/lib/fog/openstack/requests/identity/list_ec2_credentials.rb @@ -0,0 +1,26 @@ +module Fog + module Identity + class OpenStack + class Real + def list_ec2_credentials(user_id) + request( + :expects => [200, 202], + :method => 'GET', + :path => "/users/#{user_id}/credentials/OS-EC2", + ) + end + end + + class Mock + def list_ec2_credentials(user_id) + ec2_credentials = self.data[:ec2_credentials][user_id].values + + response = Excon::Response.new + response.status = 200 + response.body = { 'credentials' => ec2_credentials } + response + end + end + end + end +end diff --git a/tests/openstack/requests/identity/ec2_credentials_tests.rb b/tests/openstack/requests/identity/ec2_credentials_tests.rb new file mode 100644 index 000000000..8ccd325a3 --- /dev/null +++ b/tests/openstack/requests/identity/ec2_credentials_tests.rb @@ -0,0 +1,41 @@ +Shindo.tests('Fog::Identity[:openstack] | EC2 credential requests', ['openstack']) do + + @credential_format = { + 'access' => String, + 'tenant_id' => String, + 'secret' => String, + 'user_id' => String, + } + + tests('success') do + tests('#create_ec2_credential'). + formats({'credential' => @credential_format}) do + response = + Fog::Identity[:openstack]. + create_ec2_credential('user_id', 'tenant_id') + + @ec2_credential = response.body['credential'] + + response.body + end + + tests('#get_ec2_credential'). + formats({'credential' => @credential_format}) do + Fog::Identity[:openstack]. + get_ec2_credential('user_id', @ec2_credential['access']).body + end + + tests('#list_ec2_credentials'). + formats({'credentials' => [@credential_format]}) do + Fog::Identity[:openstack]. + list_ec2_credentials('user_id').body + end + + tests('#delete_ec2_credential').succeeds do + Fog::Identity[:openstack]. + delete_ec2_credential('user_id', @ec2_credential['access']) + end + + end + +end