Class: Deltacloud::BaseDriver

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/base_driver/features.rb,
lib/deltacloud/base_driver/base_driver.rb

Defined Under Namespace

Classes: Feature, FeatureDecl, Operation

Constant Summary collapse

MEMBER_SHOW_METHODS =
[ :realm, :image, :instance, :storage_volume, :bucket, :blob, :key ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.declare_feature(collection, name, &block) ⇒ Object

Declare a new feature



90
91
92
93
94
# File 'lib/deltacloud/base_driver/features.rb', line 90

def self.declare_feature(collection, name, &block)
  feature_decls[collection] ||= []
  raise DuplicateFeatureDeclError if feature_decl_for(collection, name)
  feature_decls[collection] << FeatureDecl.new(name, &block)
end

.define_hardware_profile(name, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/deltacloud/base_driver/base_driver.rb', line 46

def self.define_hardware_profile(name,&block)
  @hardware_profiles ||= []
  hw_profile = @hardware_profiles.find{|e| e.name == name}
  return if hw_profile
  hw_profile = ::Deltacloud::HardwareProfile.new( name, &block )
  @hardware_profiles << hw_profile
  hw_params = hw_profile.params
  unless hw_params.empty?
    feature :instances, :hardware_profiles do
      decl.operation(:create) { add_params(hw_params) }
    end
  end
end

.define_instance_states(&block) ⇒ Object



104
105
106
107
# File 'lib/deltacloud/base_driver/base_driver.rb', line 104

def self.define_instance_states(&block)
  machine = ::Deltacloud::StateMachine.new(&block)
  @instance_state_machine = machine
end

.feature(collection, name, &block) ⇒ Object

Declare in a driver that it supports a specific feature

The same feature can be declared multiple times in a driver, so that it can be changed successively by passing in different blocks.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/deltacloud/base_driver/features.rb', line 104

def self.feature(collection, name, &block)
  features[collection] ||= []
  if f = features[collection].find { |f| f.name == name }
    f.instance_eval(&block) if block_given?
    return f
  end
  unless decl = feature_decl_for(collection, name)
    raise UndeclaredFeatureError, "No feature #{name} for #{collection}"
  end
  features[collection] << Feature.new(decl, &block)
end

.feature_decl_for(collection, name) ⇒ Object



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

def self.feature_decl_for(collection, name)
  decls = feature_decls[collection]
  if decls
    decls.find { |dcl| dcl.name == name }
  else
    nil
  end
end

.feature_declsObject



76
77
78
# File 'lib/deltacloud/base_driver/features.rb', line 76

def self.feature_decls
  @@feature_decls ||= {}
end

.featuresObject



96
97
98
# File 'lib/deltacloud/base_driver/features.rb', line 96

def self.features
  @@features ||= {}
end

.hardware_profilesObject



60
61
62
63
# File 'lib/deltacloud/base_driver/base_driver.rb', line 60

def self.hardware_profiles
  @hardware_profiles ||= []
  @hardware_profiles
end

.instance_state_machineObject



109
110
111
# File 'lib/deltacloud/base_driver/base_driver.rb', line 109

def self.instance_state_machine
  @instance_state_machine
end

Instance Method Details

#blob(credentials, opts = nil) ⇒ Object



194
195
196
# File 'lib/deltacloud/base_driver/base_driver.rb', line 194

def blob(credentials, opts = nil)
  blobs(credentials, opts).first if has_capability?(:blobs)
end

#bucket(credentials, opts = nil) ⇒ Object



189
190
191
192
# File 'lib/deltacloud/base_driver/base_driver.rb', line 189

def bucket(credentials, opts = nil)
  #list of objects within bucket
  buckets(credentials, opts).first if has_capability?(:buckets)
end

#catched_exceptions_listObject



233
234
235
# File 'lib/deltacloud/base_driver/base_driver.rb', line 233

def catched_exceptions_list
  { :error => [], :auth => [], :glob => [] }
end

#features(collection) ⇒ Object



116
117
118
# File 'lib/deltacloud/base_driver/features.rb', line 116

def features(collection)
  self.class.features[collection] || []
end

#features_for_operation(collection, operation) ⇒ Object



120
121
122
123
124
# File 'lib/deltacloud/base_driver/features.rb', line 120

def features_for_operation(collection, operation)
  features(collection).select do |f|
    f.operations.detect { |o| o.name == operation }
  end
end

#filter_hardware_profiles(profiles, opts) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/deltacloud/base_driver/base_driver.rb', line 74

def filter_hardware_profiles(profiles, opts)
  if opts
    if v = opts[:architecture]
      profiles = profiles.select { |hwp| hwp.include?(:architecture, v) }
    end
    if v = opts[:name]
      profiles = profiles.select { |hwp| hwp.name == v }
    end
  end
  profiles
end

#filter_on(collection, attribute, opts) ⇒ Object



214
215
216
217
218
219
220
221
222
223
# File 'lib/deltacloud/base_driver/base_driver.rb', line 214

def filter_on(collection, attribute, opts)
  return collection if opts.nil?
  return collection if opts[attribute].nil?
  filter = opts[attribute]
  if ( filter.is_a?( Array ) )
    return collection.select{|e| filter.include?( e.send(attribute) ) }
  else
    return collection.select{|e| filter == e.send(attribute) }
  end
end

#find_hardware_profile(credentials, name, image_id) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/deltacloud/base_driver/base_driver.rb', line 86

def find_hardware_profile(credentials, name, image_id)
  hwp = nil
  if name
    unless hwp = hardware_profiles(credentials, :name => name).first
      raise BackendError.new(400, "bad-hardware-profile-name",
        "Hardware profile '#{name}' does not exist", nil)
    end
  else
    unless image = image(credentials, :id=>image_id)
      raise BackendError.new(400, "bad-image-id",
          "Image with ID '#{image_id}' does not exist", nil)
    end
    hwp = hardware_profiles(credentials,
                            :architecture=>image.architecture).first
  end
  return hwp
end

#hardware_profile(credentials, name) ⇒ Object



70
71
72
# File 'lib/deltacloud/base_driver/base_driver.rb', line 70

def hardware_profile(credentials, name)
  hardware_profiles(credentials, :name => name).first
end

#hardware_profiles(credentials, opts = nil) ⇒ Object



65
66
67
68
# File 'lib/deltacloud/base_driver/base_driver.rb', line 65

def hardware_profiles(credentials, opts = nil)
  results = self.class.hardware_profiles
  filter_hardware_profiles(results, opts)
end

#has_capability?(capability) ⇒ Boolean

Returns:

  • (Boolean)


205
206
207
208
209
210
211
# File 'lib/deltacloud/base_driver/base_driver.rb', line 205

def has_capability?(capability)
  if MEMBER_SHOW_METHODS.include?(capability.to_sym)
    has_capability?(capability.to_s.pluralize)
  else
    respond_to?(capability)
  end
end

#has_collection?(collection) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
# File 'lib/deltacloud/base_driver/base_driver.rb', line 229

def has_collection?(collection)
  supported_collections.include?(collection)
end

#image(credentials, opts) ⇒ Object



173
174
175
# File 'lib/deltacloud/base_driver/base_driver.rb', line 173

def image(credentials, opts)
  images(credentials, opts).first if has_capability?(:images)
end

#instance(credentials, opts) ⇒ Object



177
178
179
# File 'lib/deltacloud/base_driver/base_driver.rb', line 177

def instance(credentials, opts)
  instances(credentials, opts).first if has_capability?(:instances)
end

#instance_actions_for(state) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/deltacloud/base_driver/base_driver.rb', line 117

def instance_actions_for(state)
  actions = []
  state_key = state.downcase.to_sym
  states = instance_state_machine.states()
  current_state = states.find{|e| e.name == state.underscore.to_sym }
  if ( current_state )
    actions = current_state.transitions.collect{|e|e.action}
    actions.reject!{|e| e.nil?}
  end
  actions
end

#instance_state_machineObject



113
114
115
# File 'lib/deltacloud/base_driver/base_driver.rb', line 113

def instance_state_machine
  self.class.instance_state_machine
end

#key(credentials, opts = nil) ⇒ Object



198
199
200
# File 'lib/deltacloud/base_driver/base_driver.rb', line 198

def key(credentials, opts=nil)
  keys(credentials, opts).first if has_capability?(:keys)
end

#realm(credentials, opts) ⇒ Object

Capabilities The rabbit dsl supports declaring a capability that is required in the backend driver for the call to succeed. A driver can provide a capability by implementing the method with the same name as the capability. Below is a list of the capabilities as the expected method signatures.

Following the capability list are the resource member show methods. They each require that the corresponding collection method be defined

TODO: standardize all of these to the same signature (credentials, opts)

def realms(credentials, opts=nil)

def images(credentials, ops)

def instances(credentials, ops) def create_instance(credentials, image_id, opts) def start_instance(credentials, id) def stop_instance(credentials, id) def reboot_instance(credentials, id)

def storage_volumes(credentials, ops)

def storage_snapshots(credentials, ops)

def buckets(credentials, opts = nil) def create_bucket(credentials, name, opts=nil) def delete_bucket(credentials, name, opts=nil)

def blobs(credentials, opts = nil) def blob_data(credentials, bucket_id, blob_id, opts) def create_blob(credentials, bucket_id, blob_id, blob_data, opts=nil) def delete_blob(credentials, bucket_id, blob_id, opts=nil)

def keys(credentials, opts) def create_key(credentials, opts) def destroy_key(credentials, opts)



169
170
171
# File 'lib/deltacloud/base_driver/base_driver.rb', line 169

def realm(credentials, opts)
  realms = realms(credentials, opts).first if has_capability?(:realms)
end

#safely(&block) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/deltacloud/base_driver/base_driver.rb', line 237

def safely(&block)
  begin
    block.call
  rescue *catched_exceptions_list[:error] => e
    raise Deltacloud::BackendError.new(502, e.class.to_s, e.message, e.backtrace)
  rescue *catched_exceptions_list[:auth] => e
    raise Deltacloud::AuthException.new
  rescue => e
    catched_exceptions_list[:glob].each do |ex|
      raise Deltacloud::BackendError.new(502, e.class.to_s, e.message, e.backtrace) if e.class.name =~ ex
    end
    puts "======= UNHANDLED EXCEPTION ============"
    puts e.inspect
    puts "========================================"
    raise e
  end
end

#storage_snapshot(credentials, opts) ⇒ Object



185
186
187
# File 'lib/deltacloud/base_driver/base_driver.rb', line 185

def storage_snapshot(credentials, opts)
  storage_snapshots(credentials, opts).first if has_capability?(:storage_snapshots)
end

#storage_volume(credentials, opts) ⇒ Object



181
182
183
# File 'lib/deltacloud/base_driver/base_driver.rb', line 181

def storage_volume(credentials, opts)
  storage_volumes(credentials, opts).first if has_capability?(:storage_volumes)
end

#supported_collectionsObject



225
226
227
# File 'lib/deltacloud/base_driver/base_driver.rb', line 225

def supported_collections
  DEFAULT_COLLECTIONS
end