Skip to content

Commit

Permalink
[linode] cleanup and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geemus committed Jul 18, 2010
1 parent 1b35b9d commit 5ae1a79
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 19 deletions.
20 changes: 10 additions & 10 deletions lib/fog/linode.rb
Expand Up @@ -58,19 +58,19 @@ def request(params)
params[:query] ||= {}
params[:query].merge!(:api_key => @linode_api_key)

begin
response = @connection.request(params.merge!({:host => @host}))
rescue Excon::Errors::Error => error
raise case error
when Excon::Errors::NotFound
Fog::Linode::NotFound.slurp(error)
else
error
end
end
response = @connection.request(params.merge!({:host => @host}))

unless response.body.empty?
response.body = JSON.parse(response.body)
if data = response.body['ERRORARRAY'].first
error = case data['ERRORCODE']
when 5
Fog::Linode::NotFound
else
Fog::Linode::Error
end
raise error.new(data['ERRORMESSAGE'])
end
end
response
end
Expand Down
9 changes: 6 additions & 3 deletions lib/fog/linode/requests/avail_distributions.rb
Expand Up @@ -5,14 +5,17 @@ class Real
# Get available distributions
#
# ==== Parameters
# * options<~Hash>:
# * distributionId<~Integer>: id to limit results to
# * distributionId<~Integer>: id to limit results to
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_distributions(options={})
def avail_distributions(distribution_id=nil)
options = {}
if distribution_id
options.merge!(:distributionId => distribution_id)
end
request(
:expects => 200,
:method => 'GET',
Expand Down
8 changes: 6 additions & 2 deletions lib/fog/linode/requests/avail_linodeplans.rb
Expand Up @@ -8,11 +8,15 @@ class Real
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_linodeplans
def avail_linodeplans(linodeplan_id=nil)
options = {}
if linodeplan_id
options.merge!(:planId => linodeplan_id)
end
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.linodeplans' }
:query => { :api_action => 'avail.linodeplans' }.merge!(options)
)
end

