From 89c569fee24233b38c7717c508dec035ff49a015 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 23 May 2013 07:35:02 +0800 Subject: [PATCH] [stormondemand|storage] Add Storage service and cluster API --- lib/fog/storage.rb | 3 + lib/fog/storm_on_demand.rb | 3 +- .../storm_on_demand/models/storage/cluster.rb | 20 ++++ .../models/storage/clusters.rb | 20 ++++ .../requests/storage/list_clusters.rb | 16 +++ lib/fog/storm_on_demand/storage.rb | 104 ++++++++++++++++++ 6 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 lib/fog/storm_on_demand/models/storage/cluster.rb create mode 100644 lib/fog/storm_on_demand/models/storage/clusters.rb create mode 100644 lib/fog/storm_on_demand/requests/storage/list_clusters.rb create mode 100644 lib/fog/storm_on_demand/storage.rb diff --git a/lib/fog/storage.rb b/lib/fog/storage.rb index c8562d4c0..fefda43ea 100644 --- a/lib/fog/storage.rb +++ b/lib/fog/storage.rb @@ -11,6 +11,9 @@ module Fog when :internetarchive require 'fog/internet_archive/storage' Fog::Storage::InternetArchive.new(attributes) + when :stormondemand + require 'fog/storm_on_demand/storage' + Fog::Storage::StormOnDemand.new(attributes) else if self.providers.include?(provider) require "fog/#{provider}/storage" diff --git a/lib/fog/storm_on_demand.rb b/lib/fog/storm_on_demand.rb index c710bb719..9c3b9255e 100644 --- a/lib/fog/storm_on_demand.rb +++ b/lib/fog/storm_on_demand.rb @@ -6,7 +6,8 @@ module Fog extend Fog::Provider service(:compute, 'storm_on_demand/compute', 'Compute') - + service(:storage, 'storm_on_demand/storage', 'Storage') + end end diff --git a/lib/fog/storm_on_demand/models/storage/cluster.rb b/lib/fog/storm_on_demand/models/storage/cluster.rb new file mode 100644 index 000000000..890daffc5 --- /dev/null +++ b/lib/fog/storm_on_demand/models/storage/cluster.rb @@ -0,0 +1,20 @@ +require 'fog/core/model' + +module Fog + module Storage + class StormOnDemand + + class Cluster < Fog::Model + identity :id + attribute :description + attribute :zone + + def initialize(attributes={}) + super + end + + end + + end + end +end diff --git a/lib/fog/storm_on_demand/models/storage/clusters.rb b/lib/fog/storm_on_demand/models/storage/clusters.rb new file mode 100644 index 000000000..066a1827b --- /dev/null +++ b/lib/fog/storm_on_demand/models/storage/clusters.rb @@ -0,0 +1,20 @@ +require 'fog/core/collection' +require 'fog/storm_on_demand/models/storage/cluster' + +module Fog + module Storage + class StormOnDemand + + class Clusters < Fog::Collection + model Fog::Storage::StormOnDemand::Cluster + + def all(options={}) + data = service.list_clusters(options).body['items'] + load(data) + end + + end + + end + end +end diff --git a/lib/fog/storm_on_demand/requests/storage/list_clusters.rb b/lib/fog/storm_on_demand/requests/storage/list_clusters.rb new file mode 100644 index 000000000..385dddc80 --- /dev/null +++ b/lib/fog/storm_on_demand/requests/storage/list_clusters.rb @@ -0,0 +1,16 @@ +module Fog + module Storage + class StormOnDemand + class Real + + def list_clusters(options={}) + request( + :path => '/Storage/Block/Cluster/list', + :body => Fog::JSON.encode(:params => options) + ) + end + + end + end + end +end diff --git a/lib/fog/storm_on_demand/storage.rb b/lib/fog/storm_on_demand/storage.rb new file mode 100644 index 000000000..eb280e902 --- /dev/null +++ b/lib/fog/storm_on_demand/storage.rb @@ -0,0 +1,104 @@ +require "fog/storm_on_demand" +require "fog/storage" + +module Fog + module Storage + class StormOnDemand < Fog::Service + + API_URL = 'https://api.stormondemand.com' + API_VERSION = 'v1' + + requires :storm_on_demand_username, :storm_on_demand_password + recognizes :storm_on_demand_auth_url + + model_path 'fog/storm_on_demand/models/storage' + model :cluster + collection :clusters + + request_path 'fog/storm_on_demand/requests/storage' + request :list_clusters + + + class Mock + + def self.data + @data ||= Hash.new + end + + def self.reset + @data = nil + end + + def self.reset_data(keys=data.keys) + for key in [*keys] + data.delete(key) + end + end + + def initialize(options={}) + @storm_on_demand_username = options[:storm_on_demand_username] + end + + def data + self.class.data[@storm_on_demand_username] + end + + def reset_data + self.class.data.delete(@storm_on_demand_username) + end + + end + + class Real + + def initialize(options={}) + uri = URI.parse(options[:storm_on_demand_auth_url] ||= API_URL) + @connection_options = options[:connection_options] || {} + @host = uri.host + @path = uri.path + @persistent = options[:persistent] || false + @port = uri.port + @scheme = uri.scheme + @storm_on_demand_username = options[:storm_on_demand_username] + @storm_on_demand_password = options[:storm_on_demand_password] + @connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options) + end + + def reload + @connection.reset + end + + def request(params) + begin + response = @connection.request(params.merge!({ + :headers => { + 'Content-Type' => 'application/json', + 'Authorization' => 'Basic ' << Base64.encode64("#{@storm_on_demand_username}:#{@storm_on_demand_password}").chomp + }.merge!(params[:headers] || {}), + :host => @host, + :path => "#{@path}/#{API_VERSION}#{params[:path]}", + :expects => 200, + :method => :post + })) + puts response.body + rescue Excon::Errors::HTTPStatusError => error + raise case error + when Excon::Errors::NotFound + Fog::StormOnDemand::Compute::NotFound.slurp(error) + else + error + end + end + unless response.body.empty? + response.body = Fog::JSON.decode(response.body) + end + if response.body.has_key?('full_error') + raise(Fog::Compute::StormOnDemand::Error, response.body.inspect) + end + response + end + + end + end + end +end