mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
Add snapshot model/collection and corresponding tests.
This commit is contained in:
parent
128c2a59c4
commit
858ef2265e
3 changed files with 108 additions and 0 deletions
56
lib/fog/hp/models/block_storage/snapshot.rb
Normal file
56
lib/fog/hp/models/block_storage/snapshot.rb
Normal file
|
@ -0,0 +1,56 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module BlockStorage
|
||||
class HP
|
||||
|
||||
class Snapshot < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name, :aliases => 'displayName'
|
||||
attribute :description, :aliases => 'displayDescription'
|
||||
attribute :size
|
||||
attribute :status
|
||||
attribute :created_at, :aliases => 'createdAt'
|
||||
attribute :volume_id, :aliases => 'volumeId'
|
||||
#attribute :metadata
|
||||
|
||||
def initialize(attributes = {})
|
||||
# assign these attributes first to prevent race condition with new_record?
|
||||
self.force = attributes.delete(:force) # force snapshotting of attached volumes
|
||||
super
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
connection.delete_snapshot(id)
|
||||
true
|
||||
end
|
||||
|
||||
def force=(new_force)
|
||||
@force = new_force
|
||||
end
|
||||
|
||||
def ready?
|
||||
self.status == 'available'
|
||||
end
|
||||
|
||||
def save
|
||||
raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity
|
||||
requires :name, :volume_id
|
||||
options = {
|
||||
#'metadata' => metadata, # TODO: Add metadata when snapshots support it
|
||||
'force' => @force
|
||||
}
|
||||
options = options.reject {|key, value| value.nil?}
|
||||
data = connection.create_snapshot(name, description, volume_id, options)
|
||||
merge_attributes(data.body['snapshot'])
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
29
lib/fog/hp/models/block_storage/snapshots.rb
Normal file
29
lib/fog/hp/models/block_storage/snapshots.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/hp/models/block_storage/snapshot'
|
||||
|
||||
module Fog
|
||||
module BlockStorage
|
||||
class HP
|
||||
|
||||
class Snapshots < Fog::Collection
|
||||
|
||||
model Fog::BlockStorage::HP::Snapshot
|
||||
|
||||
def all
|
||||
data = connection.list_snapshots.body['snapshots']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(snapshot_id)
|
||||
if snapshot = connection.get_snapshot_details(snapshot_id).body['snapshot']
|
||||
new(snapshot)
|
||||
end
|
||||
rescue Fog::BlockStorage::HP::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
23
tests/hp/models/block_storage/snapshot_tests.rb
Normal file
23
tests/hp/models/block_storage/snapshot_tests.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
Shindo.tests("Fog::BlockStorage[:hp] | snapshots", ['hp', 'block_storage', 'snapshots']) do
|
||||
|
||||
@volume = Fog::BlockStorage[:hp].volumes.create(:name => "testsnapvol", :size => 1)
|
||||
@volume.wait_for { ready? } unless Fog.mocking?
|
||||
|
||||
model_tests(Fog::BlockStorage[:hp].snapshots, {:name => "fogsnaptests", :description => "fogsnaptests-desc", :volume_id => @volume.id}, true)
|
||||
|
||||
tests("new snapshot") do
|
||||
@snapshot = Fog::BlockStorage[:hp].snapshots.create(:name => "testvol", :volume_id => @volume.id)
|
||||
@snapshot.wait_for { ready? } unless Fog.mocking?
|
||||
|
||||
test("get(#{@snapshot.id})") do
|
||||
Fog::BlockStorage[:hp].snapshots.get(@snapshot.id) != nil?
|
||||
end
|
||||
|
||||
after do
|
||||
@snapshot.destroy
|
||||
end
|
||||
end
|
||||
|
||||
@volume.destroy
|
||||
|
||||
end
|
Loading…
Reference in a new issue