From 3de8f5c9c4ef742d60786c0c221be09a52699aee Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 24 May 2013 17:29:12 +0800 Subject: [PATCH] [stormondemand|billing] Add Billing service and related APIs --- lib/fog/billing.rb | 23 +++++++ lib/fog/storm_on_demand.rb | 1 + lib/fog/storm_on_demand/billing.rb | 61 +++++++++++++++++++ .../storm_on_demand/models/billing/invoice.rb | 28 +++++++++ .../models/billing/invoices.rb | 30 +++++++++ .../storm_on_demand/models/billing/payment.rb | 17 ++++++ .../models/billing/payments.rb | 19 ++++++ .../requests/billing/get_invoice.rb | 16 +++++ .../requests/billing/list_invoices.rb | 16 +++++ .../requests/billing/make_payment.rb | 16 +++++ .../requests/billing/next_invoice.rb | 16 +++++ 11 files changed, 243 insertions(+) create mode 100644 lib/fog/billing.rb create mode 100644 lib/fog/storm_on_demand/billing.rb create mode 100644 lib/fog/storm_on_demand/models/billing/invoice.rb create mode 100644 lib/fog/storm_on_demand/models/billing/invoices.rb create mode 100644 lib/fog/storm_on_demand/models/billing/payment.rb create mode 100644 lib/fog/storm_on_demand/models/billing/payments.rb create mode 100644 lib/fog/storm_on_demand/requests/billing/get_invoice.rb create mode 100644 lib/fog/storm_on_demand/requests/billing/list_invoices.rb create mode 100644 lib/fog/storm_on_demand/requests/billing/make_payment.rb create mode 100644 lib/fog/storm_on_demand/requests/billing/next_invoice.rb diff --git a/lib/fog/billing.rb b/lib/fog/billing.rb new file mode 100644 index 000000000..761a0e57e --- /dev/null +++ b/lib/fog/billing.rb @@ -0,0 +1,23 @@ +module Fog + module Billing + + def self.[](provider) + self.new(:provider => provider) + end + + def self.new(attributes) + attributes = attributes.dup + provider = attributes.delete(:provider).to_s.downcase.to_sym + if provider == :stormondemand + require 'fog/storm_on_demand/billing' + Fog::Billing::StormOnDemand.new(attributes) + else + raise ArgumentError.new("#{provider} has no billing service") + end + end + + def self.providers + Fog.services[:billing] + end + end +end diff --git a/lib/fog/storm_on_demand.rb b/lib/fog/storm_on_demand.rb index 98a44e87f..ea21cef78 100644 --- a/lib/fog/storm_on_demand.rb +++ b/lib/fog/storm_on_demand.rb @@ -8,6 +8,7 @@ module Fog service(:compute, 'storm_on_demand/compute', 'Compute') service(:storage, 'storm_on_demand/storage', 'Storage') service(:dns, 'storm_on_demand/dns', 'DNS') + service(:billing, 'storm_on_demand/billing', 'Billing') end end diff --git a/lib/fog/storm_on_demand/billing.rb b/lib/fog/storm_on_demand/billing.rb new file mode 100644 index 000000000..d24cc315e --- /dev/null +++ b/lib/fog/storm_on_demand/billing.rb @@ -0,0 +1,61 @@ +require 'fog/billing' +require 'fog/storm_on_demand' +require 'fog/storm_on_demand/shared' + +module Fog + module Billing + class StormOnDemand < Fog::Service + + requires :storm_on_demand_username, :storm_on_demand_password + recognizes :storm_on_demand_auth_url + + model_path 'fog/storm_on_demand/models/billing' + model :invoice + collection :invoices + model :payment + collection :payments + + request_path 'fog/storm_on_demand/requests/billing' + request :list_invoices + request :get_invoice + request :next_invoice + request :make_payment + + 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 + + include Fog::StormOnDemand::RealShared + + end + end + end +end \ No newline at end of file diff --git a/lib/fog/storm_on_demand/models/billing/invoice.rb b/lib/fog/storm_on_demand/models/billing/invoice.rb new file mode 100644 index 000000000..6d39cc54e --- /dev/null +++ b/lib/fog/storm_on_demand/models/billing/invoice.rb @@ -0,0 +1,28 @@ +require 'fog/core/model' + +module Fog + module Billing + class StormOnDemand + + class Invoice < Fog::Model + identity :id + attribute :accnt + attribute :bill_date + attribute :due + attribute :end_date + attribute :lineitem_groups + attribute :payments + attribute :start_date + attribute :status + attribute :total + attribute :type + + def initialize(attributes={}) + super + end + + end + + end + end +end diff --git a/lib/fog/storm_on_demand/models/billing/invoices.rb b/lib/fog/storm_on_demand/models/billing/invoices.rb new file mode 100644 index 000000000..f62b7662f --- /dev/null +++ b/lib/fog/storm_on_demand/models/billing/invoices.rb @@ -0,0 +1,30 @@ +require 'fog/core/collection' +require 'fog/storm_on_demand/models/billing/invoice' + +module Fog + module Billing + class StormOnDemand + + class Invoices < Fog::Collection + model Fog::Billing::StormOnDemand::Invoice + + def all(options={}) + invoices = service.list_invoices(options).body['items'] + load(invoices) + end + + def get(invoice_id) + invoice = service.get_invoice(:id => invoice_id).body + new(invoice) + end + + def next + invoice = service.next_invoice.body + new(invoice) + end + + end + + end + end +end diff --git a/lib/fog/storm_on_demand/models/billing/payment.rb b/lib/fog/storm_on_demand/models/billing/payment.rb new file mode 100644 index 000000000..d9d041e30 --- /dev/null +++ b/lib/fog/storm_on_demand/models/billing/payment.rb @@ -0,0 +1,17 @@ +require 'fog/core/model' + +module Fog + module Billing + class StormOnDemand + + class Payment < Fog::Model + + def initialize(attributes={}) + super + end + + end + + end + end +end diff --git a/lib/fog/storm_on_demand/models/billing/payments.rb b/lib/fog/storm_on_demand/models/billing/payments.rb new file mode 100644 index 000000000..230c69d54 --- /dev/null +++ b/lib/fog/storm_on_demand/models/billing/payments.rb @@ -0,0 +1,19 @@ +require 'fog/core/collection' +require 'fog/storm_on_demand/models/billing/payment' + +module Fog + module Billing + class StormOnDemand + + class Payments < Fog::Collection + model Fog::Billing::StormOnDemand::Payment + + def make(amount, card_code) + service.make_payment(:amount => amount, + :card_code => card_code).body['amount'] + end + end + + end + end +end diff --git a/lib/fog/storm_on_demand/requests/billing/get_invoice.rb b/lib/fog/storm_on_demand/requests/billing/get_invoice.rb new file mode 100644 index 000000000..952bcf13e --- /dev/null +++ b/lib/fog/storm_on_demand/requests/billing/get_invoice.rb @@ -0,0 +1,16 @@ +module Fog + module Billing + class StormOnDemand + class Real + + def get_invoice(options={}) + request( + :path => '/Billing/Invoice/details', + :body => Fog::JSON.encode(:params => options) + ) + end + + end + end + end +end diff --git a/lib/fog/storm_on_demand/requests/billing/list_invoices.rb b/lib/fog/storm_on_demand/requests/billing/list_invoices.rb new file mode 100644 index 000000000..ae2b54f6d --- /dev/null +++ b/lib/fog/storm_on_demand/requests/billing/list_invoices.rb @@ -0,0 +1,16 @@ +module Fog + module Billing + class StormOnDemand + class Real + + def list_invoices(options={}) + request( + :path => '/Billing/Invoice/list', + :body => Fog::JSON.encode(:params => options) + ) + end + + end + end + end +end diff --git a/lib/fog/storm_on_demand/requests/billing/make_payment.rb b/lib/fog/storm_on_demand/requests/billing/make_payment.rb new file mode 100644 index 000000000..4cd59a3ce --- /dev/null +++ b/lib/fog/storm_on_demand/requests/billing/make_payment.rb @@ -0,0 +1,16 @@ +module Fog + module Billing + class StormOnDemand + class Real + + def make_payment(options={}) + request( + :path => '/Billing/Payment/make', + :body => Fog::JSON.encode(:params => options) + ) + end + + end + end + end +end diff --git a/lib/fog/storm_on_demand/requests/billing/next_invoice.rb b/lib/fog/storm_on_demand/requests/billing/next_invoice.rb new file mode 100644 index 000000000..f126e03b9 --- /dev/null +++ b/lib/fog/storm_on_demand/requests/billing/next_invoice.rb @@ -0,0 +1,16 @@ +module Fog + module Billing + class StormOnDemand + class Real + + def next_invoice(options={}) + request( + :path => '/Billing/Invoice/next', + :body => Fog::JSON.encode(:params => options) + ) + end + + end + end + end +end