deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 15/26] Core: Added the Deltacloud library wrapper (require 'lib/deltacloud')
Date Tue, 15 May 2012 15:15:47 GMT
From: Michal Fojtik <mfojtik@redhat.com>

This will make possible to use Deltacloud drivers API with thirty-party
application whithout spawning a server.

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/deltacloud.rb |   64 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 54 insertions(+), 10 deletions(-)

diff --git a/server/lib/deltacloud.rb b/server/lib/deltacloud.rb
index 6ff547e..d64797d 100644
--- a/server/lib/deltacloud.rb
+++ b/server/lib/deltacloud.rb
@@ -1,4 +1,3 @@
-#
 # Licensed to the Apache Software Foundation (ASF) under one or more
 # contributor license agreements.  See the NOTICE file distributed with
 # this work for additional information regarding copyright ownership.  The
@@ -14,14 +13,59 @@
 # License for the specific language governing permissions and limitations
 # under the License.
 
-require 'deltacloud/drivers'
+require 'rubygems'
+
+unless Kernel.respond_to?(:require_relative)
+  module Kernel
+    def require_relative(path)
+      require File.join(File.dirname(caller[0]), path.to_str)
+    end
+  end
+end
+
+require 'ostruct'
+
+require_relative 'deltacloud/core_ext/string'
+require_relative 'deltacloud/core_ext/array'
+require_relative 'deltacloud/core_ext/hash'
+require_relative 'deltacloud/core_ext/integer'
+require_relative 'deltacloud/core_ext/proc'
+require_relative 'deltacloud/models'
+require_relative 'deltacloud/drivers'
+require_relative 'deltacloud/helpers/driver_helper'
+
+module Deltacloud
+
+  def self.drivers
+    Drivers.driver_config
+  end
+
+  class Library
+    include Helpers::Drivers
+    attr_reader :backend, :credentials
+
+    def initialize(driver_name, opts={}, &block)
+      Thread.current[:driver] = driver_name.to_s
+      Thread.current[:provider] = opts[:provider] if opts[:provider]
+      @backend = driver
+      @credentials = OpenStruct.new(:user => opts[:user], :password => opts[:password])
+      yield self if block_given?
+    end
+
+    def method_missing(name, *args)
+      return super unless backend.respond_to? name
+      begin
+        params = ([@credentials] + args).flatten
+        backend.send(name, *params)
+      rescue ArgumentError
+        backend.send(name, *args)
+      end
+    end
+
+  end
 
-require 'deltacloud/core_ext'
+  def self.new(driver_name, opts={}, &block)
+    Library.new(driver_name, opts, &block)
+  end
 
-require 'deltacloud/base_driver'
-require 'deltacloud/hardware_profile'
-require 'deltacloud/state_machine'
-require 'deltacloud/helpers'
-require 'deltacloud/models'
-require 'deltacloud/validation'
-require 'deltacloud/runner'
+end
-- 
1.7.10.1


Mime
View raw message