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:



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

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



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

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.



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

def backend
  @backend
end

#credentialsObject (readonly)

Returns the value of attribute credentials.



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

def credentials
  @credentials
end

Instance Method Details

#current_driverObject



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

def current_driver
  Thread.current[:driver]
end

#current_providerObject



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

def current_provider
  Thread.current[:provider]
end

#provider(opts = {}) ⇒ Object



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

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

#providersObject



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

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

#versionObject



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

def version
  Deltacloud::API_VERSION
end