From d27ec94147a42d2b1518379f83d948e0e81acc32 Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Wed, 17 Jul 2013 09:18:27 +0200 Subject: [PATCH 01/15] Rackspace: add keypair support --- lib/fog/aws/models/compute/key_pairs.rb | 5 ++- lib/fog/rackspace/compute_v2.rb | 7 ++++ lib/fog/rackspace/mock_data.rb | 2 +- .../rackspace/models/compute_v2/keypair.rb | 28 +++++++++++++++ .../rackspace/models/compute_v2/keypairs.rb | 31 ++++++++++++++++ lib/fog/rackspace/models/compute_v2/server.rb | 9 +++-- .../requests/compute_v2/create_keypair.rb | 36 +++++++++++++++++++ .../requests/compute_v2/create_server.rb | 15 ++++---- .../requests/compute_v2/delete_keypair.rb | 26 ++++++++++++++ .../requests/compute_v2/get_keypair.rb | 29 +++++++++++++++ .../requests/compute_v2/list_keypairs.rb | 26 ++++++++++++++ 11 files changed, 203 insertions(+), 11 deletions(-) create mode 100644 lib/fog/rackspace/models/compute_v2/keypair.rb create mode 100644 lib/fog/rackspace/models/compute_v2/keypairs.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/create_keypair.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/delete_keypair.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/get_keypair.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/list_keypairs.rb diff --git a/lib/fog/aws/models/compute/key_pairs.rb b/lib/fog/aws/models/compute/key_pairs.rb index 2e326606c..00875f5e6 100644 --- a/lib/fog/aws/models/compute/key_pairs.rb +++ b/lib/fog/aws/models/compute/key_pairs.rb @@ -56,7 +56,10 @@ module Fog end self.filters = filters data = service.describe_key_pairs(filters).body - load(data['keySet']) +puts "AWS KEY-PAIRS - DATA : #{data.inspect}" + r = load(data['keySet']) +puts "AWS KEY-PAIRS : #{r.inspect}" + r end # Used to retrieve a key pair that was created with the AWS.key_pairs.create method. diff --git a/lib/fog/rackspace/compute_v2.rb b/lib/fog/rackspace/compute_v2.rb index a86bba26f..79df7b6ef 100644 --- a/lib/fog/rackspace/compute_v2.rb +++ b/lib/fog/rackspace/compute_v2.rb @@ -54,6 +54,8 @@ module Fog collection :attachments model :network collection :networks + model :keypair + collection :keypairs request_path 'fog/rackspace/requests/compute_v2' request :list_servers @@ -97,6 +99,11 @@ module Fog request :create_network request :delete_network + request :list_keypairs + request :create_keypair + request :delete_keypair + request :get_keypair + class Mock < Fog::Rackspace::Service include Fog::Rackspace::MockData diff --git a/lib/fog/rackspace/mock_data.rb b/lib/fog/rackspace/mock_data.rb index 369fe6545..19be9a9ca 100644 --- a/lib/fog/rackspace/mock_data.rb +++ b/lib/fog/rackspace/mock_data.rb @@ -109,7 +109,7 @@ module Fog :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID}, :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, :networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID }, - + :keys => [], :servers => {}, #Block Storage diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb new file mode 100644 index 000000000..0e4bdd549 --- /dev/null +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -0,0 +1,28 @@ +require 'fog/core/model' + +module Fog + module Compute + class RackspaceV2 + class Keypair < Fog::Model + attribute :public_key + attribute :private_key + attribute :user_id + identity :name + attribute :fingerprint + + def save + requires :name + data = service.create_keypair(name, public_key) + merge_attributes(data.body['keypair']) + data.body['keypair']['name'] == name + end + + def destroy + data = service.delete_keypair(identity) + true + end + + end + end + end +end diff --git a/lib/fog/rackspace/models/compute_v2/keypairs.rb b/lib/fog/rackspace/models/compute_v2/keypairs.rb new file mode 100644 index 000000000..2fd925155 --- /dev/null +++ b/lib/fog/rackspace/models/compute_v2/keypairs.rb @@ -0,0 +1,31 @@ +require 'fog/core/collection' +require 'fog/rackspace/models/compute_v2/keypair' + +module Fog + module Compute + class RackspaceV2 + + class Keypairs < Fog::Collection + + model Fog::Compute::RackspaceV2::Keypair + + def all + data = [] + service.list_keypairs.body['keypairs'].each do |kp| + data << kp['keypair'] if kp['keypair'] + end + load(data) + end + + def get(key_id) + begin + new(service.get_keypair(key_id).body['keypair']) + rescue Fog::Compute::Rackspace::NotFound + nil + end + end + + end + end + end +end diff --git a/lib/fog/rackspace/models/compute_v2/server.rb b/lib/fog/rackspace/models/compute_v2/server.rb index 193495e25..ce8c99dcf 100644 --- a/lib/fog/rackspace/models/compute_v2/server.rb +++ b/lib/fog/rackspace/models/compute_v2/server.rb @@ -51,6 +51,11 @@ module Fog # @return [String] server status. # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html#server_status attribute :state, :aliases => 'status' + + # @!attribute [r] state_ext + # @return [String] server (extended) status. + # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html#server_status + attribute :state_ext, :aliases => 'OS-EXT-STS:task_state' # @!attribute [r] progress # @return [Fixnum] The build completion progress, as a percentage. Value is from 0 to 100. @@ -198,11 +203,11 @@ module Fog options[:disk_config] = disk_config unless disk_config.nil? options[:metadata] = metadata.to_hash unless @metadata.nil? options[:personality] = personality unless personality.nil? + options[:keypair] ||= attributes[:keypair] if options[:networks] options[:networks].map! { |id| { :uuid => id } } end - data = service.create_server(name, image_id, flavor_id, 1, 1, options) merge_attributes(data.body['server']) true @@ -331,7 +336,7 @@ module Fog # Server's private IPv4 address # @return [String] private IPv4 address def private_ip_address - addresses['private'].select{|a| a["version"] == 4}[0]["addr"] + addresses['private'].select{|a| a["version"] == 4}[0]["addr"] rescue '' end # Server's public IPv4 address diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb new file mode 100644 index 000000000..e2b783ebc --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -0,0 +1,36 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Requests a new keypair to be created + # + # ==== Parameters + # * name<~String> - name to give new key + # + # ==== Returns + # * response<~Excon::Response>: + # * keypair<~Hash>: + # * 'fingerprint'<~String>: + # * 'name'<~String>: + # * 'private_key'<~String>: + # * 'public_key'<~String>: + # * 'user_id'<~String>: + def create_keypair(name, public_key=nil) + data = { + 'keypair' => { + 'name' => name + } + } + + request( + :method => 'POST', + :expects => 200, + :path => '/os-keypairs', + :body => Fog::JSON.encode(data) + ) + end + end + end + end +end diff --git a/lib/fog/rackspace/requests/compute_v2/create_server.rb b/lib/fog/rackspace/requests/compute_v2/create_server.rb index fb51f0780..0d440d6a4 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_server.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_server.rb @@ -43,11 +43,11 @@ module Fog def create_server(name, image_id, flavor_id, min_count, max_count, options = {}) data = { 'server' => { - 'name' => name, - 'imageRef' => image_id, + 'name' => name, + 'imageRef' => image_id, 'flavorRef' => flavor_id, - 'minCount' => min_count, - 'maxCount' => max_count + 'minCount' => min_count, + 'maxCount' => max_count } } @@ -58,12 +58,13 @@ module Fog { :uuid => '00000000-0000-0000-0000-000000000000' }, { :uuid => '11111111-1111-1111-1111-111111111111' } ] + data['server']['key_name'] = options[:keypair][:name] unless options[:keypair].nil? request( - :body => Fog::JSON.encode(data), + :body => Fog::JSON.encode(data), :expects => [202], - :method => 'POST', - :path => "servers" + :method => 'POST', + :path => "servers" ) end end diff --git a/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb new file mode 100644 index 000000000..03144dc0e --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb @@ -0,0 +1,26 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Deletes the key specified with key_name + # + # ==== Parameters + # * key_name<~String> - name of key to be deleted + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # *'success'<~Bool>: true or false for success + def delete_keypair(key_name) + request( + :method => 'DELETE', + :expects => 202, + :path => "/os-keypairs/#{key_name}" + ) + end + end + + end + end +end diff --git a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb new file mode 100644 index 000000000..7c3b67e09 --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb @@ -0,0 +1,29 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Returns details of key by name specified + # + # ==== Parameters + # 'key_name'<~String>: name of key to request + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'keypair'<~Hash>: Name of key + # * 'public_key'<~String>: public key + # * 'name'<~String>: key name + # * 'fingerprint'<~String>: id + # + def get_key(key_name) + request( + :method => 'GET', + :expects => 200, + :path => "/os-keypairs/#{key_name}" + ) + end + end + end + end +end diff --git a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb new file mode 100644 index 000000000..1f64d2543 --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb @@ -0,0 +1,26 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Returns list of instances that the authenticated user manages. + # + # ==== Parameters + # No parameters + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'keypairs'<~Array>: list of keypairs + # * 'keypair'<~Hash>: fingerprint, name, public_key + def list_keypairs + request( + :method => 'GET', + :expects => 200, + :path => '/os-keypairs' + ) + end + end + end + end +end From 3ba1f44e401d9cf4015aede7f996a06bb348464f Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Wed, 17 Jul 2013 09:39:18 +0200 Subject: [PATCH 02/15] AWS: remove logging --- lib/fog/aws/models/compute/key_pairs.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/fog/aws/models/compute/key_pairs.rb b/lib/fog/aws/models/compute/key_pairs.rb index 00875f5e6..2e326606c 100644 --- a/lib/fog/aws/models/compute/key_pairs.rb +++ b/lib/fog/aws/models/compute/key_pairs.rb @@ -56,10 +56,7 @@ module Fog end self.filters = filters data = service.describe_key_pairs(filters).body -puts "AWS KEY-PAIRS - DATA : #{data.inspect}" - r = load(data['keySet']) -puts "AWS KEY-PAIRS : #{r.inspect}" - r + load(data['keySet']) end # Used to retrieve a key pair that was created with the AWS.key_pairs.create method. From 978c39885c993a0db90f47a12e136ff06ac00afb Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Fri, 19 Jul 2013 11:07:47 +0200 Subject: [PATCH 03/15] rackspace keypairs: add some Mocks --- lib/fog/rackspace/mock_data.rb | 30 +++++++++++++++---- .../requests/compute_v2/create_keypair.rb | 14 +++++++++ .../requests/compute_v2/list_keypairs.rb | 12 ++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/fog/rackspace/mock_data.rb b/lib/fog/rackspace/mock_data.rb index 19be9a9ca..1bfa37036 100644 --- a/lib/fog/rackspace/mock_data.rb +++ b/lib/fog/rackspace/mock_data.rb @@ -8,10 +8,11 @@ module Fog @@data ||= Hash.new do |hash, key| hash[key] = begin #Compute V2 - flavor_id = Fog.credentials[:rackspace_flavor_id].to_s ||= Fog::Mock.random_numbers(1) - image_id = Fog.credentials[:rackspace_image_id] ||= Fog::Rackspace::MockData.uuid + flavor_id = Fog.credentials[:rackspace_flavor_id].to_s ||= Fog::Mock.random_numbers(1) + image_id = Fog.credentials[:rackspace_image_id] ||= Fog::Rackspace::MockData.uuid image_name = Fog::Mock.random_letters(6) network_id = Fog::Rackspace::MockData.uuid + user_id = Fog::Mock.random_numbers(6).to_s flavor = { "OS-FLV-EXT-DATA:ephemeral" => 4, @@ -88,6 +89,22 @@ module Fog 'cidr' => '192.168.0.0/24' } + key_pair1 = { + 'public_key' => "ssh-rsa ".concat(Fog::Mock.random_letters(372)).concat(" Generated by Nova\n"), + 'private_key' => "-----BEGIN RSA PRIVATE KEY-----\n".concat(Fog::Mock.random_letters(1635)).concat("\n-----END RSA PRIVATE KEY-----\n"), + 'user_id' => user_id, + 'name' => Fog::Mock.random_letters(32), + 'fingerprint' => "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" + } + + key_pair2 = { + 'public_key' => "ssh-rsa ".concat(Fog::Mock.random_letters(372)).concat(" Generated by Nova\n"), + 'private_key' => "-----BEGIN RSA PRIVATE KEY-----\n".concat(Fog::Mock.random_letters(1635)).concat("\n-----END RSA PRIVATE KEY-----\n"), + 'user_id' => user_id, + 'name' => Fog::Mock.random_letters(32), + 'fingerprint' => "11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11" + } + #Block Storage volume_type1_id = Fog::Mock.random_numbers(3).to_s volume_type2_id = Fog::Mock.random_numbers(3).to_s @@ -106,11 +123,12 @@ module Fog mock_data = { #Compute V2 - :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID}, - :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, + :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID }, + :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, :networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID }, - :keys => [], - :servers => {}, + :keys => [], + :keypairs => [{'keypair' => key_pair1}, {'keypair' => key_pair2}], + :servers => {}, #Block Storage :volumes => {}, diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb index e2b783ebc..5d0e2ed48 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -31,6 +31,20 @@ module Fog ) end end + + class Mock + def create_keypair(name, public_key=nil) + + k = self.data[:keypairs][0] + k['keypair']['name'] = name + + response = Excon::Response.new + response.status = 200 + response.body = k + response + end + end + end end end diff --git a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb index 1f64d2543..7e83f9f7f 100644 --- a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb +++ b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb @@ -21,6 +21,18 @@ module Fog ) end end + + class Mock + def list_keypairs + response = Excon::Response.new + response.status = 200 + response.body = { + "keypairs" => self.data[:keypairs] + } + response + end + end + end end end From 000901ac46c9d213d721bbc02731d6d34361bb6b Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Tue, 23 Jul 2013 09:29:19 +0200 Subject: [PATCH 04/15] rename function 'get_key' => 'get_keypair' --- lib/fog/rackspace/requests/compute_v2/get_keypair.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb index 7c3b67e09..f91c65ede 100644 --- a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb @@ -16,7 +16,7 @@ module Fog # * 'name'<~String>: key name # * 'fingerprint'<~String>: id # - def get_key(key_name) + def get_keypair(key_name) request( :method => 'GET', :expects => 200, From 54709b86777ada1ca4163c7d8980e2c0447c8d0a Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Wed, 17 Jul 2013 09:18:27 +0200 Subject: [PATCH 05/15] Rackspace: add keypair support --- lib/fog/rackspace/compute_v2.rb | 7 ++++ lib/fog/rackspace/mock_data.rb | 2 +- .../rackspace/models/compute_v2/keypair.rb | 28 +++++++++++++++ .../rackspace/models/compute_v2/keypairs.rb | 31 ++++++++++++++++ lib/fog/rackspace/models/compute_v2/server.rb | 9 +++-- .../requests/compute_v2/create_keypair.rb | 36 +++++++++++++++++++ .../requests/compute_v2/create_server.rb | 15 ++++---- .../requests/compute_v2/delete_keypair.rb | 26 ++++++++++++++ .../requests/compute_v2/get_keypair.rb | 29 +++++++++++++++ .../requests/compute_v2/list_keypairs.rb | 26 ++++++++++++++ 10 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 lib/fog/rackspace/models/compute_v2/keypair.rb create mode 100644 lib/fog/rackspace/models/compute_v2/keypairs.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/create_keypair.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/delete_keypair.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/get_keypair.rb create mode 100644 lib/fog/rackspace/requests/compute_v2/list_keypairs.rb diff --git a/lib/fog/rackspace/compute_v2.rb b/lib/fog/rackspace/compute_v2.rb index a86bba26f..79df7b6ef 100644 --- a/lib/fog/rackspace/compute_v2.rb +++ b/lib/fog/rackspace/compute_v2.rb @@ -54,6 +54,8 @@ module Fog collection :attachments model :network collection :networks + model :keypair + collection :keypairs request_path 'fog/rackspace/requests/compute_v2' request :list_servers @@ -97,6 +99,11 @@ module Fog request :create_network request :delete_network + request :list_keypairs + request :create_keypair + request :delete_keypair + request :get_keypair + class Mock < Fog::Rackspace::Service include Fog::Rackspace::MockData diff --git a/lib/fog/rackspace/mock_data.rb b/lib/fog/rackspace/mock_data.rb index 369fe6545..19be9a9ca 100644 --- a/lib/fog/rackspace/mock_data.rb +++ b/lib/fog/rackspace/mock_data.rb @@ -109,7 +109,7 @@ module Fog :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID}, :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, :networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID }, - + :keys => [], :servers => {}, #Block Storage diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb new file mode 100644 index 000000000..0e4bdd549 --- /dev/null +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -0,0 +1,28 @@ +require 'fog/core/model' + +module Fog + module Compute + class RackspaceV2 + class Keypair < Fog::Model + attribute :public_key + attribute :private_key + attribute :user_id + identity :name + attribute :fingerprint + + def save + requires :name + data = service.create_keypair(name, public_key) + merge_attributes(data.body['keypair']) + data.body['keypair']['name'] == name + end + + def destroy + data = service.delete_keypair(identity) + true + end + + end + end + end +end diff --git a/lib/fog/rackspace/models/compute_v2/keypairs.rb b/lib/fog/rackspace/models/compute_v2/keypairs.rb new file mode 100644 index 000000000..2fd925155 --- /dev/null +++ b/lib/fog/rackspace/models/compute_v2/keypairs.rb @@ -0,0 +1,31 @@ +require 'fog/core/collection' +require 'fog/rackspace/models/compute_v2/keypair' + +module Fog + module Compute + class RackspaceV2 + + class Keypairs < Fog::Collection + + model Fog::Compute::RackspaceV2::Keypair + + def all + data = [] + service.list_keypairs.body['keypairs'].each do |kp| + data << kp['keypair'] if kp['keypair'] + end + load(data) + end + + def get(key_id) + begin + new(service.get_keypair(key_id).body['keypair']) + rescue Fog::Compute::Rackspace::NotFound + nil + end + end + + end + end + end +end diff --git a/lib/fog/rackspace/models/compute_v2/server.rb b/lib/fog/rackspace/models/compute_v2/server.rb index 193495e25..ce8c99dcf 100644 --- a/lib/fog/rackspace/models/compute_v2/server.rb +++ b/lib/fog/rackspace/models/compute_v2/server.rb @@ -51,6 +51,11 @@ module Fog # @return [String] server status. # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html#server_status attribute :state, :aliases => 'status' + + # @!attribute [r] state_ext + # @return [String] server (extended) status. + # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/List_Servers-d1e2078.html#server_status + attribute :state_ext, :aliases => 'OS-EXT-STS:task_state' # @!attribute [r] progress # @return [Fixnum] The build completion progress, as a percentage. Value is from 0 to 100. @@ -198,11 +203,11 @@ module Fog options[:disk_config] = disk_config unless disk_config.nil? options[:metadata] = metadata.to_hash unless @metadata.nil? options[:personality] = personality unless personality.nil? + options[:keypair] ||= attributes[:keypair] if options[:networks] options[:networks].map! { |id| { :uuid => id } } end - data = service.create_server(name, image_id, flavor_id, 1, 1, options) merge_attributes(data.body['server']) true @@ -331,7 +336,7 @@ module Fog # Server's private IPv4 address # @return [String] private IPv4 address def private_ip_address - addresses['private'].select{|a| a["version"] == 4}[0]["addr"] + addresses['private'].select{|a| a["version"] == 4}[0]["addr"] rescue '' end # Server's public IPv4 address diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb new file mode 100644 index 000000000..e2b783ebc --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -0,0 +1,36 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Requests a new keypair to be created + # + # ==== Parameters + # * name<~String> - name to give new key + # + # ==== Returns + # * response<~Excon::Response>: + # * keypair<~Hash>: + # * 'fingerprint'<~String>: + # * 'name'<~String>: + # * 'private_key'<~String>: + # * 'public_key'<~String>: + # * 'user_id'<~String>: + def create_keypair(name, public_key=nil) + data = { + 'keypair' => { + 'name' => name + } + } + + request( + :method => 'POST', + :expects => 200, + :path => '/os-keypairs', + :body => Fog::JSON.encode(data) + ) + end + end + end + end +end diff --git a/lib/fog/rackspace/requests/compute_v2/create_server.rb b/lib/fog/rackspace/requests/compute_v2/create_server.rb index fb51f0780..0d440d6a4 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_server.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_server.rb @@ -43,11 +43,11 @@ module Fog def create_server(name, image_id, flavor_id, min_count, max_count, options = {}) data = { 'server' => { - 'name' => name, - 'imageRef' => image_id, + 'name' => name, + 'imageRef' => image_id, 'flavorRef' => flavor_id, - 'minCount' => min_count, - 'maxCount' => max_count + 'minCount' => min_count, + 'maxCount' => max_count } } @@ -58,12 +58,13 @@ module Fog { :uuid => '00000000-0000-0000-0000-000000000000' }, { :uuid => '11111111-1111-1111-1111-111111111111' } ] + data['server']['key_name'] = options[:keypair][:name] unless options[:keypair].nil? request( - :body => Fog::JSON.encode(data), + :body => Fog::JSON.encode(data), :expects => [202], - :method => 'POST', - :path => "servers" + :method => 'POST', + :path => "servers" ) end end diff --git a/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb new file mode 100644 index 000000000..03144dc0e --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb @@ -0,0 +1,26 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Deletes the key specified with key_name + # + # ==== Parameters + # * key_name<~String> - name of key to be deleted + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # *'success'<~Bool>: true or false for success + def delete_keypair(key_name) + request( + :method => 'DELETE', + :expects => 202, + :path => "/os-keypairs/#{key_name}" + ) + end + end + + end + end +end diff --git a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb new file mode 100644 index 000000000..7c3b67e09 --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb @@ -0,0 +1,29 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Returns details of key by name specified + # + # ==== Parameters + # 'key_name'<~String>: name of key to request + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'keypair'<~Hash>: Name of key + # * 'public_key'<~String>: public key + # * 'name'<~String>: key name + # * 'fingerprint'<~String>: id + # + def get_key(key_name) + request( + :method => 'GET', + :expects => 200, + :path => "/os-keypairs/#{key_name}" + ) + end + end + end + end +end diff --git a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb new file mode 100644 index 000000000..1f64d2543 --- /dev/null +++ b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb @@ -0,0 +1,26 @@ +module Fog + module Compute + class RackspaceV2 + class Real + + # Returns list of instances that the authenticated user manages. + # + # ==== Parameters + # No parameters + # + # ==== Returns + # * response<~Excon::Response>: + # * body<~Hash>: + # * 'keypairs'<~Array>: list of keypairs + # * 'keypair'<~Hash>: fingerprint, name, public_key + def list_keypairs + request( + :method => 'GET', + :expects => 200, + :path => '/os-keypairs' + ) + end + end + end + end +end From 8bf52e4342b9e15085724941a20c0457fe80e334 Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Fri, 19 Jul 2013 11:07:47 +0200 Subject: [PATCH 06/15] rackspace keypairs: add some Mocks --- lib/fog/rackspace/mock_data.rb | 30 +++++++++++++++---- .../requests/compute_v2/create_keypair.rb | 14 +++++++++ .../requests/compute_v2/list_keypairs.rb | 12 ++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/lib/fog/rackspace/mock_data.rb b/lib/fog/rackspace/mock_data.rb index 19be9a9ca..1bfa37036 100644 --- a/lib/fog/rackspace/mock_data.rb +++ b/lib/fog/rackspace/mock_data.rb @@ -8,10 +8,11 @@ module Fog @@data ||= Hash.new do |hash, key| hash[key] = begin #Compute V2 - flavor_id = Fog.credentials[:rackspace_flavor_id].to_s ||= Fog::Mock.random_numbers(1) - image_id = Fog.credentials[:rackspace_image_id] ||= Fog::Rackspace::MockData.uuid + flavor_id = Fog.credentials[:rackspace_flavor_id].to_s ||= Fog::Mock.random_numbers(1) + image_id = Fog.credentials[:rackspace_image_id] ||= Fog::Rackspace::MockData.uuid image_name = Fog::Mock.random_letters(6) network_id = Fog::Rackspace::MockData.uuid + user_id = Fog::Mock.random_numbers(6).to_s flavor = { "OS-FLV-EXT-DATA:ephemeral" => 4, @@ -88,6 +89,22 @@ module Fog 'cidr' => '192.168.0.0/24' } + key_pair1 = { + 'public_key' => "ssh-rsa ".concat(Fog::Mock.random_letters(372)).concat(" Generated by Nova\n"), + 'private_key' => "-----BEGIN RSA PRIVATE KEY-----\n".concat(Fog::Mock.random_letters(1635)).concat("\n-----END RSA PRIVATE KEY-----\n"), + 'user_id' => user_id, + 'name' => Fog::Mock.random_letters(32), + 'fingerprint' => "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" + } + + key_pair2 = { + 'public_key' => "ssh-rsa ".concat(Fog::Mock.random_letters(372)).concat(" Generated by Nova\n"), + 'private_key' => "-----BEGIN RSA PRIVATE KEY-----\n".concat(Fog::Mock.random_letters(1635)).concat("\n-----END RSA PRIVATE KEY-----\n"), + 'user_id' => user_id, + 'name' => Fog::Mock.random_letters(32), + 'fingerprint' => "11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11" + } + #Block Storage volume_type1_id = Fog::Mock.random_numbers(3).to_s volume_type2_id = Fog::Mock.random_numbers(3).to_s @@ -106,11 +123,12 @@ module Fog mock_data = { #Compute V2 - :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID}, - :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, + :flavors => Hash.new { |h,k| h[k] = flavor unless k == NOT_FOUND_ID }, + :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, :networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID }, - :keys => [], - :servers => {}, + :keys => [], + :keypairs => [{'keypair' => key_pair1}, {'keypair' => key_pair2}], + :servers => {}, #Block Storage :volumes => {}, diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb index e2b783ebc..5d0e2ed48 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -31,6 +31,20 @@ module Fog ) end end + + class Mock + def create_keypair(name, public_key=nil) + + k = self.data[:keypairs][0] + k['keypair']['name'] = name + + response = Excon::Response.new + response.status = 200 + response.body = k + response + end + end + end end end diff --git a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb index 1f64d2543..7e83f9f7f 100644 --- a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb +++ b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb @@ -21,6 +21,18 @@ module Fog ) end end + + class Mock + def list_keypairs + response = Excon::Response.new + response.status = 200 + response.body = { + "keypairs" => self.data[:keypairs] + } + response + end + end + end end end From eb4b597cdee035c55aa4909c145d8dcbb1722ded Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Tue, 23 Jul 2013 09:29:19 +0200 Subject: [PATCH 07/15] rename function 'get_key' => 'get_keypair' --- lib/fog/rackspace/requests/compute_v2/get_keypair.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb index 7c3b67e09..f91c65ede 100644 --- a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb @@ -16,7 +16,7 @@ module Fog # * 'name'<~String>: key name # * 'fingerprint'<~String>: id # - def get_key(key_name) + def get_keypair(key_name) request( :method => 'GET', :expects => 200, From 969ae676a99f859660fc37f10a1c85796af9e415 Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Wed, 24 Jul 2013 13:28:29 +0200 Subject: [PATCH 08/15] rackspace: create_server - API change: options[:keypair] takes String i.s.o. Hash (-> the keypair name) --- lib/fog/rackspace/requests/compute_v2/create_server.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/fog/rackspace/requests/compute_v2/create_server.rb b/lib/fog/rackspace/requests/compute_v2/create_server.rb index 0d440d6a4..d41357b03 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_server.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_server.rb @@ -12,12 +12,13 @@ module Fog # @option options [Hash] metadata key value pairs of server metadata # @option options [String] OS-DCF:diskConfig The disk configuration value. (AUTO or MANUAL) # @option options [Hash] personality Hash containing data to inject into the file system of the cloud server instance during server creation. + # @option options [String] keypair Name of the kay-pair to associate with this server. # @return [Excon::Response] response: # * body [Hash]: # * server [Hash]: # * name [String] - name of server # * imageRef [String] - id of image used to create server - # * flavorRef [String] - id of flavor used to create server + # * flavorRef [String] - id of flavor used to create server # * OS-DCF:diskConfig [String] - The disk configuration value. # * name [String] - name of server # * metadata [Hash] - Metadata key and value pairs. @@ -58,7 +59,7 @@ module Fog { :uuid => '00000000-0000-0000-0000-000000000000' }, { :uuid => '11111111-1111-1111-1111-111111111111' } ] - data['server']['key_name'] = options[:keypair][:name] unless options[:keypair].nil? + data['server']['key_name'] = options[:keypair] unless options[:keypair].nil? request( :body => Fog::JSON.encode(data), From 70229ffbf9f2b3ecac1a5d8a6a713c6a6c78a9bd Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Wed, 24 Jul 2013 16:29:44 +0200 Subject: [PATCH 09/15] rackspace: keypairs - add unit tests --- lib/fog/rackspace/mock_data.rb | 15 ++--- .../requests/compute_v2/create_keypair.rb | 8 +-- .../requests/compute_v2/delete_keypair.rb | 14 ++++- .../requests/compute_v2/get_keypair.rb | 12 ++++ .../requests/compute_v2/list_keypairs.rb | 4 +- .../requests/compute_v2/keypair_tests.rb | 55 +++++++++++++++++++ 6 files changed, 88 insertions(+), 20 deletions(-) create mode 100644 tests/rackspace/requests/compute_v2/keypair_tests.rb diff --git a/lib/fog/rackspace/mock_data.rb b/lib/fog/rackspace/mock_data.rb index 1bfa37036..07aa4921c 100644 --- a/lib/fog/rackspace/mock_data.rb +++ b/lib/fog/rackspace/mock_data.rb @@ -89,7 +89,7 @@ module Fog 'cidr' => '192.168.0.0/24' } - key_pair1 = { + key_pair = { 'public_key' => "ssh-rsa ".concat(Fog::Mock.random_letters(372)).concat(" Generated by Nova\n"), 'private_key' => "-----BEGIN RSA PRIVATE KEY-----\n".concat(Fog::Mock.random_letters(1635)).concat("\n-----END RSA PRIVATE KEY-----\n"), 'user_id' => user_id, @@ -97,14 +97,6 @@ module Fog 'fingerprint' => "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" } - key_pair2 = { - 'public_key' => "ssh-rsa ".concat(Fog::Mock.random_letters(372)).concat(" Generated by Nova\n"), - 'private_key' => "-----BEGIN RSA PRIVATE KEY-----\n".concat(Fog::Mock.random_letters(1635)).concat("\n-----END RSA PRIVATE KEY-----\n"), - 'user_id' => user_id, - 'name' => Fog::Mock.random_letters(32), - 'fingerprint' => "11:11:11:11:11:11:11:11:11:11:11:11:11:11:11:11" - } - #Block Storage volume_type1_id = Fog::Mock.random_numbers(3).to_s volume_type2_id = Fog::Mock.random_numbers(3).to_s @@ -127,7 +119,8 @@ module Fog :images => Hash.new { |h,k| h[k] = image unless k == NOT_FOUND_ID }, :networks => Hash.new { |h,k| h[k] = network unless k == NOT_FOUND_ID }, :keys => [], - :keypairs => [{'keypair' => key_pair1}, {'keypair' => key_pair2}], + :keypair => key_pair, + :keypairs => [], :servers => {}, #Block Storage @@ -141,7 +134,7 @@ module Fog mock_data[:flavors][flavor_id] = flavor mock_data[:images][image_id] = image mock_data[:networks][network_id] = network - + mock_data end end[@rackspace_api_key] diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb index 5d0e2ed48..4601d444c 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -34,13 +34,13 @@ module Fog class Mock def create_keypair(name, public_key=nil) - - k = self.data[:keypairs][0] - k['keypair']['name'] = name + k = self.data[:keypair] + k['name'] = name + self.data[:keypairs] << { 'keypair' => k } response = Excon::Response.new response.status = 200 - response.body = k + response.body = { 'keypair' => k } response end end diff --git a/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb index 03144dc0e..010b53248 100644 --- a/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb @@ -10,8 +10,7 @@ module Fog # # ==== Returns # * response<~Excon::Response>: - # * body<~Hash>: - # *'success'<~Bool>: true or false for success + # def delete_keypair(key_name) request( :method => 'DELETE', @@ -21,6 +20,17 @@ module Fog end end + class Mock + def delete_keypair(name) + if self.data[:keypairs].select { |k| name.include? k['keypair']['name'] }.empty? + raise Fog::Compute::RackspaceV2::NotFound + else + self.data[:keypairs].reject! { |k| name.include? k['keypair']['name'] } + response(:status => 202) + end + end + end + end end end diff --git a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb index f91c65ede..8eb3a3f67 100644 --- a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb @@ -24,6 +24,18 @@ module Fog ) end end + + class Mock + def get_keypair(name) + key = self.data[:keypairs].select { |k| name.include? k['keypair']['name'] }.first + if key.nil? + raise Fog::Compute::RackspaceV2::NotFound + end + + response(:body => key, :status => 200) + end + end + end end end diff --git a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb index 7e83f9f7f..9c927e3a6 100644 --- a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb +++ b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb @@ -26,9 +26,7 @@ module Fog def list_keypairs response = Excon::Response.new response.status = 200 - response.body = { - "keypairs" => self.data[:keypairs] - } + response.body = { 'keypairs' => self.data[:keypairs] } response end end diff --git a/tests/rackspace/requests/compute_v2/keypair_tests.rb b/tests/rackspace/requests/compute_v2/keypair_tests.rb new file mode 100644 index 000000000..1590df0c3 --- /dev/null +++ b/tests/rackspace/requests/compute_v2/keypair_tests.rb @@ -0,0 +1,55 @@ +Shindo.tests('Fog::Compute::RackspaceV2 | keypair_tests', ['rackspace']) do + + keypair_format = { + 'name' => String, + 'public_key' => String, + 'fingerprint' => String, + } + + create_keypair_format = { + 'keypair' => keypair_format.merge({ + 'user_id' => String, + 'private_key' => String + }) + } + + list_keypair_format = { + 'keypairs' => [ 'keypair' => keypair_format ] + } + + get_keypair_format = { + 'keypair' => keypair_format + } + + service = Fog::Compute.new(:provider => 'Rackspace', :version => 'V2') + keypair_name = Fog::Mock.random_letters(32) + + tests('success') do + tests('#create_keypair').formats(create_keypair_format) do + service.create_keypair(keypair_name).body + end + + tests('#list_keypairs').formats(list_keypair_format) do + service.list_keypairs.body + end + + tests('#get_keypair').formats(get_keypair_format) do + service.get_keypair(keypair_name).body + end + + tests('#delete_keypair') do + service.delete_keypair(keypair_name).body + end + end + + unknown_keypair_name = Fog::Mock.random_letters(32) + tests('failure') do + tests('#get_unknown_keypair').raises(Fog::Compute::RackspaceV2::NotFound) do + service.get_keypair(unknown_keypair_name).body + end + + tests('#delete_unknown_keypair').raises(Fog::Compute::RackspaceV2::NotFound) do + service.delete_keypair(unknown_keypair_name).body + end + end +end From 6b5a2c63f67667101899ab678cb83ec228c19d6b Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Wed, 24 Jul 2013 23:17:36 +0200 Subject: [PATCH 10/15] rackspace:keypairs - add 'model' unit tests --- .../models/compute_v2/keypairs_tests.rb | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 tests/rackspace/models/compute_v2/keypairs_tests.rb diff --git a/tests/rackspace/models/compute_v2/keypairs_tests.rb b/tests/rackspace/models/compute_v2/keypairs_tests.rb new file mode 100644 index 000000000..72f9d31a5 --- /dev/null +++ b/tests/rackspace/models/compute_v2/keypairs_tests.rb @@ -0,0 +1,33 @@ +Shindo.tests('Fog::Compute::RackspaceV2 | keypairs', ['rackspace']) do + service = Fog::Compute::RackspaceV2.new + + name = Fog::Mock.random_letters(32) + + tests("success") do + tests("#create").succeeds do + service.keypairs.create({:name => name}) + end + + tests("#all").succeeds do + service.keypairs.all + end + + tests("#get").succeeds do + service.keypairs.get(name) + end + + tests("#delete").succeeds do + service.keypairs.destroy(name) + end + end + + tests('failure') do + tests("failed_get").raises(Fog::Compute::RackspaceV2::NotFound) do + service.keypairs.get(Fog::Mock.random_letters(32)) + end + + tests("failed_delete").raises(Fog::Compute::RackspaceV2::NotFound) do + service.keypairs.destroy(Fog::Mock.random_letters(32)) + end + end +end From b208e9df4aa9c2dcb813dfa7e13b758c1d4cf459 Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Thu, 25 Jul 2013 07:45:22 +0200 Subject: [PATCH 11/15] rackspace:kaypair - small corrections during unit test --- lib/fog/rackspace/models/compute_v2/keypair.rb | 5 ++++- lib/fog/rackspace/models/compute_v2/keypairs.rb | 2 +- tests/rackspace/models/compute_v2/keypairs_tests.rb | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb index 0e4bdd549..8854fb1ad 100644 --- a/lib/fog/rackspace/models/compute_v2/keypair.rb +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -18,7 +18,10 @@ module Fog end def destroy - data = service.delete_keypair(identity) + begin + service.delete_keypair(identity) + rescue Fog::Compute::RackspaceV2::NotFound + end true end diff --git a/lib/fog/rackspace/models/compute_v2/keypairs.rb b/lib/fog/rackspace/models/compute_v2/keypairs.rb index 2fd925155..ece51bd31 100644 --- a/lib/fog/rackspace/models/compute_v2/keypairs.rb +++ b/lib/fog/rackspace/models/compute_v2/keypairs.rb @@ -20,7 +20,7 @@ module Fog def get(key_id) begin new(service.get_keypair(key_id).body['keypair']) - rescue Fog::Compute::Rackspace::NotFound + rescue Fog::Compute::RackspaceV2::NotFound nil end end diff --git a/tests/rackspace/models/compute_v2/keypairs_tests.rb b/tests/rackspace/models/compute_v2/keypairs_tests.rb index 72f9d31a5..6afdd76be 100644 --- a/tests/rackspace/models/compute_v2/keypairs_tests.rb +++ b/tests/rackspace/models/compute_v2/keypairs_tests.rb @@ -22,12 +22,12 @@ Shindo.tests('Fog::Compute::RackspaceV2 | keypairs', ['rackspace']) do end tests('failure') do - tests("failed_get").raises(Fog::Compute::RackspaceV2::NotFound) do + tests("unknown_get").returns(nil) do service.keypairs.get(Fog::Mock.random_letters(32)) end - tests("failed_delete").raises(Fog::Compute::RackspaceV2::NotFound) do - service.keypairs.destroy(Fog::Mock.random_letters(32)) + tests("unknown_delete").returns(true) do + service.keypairs.destroy(Fog::Mock.random_letters(32)) end end end From 3f769049056a7dcc2c33135642a50f80363dacee Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Thu, 25 Jul 2013 22:58:05 +0200 Subject: [PATCH 12/15] rackspace: keypairs - throw Fog::Compute::RackspaceV2::NotFound when HTTP:404 received --- lib/fog/rackspace/models/compute_v2/keypair.rb | 5 +---- lib/fog/rackspace/models/compute_v2/keypairs.rb | 4 ---- tests/rackspace/models/compute_v2/keypairs_tests.rb | 4 ++-- 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb index 8854fb1ad..0694aa621 100644 --- a/lib/fog/rackspace/models/compute_v2/keypair.rb +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -18,11 +18,8 @@ module Fog end def destroy - begin service.delete_keypair(identity) - rescue Fog::Compute::RackspaceV2::NotFound - end - true + true end end diff --git a/lib/fog/rackspace/models/compute_v2/keypairs.rb b/lib/fog/rackspace/models/compute_v2/keypairs.rb index ece51bd31..ba2f366b0 100644 --- a/lib/fog/rackspace/models/compute_v2/keypairs.rb +++ b/lib/fog/rackspace/models/compute_v2/keypairs.rb @@ -18,11 +18,7 @@ module Fog end def get(key_id) - begin new(service.get_keypair(key_id).body['keypair']) - rescue Fog::Compute::RackspaceV2::NotFound - nil - end end end diff --git a/tests/rackspace/models/compute_v2/keypairs_tests.rb b/tests/rackspace/models/compute_v2/keypairs_tests.rb index 6afdd76be..b9fbd2bbe 100644 --- a/tests/rackspace/models/compute_v2/keypairs_tests.rb +++ b/tests/rackspace/models/compute_v2/keypairs_tests.rb @@ -22,11 +22,11 @@ Shindo.tests('Fog::Compute::RackspaceV2 | keypairs', ['rackspace']) do end tests('failure') do - tests("unknown_get").returns(nil) do + tests("unknown_get").raises(Fog::Compute::RackspaceV2::NotFound) do service.keypairs.get(Fog::Mock.random_letters(32)) end - tests("unknown_delete").returns(true) do + tests("unknown_delete").raises(Fog::Compute::RackspaceV2::NotFound) do service.keypairs.destroy(Fog::Mock.random_letters(32)) end end From 6db3f1ac062e086d80b6ede98d4342eed609dd1c Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Fri, 26 Jul 2013 08:57:02 +0200 Subject: [PATCH 13/15] rackspace: keypairs - additional unittests + correct keypairs.destroy behaviour --- .../rackspace/models/compute_v2/keypair.rb | 5 +- .../requests/compute_v2/create_keypair.rb | 3 + .../models/compute_v2/keypairs_tests.rb | 56 ++++++++++++------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb index 0694aa621..ee9c9f940 100644 --- a/lib/fog/rackspace/models/compute_v2/keypair.rb +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -18,7 +18,10 @@ module Fog end def destroy - service.delete_keypair(identity) + begin + service.delete_keypair(identity) + rescue Fog::Compute::RackspaceV2::NotFound + end true end diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb index 1bdf27908..06a5f38fc 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -34,6 +34,9 @@ module Fog class Mock def create_keypair(name, public_key=nil) + # 409 response when already existing + raise Fog::Compute::RackspaceV2::ServiceError if not self.data[:keypairs].select { |k| name.include? k['keypair']['name'] }.first.nil? + k = self.data[:keypair] k['name'] = name self.data[:keypairs] << { 'keypair' => k } diff --git a/tests/rackspace/models/compute_v2/keypairs_tests.rb b/tests/rackspace/models/compute_v2/keypairs_tests.rb index b9fbd2bbe..f1bd316d2 100644 --- a/tests/rackspace/models/compute_v2/keypairs_tests.rb +++ b/tests/rackspace/models/compute_v2/keypairs_tests.rb @@ -1,33 +1,47 @@ Shindo.tests('Fog::Compute::RackspaceV2 | keypairs', ['rackspace']) do service = Fog::Compute::RackspaceV2.new - name = Fog::Mock.random_letters(32) + name = Fog::Mock.random_letters(32) + key = nil - tests("success") do - tests("#create").succeeds do - service.keypairs.create({:name => name}) - end + tests("API access") do + begin + tests("create").succeeds do + key = service.keypairs.create({:name => name}) + end - tests("#all").succeeds do - service.keypairs.all - end + tests("list all").succeeds do + service.keypairs.all + end - tests("#get").succeeds do - service.keypairs.get(name) - end + tests("get").succeeds do + service.keypairs.get(name) + end - tests("#delete").succeeds do - service.keypairs.destroy(name) - end - end + tests("delete").succeeds do + key = nil if service.keypairs.destroy(name) + key == nil + end - tests('failure') do - tests("unknown_get").raises(Fog::Compute::RackspaceV2::NotFound) do - service.keypairs.get(Fog::Mock.random_letters(32)) - end + tests("get unknown").raises(Fog::Compute::RackspaceV2::NotFound) do + service.keypairs.get(Fog::Mock.random_letters(32)) + end - tests("unknown_delete").raises(Fog::Compute::RackspaceV2::NotFound) do - service.keypairs.destroy(Fog::Mock.random_letters(32)) + tests("delete unknown").returns(true) do + service.keypairs.destroy(Fog::Mock.random_letters(32)) + end + + tests("create again after delete").succeeds do + key = service.keypairs.create({:name => name}) + end + + tests("create already existing").raises(Fog::Compute::RackspaceV2::ServiceError) do + service.keypairs.create({:name => name}) + end + + ensure + key.destroy if key end + end end From 24deb48e01d02eefd95acd01b947f67c6bcaba17 Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Fri, 26 Jul 2013 15:00:45 +0200 Subject: [PATCH 14/15] rackspace - keypairs : redo exception/no-exception logic for ::destroy and ::get functions --- lib/fog/rackspace/models/compute_v2/keypair.rb | 6 ++---- lib/fog/rackspace/models/compute_v2/keypairs.rb | 6 +++++- tests/rackspace/models/compute_v2/keypairs_tests.rb | 4 ++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb index ee9c9f940..fbb441257 100644 --- a/lib/fog/rackspace/models/compute_v2/keypair.rb +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -18,10 +18,8 @@ module Fog end def destroy - begin - service.delete_keypair(identity) - rescue Fog::Compute::RackspaceV2::NotFound - end + requires :identity + service.delete_keypair(identity) true end diff --git a/lib/fog/rackspace/models/compute_v2/keypairs.rb b/lib/fog/rackspace/models/compute_v2/keypairs.rb index ba2f366b0..00fc79320 100644 --- a/lib/fog/rackspace/models/compute_v2/keypairs.rb +++ b/lib/fog/rackspace/models/compute_v2/keypairs.rb @@ -18,7 +18,11 @@ module Fog end def get(key_id) - new(service.get_keypair(key_id).body['keypair']) + begin + new(service.get_keypair(key_id).body['keypair']) + rescue Fog::Compute::RackspaceV2::NotFound + nil + end end end diff --git a/tests/rackspace/models/compute_v2/keypairs_tests.rb b/tests/rackspace/models/compute_v2/keypairs_tests.rb index f1bd316d2..1c2c943a0 100644 --- a/tests/rackspace/models/compute_v2/keypairs_tests.rb +++ b/tests/rackspace/models/compute_v2/keypairs_tests.rb @@ -23,11 +23,11 @@ Shindo.tests('Fog::Compute::RackspaceV2 | keypairs', ['rackspace']) do key == nil end - tests("get unknown").raises(Fog::Compute::RackspaceV2::NotFound) do + tests("get unknown").returns(nil) do service.keypairs.get(Fog::Mock.random_letters(32)) end - tests("delete unknown").returns(true) do + tests("delete unknown").raises(Fog::Compute::RackspaceV2::NotFound) do service.keypairs.destroy(Fog::Mock.random_letters(32)) end From 5ca9bba5c5924f701e54f604e7dce8c649885f6a Mon Sep 17 00:00:00 2001 From: Bart Vercammen Date: Fri, 26 Jul 2013 16:04:29 +0200 Subject: [PATCH 15/15] rackspace: keypairs - add documentation --- .../rackspace/models/compute_v2/keypair.rb | 33 ++++++++++++++-- .../rackspace/models/compute_v2/keypairs.rb | 16 +++++++- .../requests/compute_v2/create_keypair.rb | 38 ++++++++++--------- .../requests/compute_v2/delete_keypair.rb | 22 +++++------ .../requests/compute_v2/get_keypair.rb | 30 +++++++-------- .../requests/compute_v2/list_keypairs.rb | 23 ++++++----- 6 files changed, 103 insertions(+), 59 deletions(-) diff --git a/lib/fog/rackspace/models/compute_v2/keypair.rb b/lib/fog/rackspace/models/compute_v2/keypair.rb index fbb441257..ae4f7a606 100644 --- a/lib/fog/rackspace/models/compute_v2/keypair.rb +++ b/lib/fog/rackspace/models/compute_v2/keypair.rb @@ -4,12 +4,33 @@ module Fog module Compute class RackspaceV2 class Keypair < Fog::Model - attribute :public_key - attribute :private_key - attribute :user_id + + # @!attribute [rw] name + # @return [String] the keypair name identity :name + + # @!attribute [r] public_key + # @return [String] the public key + attribute :public_key + + # @!attribute [r] private_key + # @return [String] the private key + attribute :private_key + + # @!attribute [r] user_id + # @return [String] the user_id associated to + attribute :user_id + + # @!attribute [r] fingerprint + # @return [String] unique fingerprint attribute :fingerprint + # Creates a keypair + # @return [Boolean] true if the keypair is successfully created + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] def save requires :name data = service.create_keypair(name, public_key) @@ -17,6 +38,12 @@ module Fog data.body['keypair']['name'] == name end + # Destroys a keypair + # @return [Boolean] true if the keypair is successfully deleted + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] def destroy requires :identity service.delete_keypair(identity) diff --git a/lib/fog/rackspace/models/compute_v2/keypairs.rb b/lib/fog/rackspace/models/compute_v2/keypairs.rb index 00fc79320..94bab546d 100644 --- a/lib/fog/rackspace/models/compute_v2/keypairs.rb +++ b/lib/fog/rackspace/models/compute_v2/keypairs.rb @@ -9,6 +9,12 @@ module Fog model Fog::Compute::RackspaceV2::Keypair + # Fetch the list of known keypairs + # @return [Fog::Compute::RackspaceV2::Keypairs] the retreived keypairs + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] def all data = [] service.list_keypairs.body['keypairs'].each do |kp| @@ -17,9 +23,15 @@ module Fog load(data) end - def get(key_id) + # Fetch keypair details + # @param [String] key_name: name of the key to request + # @return [Fog::Compute::RackspaceV2::Keypair] the requested keypair or 'nil' when not found + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] + def get(key_name) begin - new(service.get_keypair(key_id).body['keypair']) + new(service.get_keypair(key_name).body['keypair']) rescue Fog::Compute::RackspaceV2::NotFound nil end diff --git a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb index 06a5f38fc..14473dbb8 100644 --- a/lib/fog/rackspace/requests/compute_v2/create_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/create_keypair.rb @@ -3,23 +3,25 @@ module Fog class RackspaceV2 class Real - # Requests a new keypair to be created - # - # ==== Parameters - # * name<~String> - name to give new key - # - # ==== Returns - # * response<~Excon::Response>: - # * keypair<~Hash>: - # * 'fingerprint'<~String>: - # * 'name'<~String>: - # * 'private_key'<~String>: - # * 'public_key'<~String>: - # * 'user_id'<~String>: - def create_keypair(name, public_key=nil) + # Request a new keypair to be created + # @param [String] key_name: unique name of the keypair to create + # @return [Excon::Response] response : + # * body [Hash]: - + # * 'keypair' [Hash]: - + # * 'fingerprint' [String]: unique fingerprint of the keypair + # * 'name' [String]: unique name of the keypair + # * 'private_key' [String]: the private key of the keypair (only available here, at creation time) + # * 'public_key' [String]: the public key of the keypair + # * 'user_id' [String]: the user id + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] + # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/CreateKeyPair.html + def create_keypair(key_name, public_key=nil) data = { 'keypair' => { - 'name' => name + 'name' => key_name } } @@ -33,12 +35,12 @@ module Fog end class Mock - def create_keypair(name, public_key=nil) + def create_keypair(key_name, public_key=nil) # 409 response when already existing - raise Fog::Compute::RackspaceV2::ServiceError if not self.data[:keypairs].select { |k| name.include? k['keypair']['name'] }.first.nil? + raise Fog::Compute::RackspaceV2::ServiceError if not self.data[:keypairs].select { |k| key_name.include? k['keypair']['name'] }.first.nil? k = self.data[:keypair] - k['name'] = name + k['name'] = key_name self.data[:keypairs] << { 'keypair' => k } response( :status => 200, diff --git a/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb index 010b53248..0fd7b88df 100644 --- a/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/delete_keypair.rb @@ -3,14 +3,14 @@ module Fog class RackspaceV2 class Real - # Deletes the key specified with key_name - # - # ==== Parameters - # * key_name<~String> - name of key to be deleted - # - # ==== Returns - # * response<~Excon::Response>: - # + # Delete the key specified with key_name + # @param [String] key_name: name of the key to delete + # @return [Excon::Response] response + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] + # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/DeleteKeyPair.html def delete_keypair(key_name) request( :method => 'DELETE', @@ -21,11 +21,11 @@ module Fog end class Mock - def delete_keypair(name) - if self.data[:keypairs].select { |k| name.include? k['keypair']['name'] }.empty? + def delete_keypair(key_name) + if self.data[:keypairs].select { |k| key_name.include? k['keypair']['name'] }.empty? raise Fog::Compute::RackspaceV2::NotFound else - self.data[:keypairs].reject! { |k| name.include? k['keypair']['name'] } + self.data[:keypairs].reject! { |k| key_name.include? k['keypair']['name'] } response(:status => 202) end end diff --git a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb index 8eb3a3f67..a832ee60e 100644 --- a/lib/fog/rackspace/requests/compute_v2/get_keypair.rb +++ b/lib/fog/rackspace/requests/compute_v2/get_keypair.rb @@ -3,19 +3,19 @@ module Fog class RackspaceV2 class Real - # Returns details of key by name specified - # - # ==== Parameters - # 'key_name'<~String>: name of key to request - # - # ==== Returns - # * response<~Excon::Response>: - # * body<~Hash>: - # * 'keypair'<~Hash>: Name of key - # * 'public_key'<~String>: public key - # * 'name'<~String>: key name - # * 'fingerprint'<~String>: id - # + # Retreive single keypair details + # @param [String] key_name: name of the key for which to request the details + # @return [Excon::Response] response : + # * body [Hash]: - + # * 'keypair' [Hash]: - + # * 'fingerprint' [String]: unique fingerprint of the keypair + # * 'name' [String]: unique name of the keypair + # * 'public_key' [String]: the public key assigne to the keypair + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] + # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ListKeyPairs.html def get_keypair(key_name) request( :method => 'GET', @@ -26,8 +26,8 @@ module Fog end class Mock - def get_keypair(name) - key = self.data[:keypairs].select { |k| name.include? k['keypair']['name'] }.first + def get_keypair(key_name) + key = self.data[:keypairs].select { |k| key_name.include? k['keypair']['name'] }.first if key.nil? raise Fog::Compute::RackspaceV2::NotFound end diff --git a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb index c1ce5a115..137f39032 100644 --- a/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb +++ b/lib/fog/rackspace/requests/compute_v2/list_keypairs.rb @@ -3,16 +3,19 @@ module Fog class RackspaceV2 class Real - # Returns list of instances that the authenticated user manages. - # - # ==== Parameters - # No parameters - # - # ==== Returns - # * response<~Excon::Response>: - # * body<~Hash>: - # * 'keypairs'<~Array>: list of keypairs - # * 'keypair'<~Hash>: fingerprint, name, public_key + # Returns a list of all key pairs associated with an account. + # @return [Excon::Response] response : + # * body [Hash]: - + # * 'keypairs' [Array]: list of keypairs + # * 'keypair' [Hash]: - + # * 'fingerprint' [String]: unique fingerprint of the keypair + # * 'name' [String]: unique name of the keypair + # * 'public_key' [String]: the public key assigned to the keypair + # @raise [Fog::Compute::RackspaceV2::NotFound] + # @raise [Fog::Compute::RackspaceV2::BadRequest] + # @raise [Fog::Compute::RackspaceV2::InternalServerError] + # @raise [Fog::Compute::RackspaceV2::ServiceError] + # @see http://docs.rackspace.com/servers/api/v2/cs-devguide/content/ListKeyPairs.html def list_keypairs request( :method => 'GET',