1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

[linode] consistency between providers/services

This commit is contained in:
geemus 2010-09-08 10:56:38 -07:00
parent b773f929b4
commit fb34c07940
21 changed files with 423 additions and 384 deletions

View file

@ -1,79 +1,19 @@
module Fog
class Linode < Fog::Service
module Linode
requires :linode_api_key
extend Fog::Provider
model_path 'fog/linode/models'
request_path 'fog/linode/requests'
request :avail_datacenters
request :avail_distributions
request :avail_kernels
request :avail_linodeplans
request :avail_stackscripts
request :linode_create
request :linode_delete
request :linode_list
request :linode_reboot
class Mock
include Collections
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
@linode_api_key = options[:linode_api_key]
@data = self.class.data[@linode_api_key]
end
service_path 'fog/linode'
service 'compute'
def self.new(attributes = {})
location = caller.first
warning = "[yellow][WARN] Fog::Linode#new is deprecated, use Fog::Linode::Compute#new instead[/]"
warning << " [light_black](" << location << ")[/] "
Formatador.display_line(warning)
Fog::Linode::Compute.new(attributes)
end
class Real
include Collections
def initialize(options={})
@linode_api_key = options[:linode_api_key]
@host = options[:host] || "api.linode.com"
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@connection.reset
end
def request(params)
params[:query] ||= {}
params[:query].merge!(:api_key => @linode_api_key)
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
end
end
end

View file

@ -9,15 +9,15 @@ module Linode
def [](service)
@@connections ||= Hash.new do |hash, key|
hash[key] = case key
when :linode
Fog::Linode.new
when :compute
Fog::Linode::Compute.new
end
end
@@connections[service]
end
def services
[:linode]
[:compute]
end
else

81
lib/fog/linode/compute.rb Normal file
View file

@ -0,0 +1,81 @@
module Fog
module Linode
class Compute < Fog::Service
requires :linode_api_key
model_path 'fog/linode/models/compute'
request_path 'fog/linode/requests/compute'
request :avail_datacenters
request :avail_distributions
request :avail_kernels
request :avail_linodeplans
request :avail_stackscripts
request :linode_create
request :linode_delete
request :linode_list
request :linode_reboot
class Mock
include Collections
def self.data
@data ||= Hash.new do |hash, key|
hash[key] = {}
end
end
def self.reset_data(keys=data.keys)
for key in [*keys]
data.delete(key)
end
end
def initialize(options={})
@linode_api_key = options[:linode_api_key]
@data = self.class.data[@linode_api_key]
end
end
class Real
include Collections
def initialize(options={})
@linode_api_key = options[:linode_api_key]
@host = options[:host] || "api.linode.com"
@port = options[:port] || 443
@scheme = options[:scheme] || 'https'
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", options[:persistent])
end
def reload
@connection.reset
end
def request(params)
params[:query] ||= {}
params[:query].merge!(:api_key => @linode_api_key)
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
end
end
end
end

View file

@ -1,29 +0,0 @@
module Fog
class Linode
class Real
# Get available data centers
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_datacenters
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.datacenters' }
)
end
end
class Mock
def avail_datacenters
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,36 +0,0 @@
module Fog
class Linode
class Real
# Get available distributions
#
# ==== Parameters
# * distributionId<~Integer>: id to limit results to
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_distributions(distribution_id=nil)
options = {}
if distribution_id
options.merge!(:distributionId => distribution_id)
end
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.distributions' }.merge!(options)
)
end
end
class Mock
def avail_distributions(options={})
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,34 +0,0 @@
module Fog
class Linode
class Real
# Get available kernels
#
# ==== Parameters
# * options<~Hash>:
# * kernelId<~Integer>: id to limit results to
# * isXen<~Integer>: if 1 limits results to only zen
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_kernels(options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.kernels' }.merge!(options)
)
end
end
class Mock
def avail_kernels(options={})
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,33 +0,0 @@
module Fog
class Linode
class Real
# Get available plans
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
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' }.merge!(options)
)
end
end
class Mock
def avail_linodeplans
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,35 +0,0 @@
module Fog
class Linode
class Real
# Get available stack scripts
#
# ==== Parameters
# * options<~Hash>:
# * distributionId<~Integer>: Limit the results to Stackscripts that can be applied to this distribution id
# * distributionVendor<~String>: Debian, Ubuntu, Fedora, etc.
# * keywords<~String>: Search terms
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_stackscripts(options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.stackscripts' }.merge!(options)
)
end
end
class Mock
def avail_stackscripts(options={})
Fog::Mock.not_implemented
end
end
end
end

View file

