From fb34c07940fb30a74868c9fa4b763d0ed5c900ab Mon Sep 17 00:00:00 2001 From: geemus Date: Wed, 8 Sep 2010 10:56:38 -0700 Subject: [PATCH] [linode] consistency between providers/services --- lib/fog/linode.rb | 82 +++---------------- lib/fog/linode/bin.rb | 6 +- lib/fog/linode/compute.rb | 81 ++++++++++++++++++ lib/fog/linode/requests/avail_datacenters.rb | 29 ------- .../linode/requests/avail_distributions.rb | 36 -------- lib/fog/linode/requests/avail_kernels.rb | 34 -------- lib/fog/linode/requests/avail_linodeplans.rb | 33 -------- lib/fog/linode/requests/avail_stackscripts.rb | 35 -------- .../requests/compute/avail_datacenters.rb | 31 +++++++ .../requests/compute/avail_distributions.rb | 38 +++++++++ .../linode/requests/compute/avail_kernels.rb | 36 ++++++++ .../requests/compute/avail_linodeplans.rb | 35 ++++++++ .../requests/compute/avail_stackscripts.rb | 37 +++++++++ .../linode/requests/compute/linode_create.rb | 41 ++++++++++ .../linode/requests/compute/linode_delete.rb | 36 ++++++++ .../linode/requests/compute/linode_list.rb | 38 +++++++++ .../linode/requests/compute/linode_reboot.rb | 36 ++++++++ lib/fog/linode/requests/linode_create.rb | 39 --------- lib/fog/linode/requests/linode_delete.rb | 34 -------- lib/fog/linode/requests/linode_list.rb | 36 -------- lib/fog/linode/requests/linode_reboot.rb | 34 -------- 21 files changed, 423 insertions(+), 384 deletions(-) create mode 100644 lib/fog/linode/compute.rb delete mode 100644 lib/fog/linode/requests/avail_datacenters.rb delete mode 100644 lib/fog/linode/requests/avail_distributions.rb delete mode 100644 lib/fog/linode/requests/avail_kernels.rb delete mode 100644 lib/fog/linode/requests/avail_linodeplans.rb delete mode 100644 lib/fog/linode/requests/avail_stackscripts.rb create mode 100644 lib/fog/linode/requests/compute/avail_datacenters.rb create mode 100644 lib/fog/linode/requests/compute/avail_distributions.rb create mode 100644 lib/fog/linode/requests/compute/avail_kernels.rb create mode 100644 lib/fog/linode/requests/compute/avail_linodeplans.rb create mode 100644 lib/fog/linode/requests/compute/avail_stackscripts.rb create mode 100644 lib/fog/linode/requests/compute/linode_create.rb create mode 100644 lib/fog/linode/requests/compute/linode_delete.rb create mode 100644 lib/fog/linode/requests/compute/linode_list.rb create mode 100644 lib/fog/linode/requests/compute/linode_reboot.rb delete mode 100644 lib/fog/linode/requests/linode_create.rb delete mode 100644 lib/fog/linode/requests/linode_delete.rb delete mode 100644 lib/fog/linode/requests/linode_list.rb delete mode 100644 lib/fog/linode/requests/linode_reboot.rb diff --git a/lib/fog/linode.rb b/lib/fog/linode.rb index d15232693..48eda5890 100644 --- a/lib/fog/linode.rb +++ b/lib/fog/linode.rb @@ -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 + diff --git a/lib/fog/linode/bin.rb b/lib/fog/linode/bin.rb index 506428142..e41494172 100644 --- a/lib/fog/linode/bin.rb +++ b/lib/fog/linode/bin.rb @@ -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 diff --git a/lib/fog/linode/compute.rb b/lib/fog/linode/compute.rb new file mode 100644 index 000000000..828a8cab0 --- /dev/null +++ b/lib/fog/linode/compute.rb @@ -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 diff --git a/lib/fog/linode/requests/avail_datacenters.rb b/lib/fog/linode/requests/avail_datacenters.rb deleted file mode 100644 index 38ce1f957..000000000 --- a/lib/fog/linode/requests/avail_datacenters.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/avail_distributions.rb b/lib/fog/linode/requests/avail_distributions.rb deleted file mode 100644 index b6ab4e92d..000000000 --- a/lib/fog/linode/requests/avail_distributions.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/avail_kernels.rb b/lib/fog/linode/requests/avail_kernels.rb deleted file mode 100644 index 11dbcfd03..000000000 --- a/lib/fog/linode/requests/avail_kernels.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/avail_linodeplans.rb b/lib/fog/linode/requests/avail_linodeplans.rb deleted file mode 100644 index dd0750c3c..000000000 --- a/lib/fog/linode/requests/avail_linodeplans.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/avail_stackscripts.rb b/lib/fog/linode/requests/avail_stackscripts.rb deleted file mode 100644 index 3375f9887..000000000 --- a/lib/fog/linode/requests/avail_stackscripts.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/compute/avail_datacenters.rb b/lib/fog/linode/requests/compute/avail_datacenters.rb new file mode 100644 index 000000000..451730380 --- /dev/null +++ b/lib/fog/linode/requests/compute/avail_datacenters.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/avail_distributions.rb b/lib/fog/linode/requests/compute/avail_distributions.rb new file mode 100644 index 000000000..d61e382fe --- /dev/null +++ b/lib/fog/linode/requests/compute/avail_distributions.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/avail_kernels.rb b/lib/fog/linode/requests/compute/avail_kernels.rb new file mode 100644 index 000000000..f402fd894 --- /dev/null +++ b/lib/fog/linode/requests/compute/avail_kernels.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/avail_linodeplans.rb b/lib/fog/linode/requests/compute/avail_linodeplans.rb new file mode 100644 index 000000000..55571cb2a --- /dev/null +++ b/lib/fog/linode/requests/compute/avail_linodeplans.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/avail_stackscripts.rb b/lib/fog/linode/requests/compute/avail_stackscripts.rb new file mode 100644 index 000000000..957059573 --- /dev/null +++ b/lib/fog/linode/requests/compute/avail_stackscripts.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/linode_create.rb b/lib/fog/linode/requests/compute/linode_create.rb new file mode 100644 index 000000000..4de8264e7 --- /dev/null +++ b/lib/fog/linode/requests/compute/linode_create.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/linode_delete.rb b/lib/fog/linode/requests/compute/linode_delete.rb new file mode 100644 index 000000000..84938cb57 --- /dev/null +++ b/lib/fog/linode/requests/compute/linode_delete.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/linode_list.rb b/lib/fog/linode/requests/compute/linode_list.rb new file mode 100644 index 000000000..3baf8d045 --- /dev/null +++ b/lib/fog/linode/requests/compute/linode_list.rb @@ -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 diff --git a/lib/fog/linode/requests/compute/linode_reboot.rb b/lib/fog/linode/requests/compute/linode_reboot.rb new file mode 100644 index 000000000..cc30baf76 --- /dev/null +++ b/lib/fog/linode/requests/compute/linode_reboot.rb @@ -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 diff --git a/lib/fog/linode/requests/linode_create.rb b/lib/fog/linode/requests/linode_create.rb deleted file mode 100644 index 4b3489c55..000000000 --- a/lib/fog/linode/requests/linode_create.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/linode_delete.rb b/lib/fog/linode/requests/linode_delete.rb deleted file mode 100644 index e3854b5da..000000000 --- a/lib/fog/linode/requests/linode_delete.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/linode_list.rb b/lib/fog/linode/requests/linode_list.rb deleted file mode 100644 index 64aef03a3..000000000 --- a/lib/fog/linode/requests/linode_list.rb +++ /dev/null @@ -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 diff --git a/lib/fog/linode/requests/linode_reboot.rb b/lib/fog/linode/requests/linode_reboot.rb deleted file mode 100644 index ee4d971cf..000000000 --- a/lib/fog/linode/requests/linode_reboot.rb +++ /dev/null @@ -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