diff --git a/lib/fog/digitalocean/models/compute/servers.rb b/lib/fog/digitalocean/models/compute/servers.rb index 51a5eeaa5c..e308159762 100644 --- a/lib/fog/digitalocean/models/compute/servers.rb +++ b/lib/fog/digitalocean/models/compute/servers.rb @@ -17,22 +17,28 @@ def all(filters = {}) def bootstrap(new_attributes = {}) server = new(new_attributes) - raise(ArgumentError, "public_key_path is required to configure the server.") unless new_attributes[:public_key_path] - raise(ArgumentError, "private_key_path is required to configure the server.") unless new_attributes[:private_key_path] + check_keys(new_attributes) + credential = Fog.respond_to?(:credential) && Fog.credential || :default name = "fog_#{credential}" ssh_key = service.ssh_keys.detect { |key| key.name == name } if ssh_key.nil? ssh_key = service.ssh_keys.create( :name => name, - :ssh_pub_key => File.read(new_attributes[:public_key_path]) + :ssh_pub_key => (new_attributes[:public_key] || File.read(new_attributes[:public_key_path])) ) end server.ssh_keys = [ssh_key] server.save server.wait_for { ready? } - server.setup :keys => [new_attributes[:private_key_path]] + + if new_attributes[:private_key] + server.setup :key_data => [new_attributes[:private_key]] + else + server.setup :keys => [new_attributes[:private_key_path]] + end + server end @@ -43,6 +49,19 @@ def get(id) nil end + protected + + def check_keys(attributes) + check_key :public, attributes[:public_key], attributes[:public_key_path] + check_key :private, attributes[:private_key], attributes[:private_key_path] + end + + def check_key(name, data, path) + if [data, path].all?(&:nil?) + raise ArgumentError, "either #{name}_key or #{name}_key_path is required to configure the server" + end + end + end end diff --git a/tests/digitalocean/helper.rb b/tests/digitalocean/helper.rb index 1edc3d00a7..0d9792788a 100644 --- a/tests/digitalocean/helper.rb +++ b/tests/digitalocean/helper.rb @@ -5,7 +5,7 @@ def service end def fog_test_server_attributes - image = service.images.find { |i| i.name == 'Ubuntu 12.04 x64' } + image = service.images.find { |i| i.name == 'Ubuntu 13.10 x64' } region = service.regions.find { |r| r.name == 'New York 1' } flavor = service.flavors.find { |r| r.name == '512MB' } diff --git a/tests/digitalocean/models/compute/servers_tests.rb b/tests/digitalocean/models/compute/servers_tests.rb index e67f0f2d72..55777fcb4d 100644 --- a/tests/digitalocean/models/compute/servers_tests.rb +++ b/tests/digitalocean/models/compute/servers_tests.rb @@ -5,17 +5,32 @@ :name => "#{fog_server_name}-#{Time.now.to_i.to_s}" }.merge fog_test_server_attributes + public_key_path = File.join(File.dirname(__FILE__), '../../fixtures/id_rsa.pub') + private_key_path = File.join(File.dirname(__FILE__), '../../fixtures/id_rsa') + collection_tests(service.servers, options, true) do @instance.wait_for { ready? } end - tests("#bootstrap").succeeds do + tests("#bootstrap with public/private_key_path").succeeds do pending if Fog.mocking? @server = service.servers.bootstrap({ - :public_key_path => File.join(File.dirname(__FILE__), '../../fixtures/id_rsa.pub'), - :private_key_path => File.join(File.dirname(__FILE__), '../../fixtures/id_rsa') + :public_key_path => public_key_path, + :private_key_path => private_key_path }.merge(options)) + @server.destroy end - @server.destroy if @server + tests("#bootstrap with public/private_key").succeeds do + pending if Fog.mocking? + @server = service.servers.bootstrap({ + :public_key => File.read(public_key_path), + :private_key => File.read(private_key_path) + }.merge(options)) + @server.destroy + end + + tests("#bootstrap with no public/private keys") do + raises(ArgumentError, 'raises ArgumentError') { service.servers.bootstrap(options) } + end end