From 1220c8989b4f324b76ae9e7bdbd3e7779d1953cc Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Tue, 30 Apr 2013 18:11:22 -0700 Subject: [PATCH] add set_metadata to upload ssh keys --- lib/fog/google/models/compute/server.rb | 8 ++--- .../google/requests/compute/insert_network.rb | 2 +- .../google/requests/compute/set_metadata.rb | 36 +++++++++++++++++++ tests/google/models/compute/server_tests.rb | 8 +++++ 4 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 lib/fog/google/requests/compute/set_metadata.rb diff --git a/lib/fog/google/models/compute/server.rb b/lib/fog/google/models/compute/server.rb index d3709b673..1c7d90545 100644 --- a/lib/fog/google/models/compute/server.rb +++ b/lib/fog/google/models/compute/server.rb @@ -13,6 +13,7 @@ module Fog attribute :state, :aliases => 'status' attribute :zone_name, :aliases => 'zone' attribute :machine_type, :aliases => 'machineType' + attribute :metadata def destroy requires :name @@ -59,11 +60,8 @@ module Fog end def setup(credentials = {}) - requires :public_ip_address, :identity, :public_key, :username - Fog::SSH.new(public_ip_address, username, credentials).run([ - %{mkdir .ssh}, - %{echo "#{public_key}" >> ~/.ssh/authorized_keys}, - ]) + requires :public_ip_address, :public_key, :username + service.set_metadata(self.instance, self.zone, {'sshKeys' => self.public_key }) rescue Errno::ECONNREFUSED sleep(1) retry diff --git a/lib/fog/google/requests/compute/insert_network.rb b/lib/fog/google/requests/compute/insert_network.rb index 6aa395888..8442608a9 100644 --- a/lib/fog/google/requests/compute/insert_network.rb +++ b/lib/fog/google/requests/compute/insert_network.rb @@ -4,7 +4,7 @@ module Fog class Mock - def insert_network(network_name) + def insert_network(network_name, ip_range) Fog::Mock.not_implemented end diff --git a/lib/fog/google/requests/compute/set_metadata.rb b/lib/fog/google/requests/compute/set_metadata.rb new file mode 100644 index 000000000..6f164ed6f --- /dev/null +++ b/lib/fog/google/requests/compute/set_metadata.rb @@ -0,0 +1,36 @@ +module Fog + module Compute + class Google + + class Mock + + def set_metadata(instance, zone, metadata={}) + Fog::Mock.not_implemented + end + + end + + class Real + + def set_metadata(instance, zone, metadata={}) + api_method = @compute.instance.set_metadata + parameters = { + 'project' => @project, + 'instance' => instance, + 'zone' => zone, + } + body_object = { + "items" => metadata.to_a.map {|pair| { :key => pair[0], :value => pair[1] } } + } + result = self.build_result( + api_method, + parameters, + body_object=body_object) + response = self.build_response(result) + end + + end + + end + end +end diff --git a/tests/google/models/compute/server_tests.rb b/tests/google/models/compute/server_tests.rb index ff351d235..8801142b7 100644 --- a/tests/google/models/compute/server_tests.rb +++ b/tests/google/models/compute/server_tests.rb @@ -7,6 +7,14 @@ Shindo.tests("Fog::Compute[:google] | server model", ['google']) do @instance.ready? end + test('#sshable?') do + @instance.sshable? + end + + test('#ssh') do + @instance.ssh("uname") == "Linux" + end + test('#destroy') do response = @instance.destroy response.body['operationType'] == 'delete'