From 7f1c6d07e343ce14db28d97fc9166ad44d8dd762 Mon Sep 17 00:00:00 2001 From: geemus Date: Sat, 3 Jul 2010 10:48:13 -0500 Subject: [PATCH] [linode] add basics and first few calls --- lib/fog.rb | 1 + lib/fog/bin.rb | 3 +- lib/fog/linode.rb | 75 +++++++++++++++++++ lib/fog/linode/bin.rb | 30 ++++++++ lib/fog/linode/requests/avail_datacenters.rb | 29 +++++++ .../linode/requests/avail_distributions.rb | 33 ++++++++ lib/fog/linode/requests/avail_kernels.rb | 34 +++++++++ lib/fog/linode/requests/avail_linodeplans.rb | 29 +++++++ 8 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 lib/fog/linode.rb create mode 100644 lib/fog/linode/bin.rb create mode 100644 lib/fog/linode/requests/avail_datacenters.rb create mode 100644 lib/fog/linode/requests/avail_distributions.rb create mode 100644 lib/fog/linode/requests/avail_kernels.rb create mode 100644 lib/fog/linode/requests/avail_linodeplans.rb diff --git a/lib/fog.rb b/lib/fog.rb index 11789d902..79d8b53e4 100644 --- a/lib/fog.rb +++ b/lib/fog.rb @@ -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' diff --git a/lib/fog/bin.rb b/lib/fog/bin.rb index e8ae4bcad..63673b0ce 100644 --- a/lib/fog/bin.rb +++ b/lib/fog/bin.rb @@ -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 diff --git a/lib/fog/linode.rb b/lib/fog/linode.rb new file mode 100644 index 000000000..3ee767803 --- /dev/null +++ b/lib/fog/linode.rb @@ -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 diff --git a/lib/fog/linode/bin.rb b/lib/fog/linode/bin.rb new file mode 100644 index 000000000..751016482 --- /dev/null +++ b/lib/fog/linode/bin.rb @@ -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 diff --git a/lib/fog/linode/requests/avail_datacenters.rb b/lib/fog/linode/requests/avail_datacenters.rb new file mode 100644 index 000000000..20527fb79 --- /dev/null +++ b/lib/fog/linode/requests/avail_datacenters.rb @@ -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 diff --git a/lib/fog/linode/requests/avail_distributions.rb b/lib/fog/linode/requests/avail_distributions.rb new file mode 100644 index 000000000..cc59d55c7 --- /dev/null +++ b/lib/fog/linode/requests/avail_distributions.rb @@ -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 diff --git a/lib/fog/linode/requests/avail_kernels.rb b/lib/fog/linode/requests/avail_kernels.rb new file mode 100644 index 000000000..460cd9098 --- /dev/null +++ b/lib/fog/linode/requests/avail_kernels.rb @@ -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 diff --git a/lib/fog/linode/requests/avail_linodeplans.rb b/lib/fog/linode/requests/avail_linodeplans.rb new file mode 100644 index 000000000..02c2b4144 --- /dev/null +++ b/lib/fog/linode/requests/avail_linodeplans.rb @@ -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