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

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



210
211
212
213
# File 'lib/deltacloud/base_driver/base_driver.rb', line 210

def blob(credentials, opts = nil)
   list = blobs(credentials, opts)
   return list.first unless list.empty?
end

#blob_data(credentials, bucket_id, blob_id, opts) ⇒ Object



215
216
# File 'lib/deltacloud/base_driver/base_driver.rb', line 215

def blob_data(credentials, bucket_id, blob_id, opts)
end

#blobs(credentials, opts = nil) ⇒ Object



206
207
208
# File 'lib/deltacloud/base_driver/base_driver.rb', line 206

def blobs(credentials, opts = nil)
  []
end

#bucket(credentials, opts = nil) ⇒ Object



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

def bucket(credentials, opts = nil)
#list of objects within bucket
  list = buckets(credentials, opts)
  return list.first unless list.empty?
  nil
end

#buckets(credentials, opts = nil) ⇒ Object



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

def buckets(credentials, opts = nil)
  #list of buckets belonging to account
  []
end

#catched_exceptions_listObject



238
239
240
# File 'lib/deltacloud/base_driver/base_driver.rb', line 238

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

#create_bucket(credentials, name, opts = nil) ⇒ Object



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

def create_bucket(credentials, name, opts=nil)
end

#create_instance(credentials, image_id, opts) ⇒ Object



159
160
# File 'lib/deltacloud/base_driver/base_driver.rb', line 159

def create_instance(credentials, image_id, opts)
end

#delete_bucket(credentials, name, opts = nil) ⇒ Object



203
204
# File 'lib/deltacloud/base_driver/base_driver.rb', line 203

def delete_bucket(credentials, name, opts=nil)
end

#features(collection) ⇒ Object



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

def features(collection)
  self.class.features[collection] || []
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



218
219
220
221
222
223
224
225
226
227
# File 'lib/deltacloud/base_driver/base_driver.rb', line 218

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_collection?(collection) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_collection?(collection)
  return true if self.supported_collections.include?(collection)
  return false
end

#image(credentials, opts) ⇒ Object



139
140
141
142
143
# File 'lib/deltacloud/base_driver/base_driver.rb', line 139

def image(credentials, opts)
  images = images(credentials, opts)
  return images.first unless images.empty?
  nil
end

#images(credentials, ops) ⇒ Object



145
146
147
# File 'lib/deltacloud/base_driver/base_driver.rb', line 145

def images(credentials, ops)
  []
end

#instance(credentials, opts) ⇒ Object



149
150
151
152
153
# File 'lib/deltacloud/base_driver/base_driver.rb', line 149

def instance(credentials, opts)
  instances = instances(credentials, opts)
  return instances.first unless instances.empty?
  nil
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

#instances(credentials, ops) ⇒ Object



155
156
157
# File 'lib/deltacloud/base_driver/base_driver.rb', line 155

def instances(credentials, ops)
  []
end

#realm(credentials, opts) ⇒ Object



129
130
131
132
133
# File 'lib/deltacloud/base_driver/base_driver.rb', line 129

def realm(credentials, opts)
  realms = realms(credentials, opts)
  return realms.first unless realms.empty?
  nil
end

#realms(credentials, opts = nil) ⇒ Object



135
136
137
# File 'lib/deltacloud/base_driver/base_driver.rb', line 135

def realms(credentials, opts=nil)
  []
end

#reboot_instance(credentials, id) ⇒ Object



165
166
# File 'lib/deltacloud/base_driver/base_driver.rb', line 165

def reboot_instance(credentials, id)
end

#safely(&block) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/deltacloud/base_driver/base_driver.rb', line 242

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
    raise e
  end
end

#start_instance(credentials, id) ⇒ Object



161
162
# File 'lib/deltacloud/base_driver/base_driver.rb', line 161

def start_instance(credentials, id)
end

#stop_instance(credentials, id) ⇒ Object



163
164
# File 'lib/deltacloud/base_driver/base_driver.rb', line 163

def stop_instance(credentials, id)
end

#storage_snapshot(credentials, opts) ⇒ Object



178
179
180
181
182
# File 'lib/deltacloud/base_driver/base_driver.rb', line 178

def storage_snapshot(credentials, opts)
  snapshots = storage_snapshots(credentials, opts)
  return snapshots.first unless snapshots.empty?
  nil
end

#storage_snapshots(credentials, ops) ⇒ Object



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

def storage_snapshots(credentials, ops)
  []
end

#storage_volume(credentials, opts) ⇒ Object



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

def storage_volume(credentials, opts)
  volumes = storage_volumes(credentials, opts)
  return volumes.first unless volumes.empty?
  nil
end

#storage_volumes(credentials, ops) ⇒ Object



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

def storage_volumes(credentials, ops)
  []
end

#supported_collectionsObject



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

def supported_collections
  DEFAULT_COLLECTIONS
end