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

[linode] add basics and first few calls

This commit is contained in:
geemus 2010-07-03 10:48:13 -05:00
parent aba6a6af87
commit 7f1c6d07e3
8 changed files with 233 additions and 1 deletions

View file

@ -29,6 +29,7 @@ require 'fog/ssh'
require 'fog/aws'
require 'fog/bluebox'
require 'fog/linode'
require 'fog/local'
require 'fog/new_servers'
require 'fog/rackspace'

View file

@ -1,6 +1,7 @@
require 'fog/credentials'
require 'fog/aws/bin'
require 'fog/linode/bin'
require 'fog/local/bin'
require 'fog/new_servers/bin'
require 'fog/rackspace/bin'
@ -14,7 +15,7 @@ module Fog
def services
services = []
[::AWS, ::Local, ::NewServers, ::Rackspace, ::Slicehost, ::Terremark, ::Vcloud, ::Bluebox].each do |service|
[::AWS, ::Linode, ::Local, ::NewServers, ::Rackspace, ::Slicehost, ::Terremark, ::Vcloud, ::Bluebox].each do |service|
if service.initialized?
services << service
end

75
lib/fog/linode.rb Normal file
View file

@ -0,0 +1,75 @@
module Fog
module Linode
extend Fog::Service
requires :linode_api_key
model_path 'fog/linode/models'
request_path 'fog/linode/requests'
request 'avail_datacenters'
request 'avail_distributions'
request 'avail_kernels'
request 'avail_linodeplans'
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)
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
unless response.body.empty?
response.body = JSON.parse(response.body)
end
response
end
end
end
end

30
lib/fog/linode/bin.rb Normal file
View file

@ -0,0 +1,30 @@
module Linode
class << self
if Fog.credentials[:linode_api_key]
def initialized?
true
end
def [](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
else
def initialized?
false
end
end
end
end

View file

@ -0,0 +1,29 @@
module Fog
module 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

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

View file

@ -0,0 +1,34 @@
module Fog
module 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(distribution_id)
Fog::Mock.not_implemented
end
end
end
end

View file

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