Skip to content

Commit

Permalink
Merge pull request fog#3500 from tnn2/glesys_sshkeys
Browse files Browse the repository at this point in the history
[GleSYS] add support for SSH key management
  • Loading branch information
geemus committed Mar 31, 2015
2 parents c26b252 + 8150e0f commit 8a54eab
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/fog/glesys/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Glesys < Fog::Service
model :template
collection :ips
model :ip
collection :ssh_keys
model :ssh_key

request_path 'fog/glesys/requests/compute'

Expand All @@ -40,6 +42,11 @@ class Glesys < Fog::Service
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
Expand Down
28 changes: 28 additions & 0 deletions lib/fog/glesys/models/compute/ssh_key.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions lib/fog/glesys/models/compute/ssh_keys.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/fog/glesys/requests/compute/ssh_key_add.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/fog/glesys/requests/compute/ssh_key_list.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/fog/glesys/requests/compute/ssh_key_remove.rb
Original file line number Diff line number Diff line change
@@ -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
53 changes: 53 additions & 0 deletions tests/glesys/requests/compute/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,59 @@ module Ips
}
}
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' => {
Expand Down
47 changes: 47 additions & 0 deletions tests/glesys/requests/compute/ssh_key_tests.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8a54eab

Please sign in to comment.