diff --git a/lib/fog/glesys/compute.rb b/lib/fog/glesys/compute.rb index 90e5c717a..a22a7364f 100644 --- a/lib/fog/glesys/compute.rb +++ b/lib/fog/glesys/compute.rb @@ -14,6 +14,8 @@ module Fog model :template collection :ips model :ip + collection :ssh_keys + model :ssh_key request_path 'fog/glesys/requests/compute' @@ -40,6 +42,11 @@ module Fog request :ip_add request :ip_remove + # SSH keys + request :ssh_key_list + request :ssh_key_add + request :ssh_key_remove + class Mock def initialize(options={}) @api_url = options[:glesys_api_url] || API_URL diff --git a/lib/fog/glesys/models/compute/ssh_key.rb b/lib/fog/glesys/models/compute/ssh_key.rb new file mode 100644 index 000000000..2f7770225 --- /dev/null +++ b/lib/fog/glesys/models/compute/ssh_key.rb @@ -0,0 +1,28 @@ +module Fog + module Compute + class Glesys + class SshKey < Fog::Model + identity :id + + attribute :description + attribute :data + + def save + requires :description, :data + + merge_attributes(service.ssh_key_add(:description => description, + :sshkey => data + ).body["response"]["sshkey"]) + true + end + + def destroy + requires :id + + service.ssh_key_remove(:sshkeyids => id) + true + end + end + end + end +end diff --git a/lib/fog/glesys/models/compute/ssh_keys.rb b/lib/fog/glesys/models/compute/ssh_keys.rb new file mode 100644 index 000000000..18e85a13d --- /dev/null +++ b/lib/fog/glesys/models/compute/ssh_keys.rb @@ -0,0 +1,21 @@ +require "fog/glesys/models/compute/ssh_key" + +module Fog + module Compute + class Glesys + class SshKeys < Fog::Collection + model Fog::Compute::Glesys::SshKey + + def all + data = service.ssh_key_list.body["response"]["sshkeys"] + load(data) + end + + def get(id) + hash = service.ssh_key_list.body["response"]["sshkeys"].find{|a| a["id"] == id} + hash.nil? ? nil : new(hash) + end + end + end + end +end diff --git a/lib/fog/glesys/requests/compute/ssh_key_add.rb b/lib/fog/glesys/requests/compute/ssh_key_add.rb new file mode 100644 index 000000000..74606c84b --- /dev/null +++ b/lib/fog/glesys/requests/compute/ssh_key_add.rb @@ -0,0 +1,11 @@ +module Fog + module Compute + class Glesys + class Real + def ssh_key_add(options) + request("sshkey/add", options) + end + end + end + end +end diff --git a/lib/fog/glesys/requests/compute/ssh_key_list.rb b/lib/fog/glesys/requests/compute/ssh_key_list.rb new file mode 100644 index 000000000..6976eae36 --- /dev/null +++ b/lib/fog/glesys/requests/compute/ssh_key_list.rb @@ -0,0 +1,11 @@ +module Fog + module Compute + class Glesys + class Real + def ssh_key_list(options = {}) + request("sshkey/list", options) + end + end + end + end +end diff --git a/lib/fog/glesys/requests/compute/ssh_key_remove.rb b/lib/fog/glesys/requests/compute/ssh_key_remove.rb new file mode 100644 index 000000000..59649617f --- /dev/null +++ b/lib/fog/glesys/requests/compute/ssh_key_remove.rb @@ -0,0 +1,11 @@ +module Fog + module Compute + class Glesys + class Real + def ssh_key_remove(options) + request("sshkey/remove", options) + end + end + end + end +end diff --git a/tests/glesys/requests/compute/helper.rb b/tests/glesys/requests/compute/helper.rb index 2f58fcf74..50e4d71fa 100644 --- a/tests/glesys/requests/compute/helper.rb +++ b/tests/glesys/requests/compute/helper.rb @@ -240,6 +240,59 @@ class Glesys } } end + module SshKeys + ADD = { + 'debug' => { + 'input' => { + 'sshkey' => String, + 'description' => String + } + }, + 'sshkey' => { + 'id' => Integer, + 'account' => String, + 'description' => String, + 'data' => String + }, + 'status' => { + 'timestamp' => String, + 'code' => Integer, + 'text' => String, + 'transactionid' => Fog::Nullable::String + } + } + REMOVE = { + 'debug' => { + 'input' => { + 'sshkeyids' => String, + } + }, + 'status' => { + 'timestamp' => String, + 'code' => Integer, + 'text' => String, + 'transactionid' => Fog::Nullable::String + } + } + LIST = { + 'debug' => { + 'input' => [] + }, + 'sshkeys' => [ + { + 'id' => Integer, + 'account' => String, + 'description' => String, + 'data' => String, + } + ], + 'status' => { + 'timestamp' => String, + 'code' => Integer, + 'text' => String + } + } + end module Templates LIST = { 'debug' => { diff --git a/tests/glesys/requests/compute/ssh_key_tests.rb b/tests/glesys/requests/compute/ssh_key_tests.rb new file mode 100644 index 000000000..894b83012 --- /dev/null +++ b/tests/glesys/requests/compute/ssh_key_tests.rb @@ -0,0 +1,47 @@ +Shindo.tests("Fog::Compute[:glesys] | ssh_key requests", ["glesys", "compute"]) do + @testdescription = "My test key to be removed" + @testdata = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDv+r/dCIDv+YazWsyc1WCixR+iOeaswTx1U45h6vh4/ fog-unittest@GleSYS" + @testdata_malformed = "ssh-rot13 AAAAthis_is_not_an_ssh_key fog-unittest@GleSYS" + + tests("success") do + tests("#ssh_key_add").formats(Glesys::Compute::Formats::SshKeys::ADD) do + pending if Fog.mocking? + @resp = Fog::Compute[:glesys].ssh_key_add(:description => @testdescription, :sshkey => @testdata) + @resp.body["response"] + end + + unless Fog.mocking? + Fog::Compute[:glesys].ssh_keys.destroy(@resp.body["response"]["sshkey"]["id"]) + @key = Fog::Compute[:glesys].ssh_keys.create(:description => @testdescription, :data => @testdata) + end + + tests("#ssh_key_list").formats(Glesys::Compute::Formats::SshKeys::LIST) do + pending if Fog.mocking? + @resp = Fog::Compute[:glesys].ssh_key_list + @resp.body["response"] + end + + unless Fog.mocking? + Fog::Compute[:glesys].ssh_keys.destroy(@key.id) + @key = Fog::Compute[:glesys].ssh_keys.create(:description => @testdescription, :data => @testdata) + end + + tests("#ssh_key_remove").formats(Glesys::Compute::Formats::SshKeys::REMOVE) do + pending if Fog.mocking? + @resp = Fog::Compute[:glesys].ssh_key_remove(:sshkeyids => @key.id) + @resp.body["response"] + end + end + + tests("failure") do + tests("#ssh_key_add with malformed key data").raises(Excon::Errors::HTTPStatusError) do + pending if Fog.mocking? + Fog::Compute[:glesys].ssh_key_add(:description => @testdescription, :data => @testdata_malformed) + end + + tests("#ssh_key_remove with nonexistent/invalid key id").raises(Excon::Errors::HTTPStatusError) do + pending if Fog.mocking? + Fog::Compute[:glesys].ssh_key_remove(:id => -1) + end + end +end