Expand Down
9 changes: 6 additions & 3 deletions lib/fog/linode/requests/linode_list.rb
Expand Up @@ -5,14 +5,17 @@ class Real
# List all linodes user has access or delete to
#
# ==== Parameters
# * options<~Hash>:
# * linodeId<~Integer>: Limit the list to the specified LinodeID
# * linodeId<~Integer>: Limit the list to the specified LinodeID
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_list(options={})
def linode_list(linode_id=nil)
options = {}
if linode_id
options.merge!(:linodeId => linode_id)
end
request(
:expects => 200,
:method => 'GET',
Expand Down
25 changes: 25 additions & 0 deletions tests/linode/helper.rb
@@ -0,0 +1,25 @@
module Linode

def self.[](service)
@@connections ||= Hash.new do |hash, key|
credentials = Fog.credentials.reject do |k,v|
![:linode_api_key].include?(k)
end
hash[key] = case key
when :linode
Fog::Linode.new(credentials)
end
end
@@connections[service]
end

module Formats

BASIC = {
'ERRORARRAY' => [],
'ACTION' => String
}

end

end
18 changes: 18 additions & 0 deletions tests/linode/requests/datacenter_tests.rb
@@ -0,0 +1,18 @@
Shindo.tests('Linode | datacenter requests', ['linode']) do

@datacenters_format = Linode::Formats::BASIC.merge({
'DATA' => [{
'DATACENTERID' => Integer,
'LOCATION' => String
}]
})

tests('success') do

tests('#avail_datacenters').formats(@datacenters_format) do
Linode[:linode].avail_datacenters.body
end

end

end
30 changes: 30 additions & 0 deletions tests/linode/requests/distribution_tests.rb
@@ -0,0 +1,30 @@
Shindo.tests('Linode | distribution requests', ['linode']) do

@distributions_format = Linode::Formats::BASIC.merge({
'DATA' => [{
'CREATE_DT' => String,
'DISTRIBUTIONID' => Integer,
'IS64BIT' => Integer,
'LABEL' => String,
'MINIMAGESIZE' => Integer,
'REQUIRESPVOPSKERNEL' => Integer
}]
})

tests('success') do

@distribution_id = nil

tests('#avail_distributions').formats(@distributions_format) do
data = Linode[:linode].avail_distributions.body
@distribution_id = data['DATA'].first['DISTRIBUTIONID']
data
end

tests("@avail_distributions(#{@distribution_id})").formats(@distributions_format) do
Linode[:linode].avail_distributions(@distribution_id).body
end

end

end
79 changes: 79 additions & 0 deletions tests/linode/requests/linode_tests.rb
@@ -0,0 +1,79 @@
Shindo.tests('Linode | linode requests', ['linode']) do

@linode_format = Linode::Formats::BASIC.merge({
'DATA' => { 'LinodeID' => Integer }
})

@linodes_format = Linode::Formats::BASIC.merge({
'DATA' => [{
'ALERT_BWIN_ENABLED' => Integer,
'ALERT_CPU_THRESHOLD' => Integer,
'ALERT_BWOUT_ENABLED' => Integer,
'ALERT_BWOUT_THRESHOLD' => Integer,
'ALERT_BWQUOTA_ENABLED' => Integer,
'ALERT_BWQUOTA_THRESHOLD' => Integer,
'ALERT_BWIN_THRESHOLD' => Integer,
'ALERT_CPU_ENABLED' => Integer,
'ALERT_DISKIO_ENABLED' => Integer,
'ALERT_DISKIO_THRESHOLD' => Integer,
'BACKUPSENABLED' => Integer,
'BACKUPWEEKLYDAY' => Integer,
'BACKUPWINDOW' => Integer,
'DATACENTERID' => Integer,
'LABEL' => String,
'LINODEID' => Integer,
'LPM_DISPLAYGROUP' => String,
'STATUS' => Integer,
'TOTALHD' => Integer,
'TOTALRAM' => Integer,
'TOTALXFER' => Integer,
'WATCHDOG' => Integer,
}]
})

@reboot_format = Linode::Formats::BASIC.merge({
'DATA' => { 'JobID' => Integer }
})

tests('success') do

@linode_id = nil

# (2 => Dallas, TX, USA), (1 => 1 month), (1 => Linode 512)
tests('#linode_create(2, 1, 1)').formats(@linode_format) do
data = Linode[:linode].linode_create(2, 1, 1).body
@linode_id = data['DATA']['LinodeID']
data
end

tests("#linode_list(#{@linode_id})").formats(@linodes_format) do
Linode[:linode].linode_list(@linode_id).body
end

tests('#linode_list').formats(@linodes_format) do
Linode[:linode].linode_list.body
end

# tests("#linode_reboot(#{@linode_id})").formats(@reboot_format) do
# Linode[:linode].linode_reboot(@linode_id).body
# end

tests('#linode_delete(#{@linode_id})').succeeds do
Linode[:linode].linode_delete(@linode_id)
end

end

tests('failure') do

tests('#linode_reboot(0)').raises(Fog::Linode::NotFound) do
Linode[:linode].linode_reboot(0)
end

tests('#linode_delete(0)').raises(Fog::Linode::NotFound) do
Linode[:linode].linode_delete(0)
end

end

end
37 changes: 37 additions & 0 deletions tests/linode/requests/linodeplans_tests.rb
@@ -0,0 +1,37 @@
Shindo.tests('Linode | linodeplans requests', ['linode']) do

@linodeplans_format = Linode::Formats::BASIC.merge({
'DATA' => [{
'AVAIL' => {
'2' => Integer,
'3' => Integer,
'4' => Integer,
'6' => Integer,
'7' => Integer
},
'DISK' => Integer,
'PLANID' => Integer,
'PRICE' => Float,
'RAM' => Integer,
'LABEL' => String,
'XFER' => Integer
}]
})

tests('success') do

@linodeplan_id = nil

tests('#avail_linodeplans').formats(@linodeplans_format) do
data = Linode[:linode].avail_linodeplans.body
@linodeplan_id = data['DATA'].first['PLANID']
data
end

tests("#avail_linodeplans(#{@linodeplan_id})").formats(@linodeplans_format) do
Linode[:linode].avail_linodeplans(@linodeplan_id).body
end

end

end
2 changes: 1 addition & 1 deletion tests/slicehost/requests/slice_tests.rb
Expand Up @@ -54,7 +54,7 @@
Slicehost[:slices].reboot_slice(0)
end

tests('delete_slice(0)').raises(Fog::Slicehost::NotFound) do
tests('#delete_slice(0)').raises(Fog::Slicehost::NotFound) do
Slicehost[:slices].delete_slice(0)
end

Expand Down

0 comments on commit 5ae1a79

Please sign in to comment.