Skip to content

Commit 5ae1a79

Browse files
committed
[linode] cleanup and tests
1 parent 1b35b9d commit 5ae1a79

10 files changed

Lines changed: 218 additions & 19 deletions

File tree

‎lib/fog/linode.rb‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ def request(params)
5858
params[:query] ||= {}
5959
params[:query].merge!(:api_key => @linode_api_key)
6060

61-
begin
62-
response = @connection.request(params.merge!({:host => @host}))
63-
rescue Excon::Errors::Error => error
64-
raise case error
65-
when Excon::Errors::NotFound
66-
Fog::Linode::NotFound.slurp(error)
67-
else
68-
error
69-
end
70-
end
61+
response = @connection.request(params.merge!({:host => @host}))
7162

7263
unless response.body.empty?
7364
response.body = JSON.parse(response.body)
65+
if data = response.body['ERRORARRAY'].first
66+
error = case data['ERRORCODE']
67+
when 5
68+
Fog::Linode::NotFound
69+
else
70+
Fog::Linode::Error
71+
end
72+
raise error.new(data['ERRORMESSAGE'])
73+
end
7474
end
7575
response
7676
end

‎lib/fog/linode/requests/avail_distributions.rb‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ class Real
55
# Get available distributions
66
#
77
# ==== Parameters
8-
# * options<~Hash>:
9-
# * distributionId<~Integer>: id to limit results to
8+
# * distributionId<~Integer>: id to limit results to
109
#
1110
# ==== Returns
1211
# * response<~Excon::Response>:
1312
# * body<~Array>:
1413
# TODO: docs
15-
def avail_distributions(options={})
14+
def avail_distributions(distribution_id=nil)
15+
options = {}
16+
if distribution_id
17+
options.merge!(:distributionId => distribution_id)
18+
end
1619
request(
1720
:expects => 200,
1821
:method => 'GET',

‎lib/fog/linode/requests/avail_linodeplans.rb‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ class Real
88
# * response<~Excon::Response>:
99
# * body<~Array>:
1010
# TODO: docs
11-
def avail_linodeplans
11+
def avail_linodeplans(linodeplan_id=nil)
12+
options = {}
13+
if linodeplan_id
14+
options.merge!(:planId => linodeplan_id)
15+
end
1216
request(
1317
:expects => 200,
1418
:method => 'GET',
15-
:query => { :api_action => 'avail.linodeplans' }
19+
:query => { :api_action => 'avail.linodeplans' }.merge!(options)
1620
)
1721
end
1822

‎lib/fog/linode/requests/linode_list.rb‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ class Real
55
# List all linodes user has access or delete to
66
#
77
# ==== Parameters
8-
# * options<~Hash>:
9-
# * linodeId<~Integer>: Limit the list to the specified LinodeID
8+
# * linodeId<~Integer>: Limit the list to the specified LinodeID
109
#
1110
# ==== Returns
1211
# * response<~Excon::Response>:
1312
# * body<~Array>:
1413
# TODO: docs
15-
def linode_list(options={})
14+
def linode_list(linode_id=nil)
15+
options = {}
16+
if linode_id
17+
options.merge!(:linodeId => linode_id)
18+
end
1619
request(
1720
:expects => 200,
1821
:method => 'GET',

‎tests/linode/helper.rb‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module Linode
2+
3+
def self.[](service)
4+
@@connections ||= Hash.new do |hash, key|
5+
credentials = Fog.credentials.reject do |k,v|
6+
![:linode_api_key].include?(k)
7+
end
8+
hash[key] = case key
9+
when :linode
10+
Fog::Linode.new(credentials)
11+
end
12+
end
13+
@@connections[service]
14+
end
15+
16+
module Formats
17+
18+
BASIC = {
19+
'ERRORARRAY' => [],
20+
'ACTION' => String
21+
}
22+
23+
end
24+
25+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Shindo.tests('Linode | datacenter requests', ['linode']) do
2+
3+
@datacenters_format = Linode::Formats::BASIC.merge({
4+
'DATA' => [{
5+
'DATACENTERID' => Integer,
6+
'LOCATION' => String
7+
}]
8+
})
9+
10+
tests('success') do
11+
12+
tests('#avail_datacenters').formats(@datacenters_format) do
13+
Linode[:linode].avail_datacenters.body
14+
end
15+
16+
end
17+
18+
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Shindo.tests('Linode | distribution requests', ['linode']) do
2+
3+
@distributions_format = Linode::Formats::BASIC.merge({
4+
'DATA' => [{
5+
'CREATE_DT' => String,
6+
'DISTRIBUTIONID' => Integer,
7+
'IS64BIT' => Integer,
8+
'LABEL' => String,
9+
'MINIMAGESIZE' => Integer,
10+
'REQUIRESPVOPSKERNEL' => Integer
11+
}]
12+
})
13+
14+
tests('success') do
15+
16+
@distribution_id = nil
17+
18+
tests('#avail_distributions').formats(@distributions_format) do
19+
data = Linode[:linode].avail_distributions.body
20+
@distribution_id = data['DATA'].first['DISTRIBUTIONID']
21+
data
22+
end
23+
24+
tests("@avail_distributions(#{@distribution_id})").formats(@distributions_format) do
25+
Linode[:linode].avail_distributions(@distribution_id).body
26+
end
27+
28+
end
29+
30+
end
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Shindo.tests('Linode | linode requests', ['linode']) do
2+
3+
@linode_format = Linode::Formats::BASIC.merge({
4+
'DATA' => { 'LinodeID' => Integer }
5+
})
6+
7+
@linodes_format = Linode::Formats::BASIC.merge({
8+
'DATA' => [{
9+
'ALERT_BWIN_ENABLED' => Integer,
10+
'ALERT_CPU_THRESHOLD' => Integer,
11+
'ALERT_BWOUT_ENABLED' => Integer,
12+
'ALERT_BWOUT_THRESHOLD' => Integer,
13+
'ALERT_BWQUOTA_ENABLED' => Integer,
14+
'ALERT_BWQUOTA_THRESHOLD' => Integer,
15+
'ALERT_BWIN_THRESHOLD' => Integer,
16+
'ALERT_CPU_ENABLED' => Integer,
17+
'ALERT_DISKIO_ENABLED' => Integer,
18+
'ALERT_DISKIO_THRESHOLD' => Integer,
19+
'BACKUPSENABLED' => Integer,
20+
'BACKUPWEEKLYDAY' => Integer,
21+
'BACKUPWINDOW' => Integer,
22+
'DATACENTERID' => Integer,
23+
'LABEL' => String,
24+
'LINODEID' => Integer,
25+
'LPM_DISPLAYGROUP' => String,
26+
'STATUS' => Integer,
27+
'TOTALHD' => Integer,
28+
'TOTALRAM' => Integer,
29+
'TOTALXFER' => Integer,
30+
'WATCHDOG' => Integer,
31+
}]
32+
})
33+
34+
@reboot_format = Linode::Formats::BASIC.merge({
35+
'DATA' => { 'JobID' => Integer }
36+
})
37+
38+
tests('success') do
39+
40+
@linode_id = nil
41+
42+
# (2 => Dallas, TX, USA), (1 => 1 month), (1 => Linode 512)
43+
tests('#linode_create(2, 1, 1)').formats(@linode_format) do
44+
data = Linode[:linode].linode_create(2, 1, 1).body
45+
@linode_id = data['DATA']['LinodeID']
46+
data
47+
end
48+
49+
tests("#linode_list(#{@linode_id})").formats(@linodes_format) do
50+
Linode[:linode].linode_list(@linode_id).body
51+
end
52+
53+
tests('#linode_list').formats(@linodes_format) do
54+
Linode[:linode].linode_list.body
55+
end
56+
57+
# tests("#linode_reboot(#{@linode_id})").formats(@reboot_format) do
58+
# Linode[:linode].linode_reboot(@linode_id).body
59+
# end
60+
61+
tests('#linode_delete(#{@linode_id})').succeeds do
62+
Linode[:linode].linode_delete(@linode_id)
63+
end
64+
65+
end
66+
67+
tests('failure') do
68+
69+
tests('#linode_reboot(0)').raises(Fog::Linode::NotFound) do
70+
Linode[:linode].linode_reboot(0)
71+
end
72+
73+
tests('#linode_delete(0)').raises(Fog::Linode::NotFound) do
74+
Linode[:linode].linode_delete(0)
75+
end
76+
77+
end
78+
79+
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Shindo.tests('Linode | linodeplans requests', ['linode']) do
2+
3+
@linodeplans_format = Linode::Formats::BASIC.merge({
4+
'DATA' => [{
5+
'AVAIL' => {
6+
'2' => Integer,
7+
'3' => Integer,
8+
'4' => Integer,
9+
'6' => Integer,
10+
'7' => Integer
11+
},
12+
'DISK' => Integer,
13+
'PLANID' => Integer,
14+
'PRICE' => Float,
15+
'RAM' => Integer,
16+
'LABEL' => String,
17+
'XFER' => Integer
18+
}]
19+
})
20+
21+
tests('success') do
22+
23+
@linodeplan_id = nil
24+
25+
tests('#avail_linodeplans').formats(@linodeplans_format) do
26+
data = Linode[:linode].avail_linodeplans.body
27+
@linodeplan_id = data['DATA'].first['PLANID']
28+
data
29+
end
30+
31+
tests("#avail_linodeplans(#{@linodeplan_id})").formats(@linodeplans_format) do
32+
Linode[:linode].avail_linodeplans(@linodeplan_id).body
33+
end
34+
35+
end
36+
37+
end

‎tests/slicehost/requests/slice_tests.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
Slicehost[:slices].reboot_slice(0)
5555
end
5656

57-
tests('delete_slice(0)').raises(Fog::Slicehost::NotFound) do
57+
tests('#delete_slice(0)').raises(Fog::Slicehost::NotFound) do
5858
Slicehost[:slices].delete_slice(0)
5959
end
6060

0 commit comments

Comments
 (0)