@ -0,0 +1,31 @@
module Fog
module Linode
class Compute
class Real
# Get available data centers
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_datacenters
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.datacenters' }
)
end
end
class Mock
def avail_datacenters
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,38 @@
module Fog
module Linode
class Compute
class Real
# Get available distributions
#
# ==== Parameters
# * distributionId<~Integer>: id to limit results to
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_distributions(distribution_id=nil)
options = {}
if distribution_id
options.merge!(:distributionId => distribution_id)
end
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.distributions' }.merge!(options)
)
end
end
class Mock
def avail_distributions(options={})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,36 @@
module Fog
module Linode
class Compute
class Real
# Get available kernels
#
# ==== Parameters
# * options<~Hash>:
# * kernelId<~Integer>: id to limit results to
# * isXen<~Integer>: if 1 limits results to only zen
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_kernels(options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.kernels' }.merge!(options)
)
end
end
class Mock
def avail_kernels(options={})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,35 @@
module Fog
module Linode
class Compute
class Real
# Get available plans
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
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' }.merge!(options)
)
end
end
class Mock
def avail_linodeplans
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,37 @@
module Fog
module Linode
class Compute
class Real
# Get available stack scripts
#
# ==== Parameters
# * options<~Hash>:
# * distributionId<~Integer>: Limit the results to Stackscripts that can be applied to this distribution id
# * distributionVendor<~String>: Debian, Ubuntu, Fedora, etc.
# * keywords<~String>: Search terms
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def avail_stackscripts(options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'avail.stackscripts' }.merge!(options)
)
end
end
class Mock
def avail_stackscripts(options={})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,41 @@
module Fog
module Linode
class Compute
class Real
# Creates a linode and assigns you full privileges
#
# ==== Parameters
# * datacenter_id<~Integer>: id of datacenter to place new linode in
# * payment_term<~Integer>: Subscription term in months, in [1, 12, 24]
# * plan_id<~Integer>: id of plan to boot new linode with
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_create(datacenter_id, payment_term, plan_id)
request(
:expects => 200,
:method => 'GET',
:query => {
:api_action => 'linode.create',
:datacenterId => datacenter_id,
:paymentTerm => payment_term,
:planId => plan_id
}
)
end
end
class Mock
def linode_create(datacenter_id, payment_term, plan_id)
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,36 @@
module Fog
module Linode
class Compute
class Real
# List all linodes user has access or delete to
#
# ==== Parameters
# * linode_id<~Integer>: id of linode to delete
# * options<~Hash>:
# * skipChecks<~Boolean>: skips safety checks and always deletes
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_delete(linode_id, options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'linode.delete', :linodeId => linode_id }.merge!(options)
)
end
end
class Mock
def linode_delete(linode_id, options={})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,38 @@
module Fog
module Linode
class Compute
class Real
# List all linodes user has access or delete to
#
# ==== Parameters
# * linodeId<~Integer>: Limit the list to the specified LinodeID
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_list(linode_id=nil)
options = {}
if linode_id
options.merge!(:linodeId => linode_id)
end
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'linode.list' }.merge!(options)
)
end
end
class Mock
def linode_list(options={})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -0,0 +1,36 @@
module Fog
module Linode
class Compute
class Real
# Issues a shutdown, and then a boot job for a given linode
#
# ==== Parameters
# * linode_id<~Integer>: id of linode to reboot
# * options<~Hash>:
# * configId<~Boolean>: id of config to boot server with
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_reboot(linode_id, options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'linode.reboot', :linodeId => linode_id }.merge!(options)
)
end
end
class Mock
def linode_reboot(linode_id, options={})
Fog::Mock.not_implemented
end
end
end
end
end

View file

@ -1,39 +0,0 @@
module Fog
class Linode
class Real
# Creates a linode and assigns you full privileges
#
# ==== Parameters
# * datacenter_id<~Integer>: id of datacenter to place new linode in
# * payment_term<~Integer>: Subscription term in months, in [1, 12, 24]
# * plan_id<~Integer>: id of plan to boot new linode with
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_create(datacenter_id, payment_term, plan_id)
request(
:expects => 200,
:method => 'GET',
:query => {
:api_action => 'linode.create',
:datacenterId => datacenter_id,
:paymentTerm => payment_term,
:planId => plan_id
}
)
end
end
class Mock
def linode_create(datacenter_id, payment_term, plan_id)
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,34 +0,0 @@
module Fog
class Linode
class Real
# List all linodes user has access or delete to
#
# ==== Parameters
# * linode_id<~Integer>: id of linode to delete
# * options<~Hash>:
# * skipChecks<~Boolean>: skips safety checks and always deletes
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_delete(linode_id, options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'linode.delete', :linodeId => linode_id }.merge!(options)
)
end
end
class Mock
def linode_delete(linode_id, options={})
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,36 +0,0 @@
module Fog
class Linode
class Real
# List all linodes user has access or delete to
#
# ==== Parameters
# * linodeId<~Integer>: Limit the list to the specified LinodeID
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_list(linode_id=nil)
options = {}
if linode_id
options.merge!(:linodeId => linode_id)
end
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'linode.list' }.merge!(options)
)
end
end
class Mock
def linode_list(options={})
Fog::Mock.not_implemented
end
end
end
end

View file

@ -1,34 +0,0 @@
module Fog
class Linode
class Real
# Issues a shutdown, and then a boot job for a given linode
#
# ==== Parameters
# * linode_id<~Integer>: id of linode to reboot
# * options<~Hash>:
# * configId<~Boolean>: id of config to boot server with
#
# ==== Returns
# * response<~Excon::Response>:
# * body<~Array>:
# TODO: docs
def linode_reboot(linode_id, options={})
request(
:expects => 200,
:method => 'GET',
:query => { :api_action => 'linode.reboot', :linodeId => linode_id }.merge!(options)
)
end
end
class Mock
def linode_reboot(linode_id, options={})
Fog::Mock.not_implemented
end
end
end
end