From db487f9b320dbd0f0a4c6c2c22629d9120f680f6 Mon Sep 17 00:00:00 2001 From: Sergio Rubio Date: Mon, 25 Mar 2013 22:43:23 +0100 Subject: [PATCH] [digitalocean|compute] added SshKey model, collection and related tests --- .../digitalocean/models/compute/ssh_key.rb | 28 +++++++++++++ .../digitalocean/models/compute/ssh_keys.rb | 27 +++++++++++++ .../models/compute/ssh_key_tests.rb | 40 +++++++++++++++++++ .../models/compute/ssh_keys_tests.rb | 28 +++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 lib/fog/digitalocean/models/compute/ssh_key.rb create mode 100644 lib/fog/digitalocean/models/compute/ssh_keys.rb create mode 100644 tests/digitalocean/models/compute/ssh_key_tests.rb create mode 100644 tests/digitalocean/models/compute/ssh_keys_tests.rb diff --git a/lib/fog/digitalocean/models/compute/ssh_key.rb b/lib/fog/digitalocean/models/compute/ssh_key.rb new file mode 100644 index 000000000..a5d203536 --- /dev/null +++ b/lib/fog/digitalocean/models/compute/ssh_key.rb @@ -0,0 +1,28 @@ +module Fog + module Compute + class DigitalOcean + class SshKey < Fog::Model + + identity :id + + attribute :name + attribute :ssh_pub_key + + def save + requires :name, :ssh_pub_key + + merge_attributes(service.create_ssh_key(name, ssh_pub_key).body['ssh_key']) + true + end + + def destroy + requires :id + + service.destroy_ssh_key id + true + end + + end + end + end +end diff --git a/lib/fog/digitalocean/models/compute/ssh_keys.rb b/lib/fog/digitalocean/models/compute/ssh_keys.rb new file mode 100644 index 000000000..6e5d27bc6 --- /dev/null +++ b/lib/fog/digitalocean/models/compute/ssh_keys.rb @@ -0,0 +1,27 @@ +require 'fog/digitalocean/models/compute/ssh_key' + +module Fog + module Compute + class DigitalOcean + class SshKeys < Fog::Collection + + identity :href + + model Fog::Compute::DigitalOcean::SshKey + + def all + data = service.list_ssh_keys.body['ssh_keys'] + load(data) + end + + def get(uri) + if data = service.get_ssh_key(uri) + new(data.body) + end + rescue Fog::Errors::NotFound + nil + end + end + end + end +end diff --git a/tests/digitalocean/models/compute/ssh_key_tests.rb b/tests/digitalocean/models/compute/ssh_key_tests.rb new file mode 100644 index 000000000..906f0a836 --- /dev/null +++ b/tests/digitalocean/models/compute/ssh_key_tests.rb @@ -0,0 +1,40 @@ +Shindo.tests("Fog::Compute[:digitalocean] | ssh_key model", ['digitalocean', 'compute']) do + + service = Fog::Compute[:digitalocean] + + tests('The ssh_key model should') do + + test('#save') do + @key = service.ssh_keys.create :name => 'fookey', + :ssh_pub_key => 'fookey' + @key.is_a? Fog::Compute::DigitalOcean::SshKey + end + tests('have the action') do + test('reload') { @key.respond_to? 'reload' } + %w{ + save + destroy + }.each do |action| + test(action) { @key.respond_to? action } + end + end + tests('have attributes') do + attributes = [ + :id, + :name, + :ssh_pub_key + ] + tests("The key model should respond to") do + attributes.each do |attribute| + test("#{attribute}") { @key.respond_to? attribute } + end + end + end + test('#destroy') do + @key.destroy + end + + end + +end + diff --git a/tests/digitalocean/models/compute/ssh_keys_tests.rb b/tests/digitalocean/models/compute/ssh_keys_tests.rb new file mode 100644 index 000000000..10f64f12a --- /dev/null +++ b/tests/digitalocean/models/compute/ssh_keys_tests.rb @@ -0,0 +1,28 @@ +Shindo.tests('Fog::Compute[:digitalocean] | ssh_keys collection', ['digitalocean']) do + + service = Fog::Compute[:digitalocean] + + tests('The ssh_keys collection') do + key = service.ssh_keys.create :name => 'fookey', + :ssh_pub_key => 'fookey' + [:all, :get].each do |method| + test("should respond to #{method}") do + service.ssh_keys.respond_to? method + end + end + + tests('should have Fog::Compute::DigitalOcean::SshKey inside') do + service.ssh_keys.each do |s| + test { s.kind_of? Fog::Compute::DigitalOcean::SshKey } + end + end + + tests('should be able to get a model') do + test('by instance id') do + service.ssh_keys.get(key.id).kind_of? Fog::Compute::DigitalOcean::SshKey + end + end + + end + +end