Class: Deltacloud::Library

Inherits:
Object
  • Object
show all
Includes:
Helpers::Drivers
Defined in:
lib/deltacloud/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Drivers

#driver, #driver_class, #driver_class_name, #driver_name, #driver_source_name, #driver_symbol, included, #provider_name

Constructor Details

#initialize(driver_name, opts = {}) {|backend| ... } ⇒ Library

Returns a new instance of Library.

Yields:



80
81
82
83
84
85
86
87
88
# File 'lib/deltacloud/api.rb', line 80

def initialize(driver_name, opts={}, &block)
  Thread.current[:driver] = driver_name.to_s
  Thread.current[:provider] = opts[:provider]
  @backend = driver
  opts[:user] ||= 'mockuser'
  opts[:password] ||= 'mockpassword'
  @credentials = OpenStruct.new(:user => opts[:user], :password => opts[:password])
  yield backend if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
# File 'lib/deltacloud/api.rb', line 114

def method_missing(name, *args)
  return super unless backend.respond_to? name
  begin
    params = ([@credentials] + args).flatten
    backend.send(name, *params)
  rescue ArgumentError => e
    puts "[W] Wrong arguments for #{name}. Omitting credentials injection. (#{e.message})"
    backend.send(name, *args)
  end
end

Instance Attribute Details

#backendObject (readonly)

Returns the value of attribute backend.



78
79
80
# File 'lib/deltacloud/api.rb', line 78

def backend
  @backend
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



78
79
80
# File 'lib/deltacloud/api.rb', line 78

def credentials
  @credentials
end

Instance Method Details

#current_driverObject



98
99
100
# File 'lib/deltacloud/api.rb', line 98

def current_driver
  Thread.current[:driver]
end

#current_providerObject



94
95
96
# File 'lib/deltacloud/api.rb', line 94

def current_provider
  Thread.current[:provider]
end

#provider(opts = {}) ⇒ Object



110
111
112
# File 'lib/deltacloud/api.rb', line 110

def provider(opts={})
  providers.find { |p| p.id == opts[:id] }
end

#providersObject



102
103
104
105
106
107
108
# File 'lib/deltacloud/api.rb', line 102

def providers
  if backend.respond_to? :providers
    backend.providers(@credentials)
  else
    Deltacloud.drivers[current_driver.to_sym]
  end
end

#versionObject



90
91
92
# File 'lib/deltacloud/api.rb', line 90

def version
  Deltacloud::API_VERSION
end