mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
[hp|block_storage_v2] Add snapshot model along with tests.
This commit is contained in:
parent
e057f8ff57
commit
e672899cee
4 changed files with 137 additions and 2 deletions
|
@ -15,8 +15,8 @@ module Fog
|
|||
model :volume
|
||||
collection :volumes
|
||||
#collection :bootable_volumes
|
||||
#model :snapshot
|
||||
#collection :snapshots
|
||||
model :snapshot
|
||||
collection :snapshots
|
||||
|
||||
request_path 'fog/hp/requests/block_storage_v2'
|
||||
request :create_volume
|
||||
|
|
76
lib/fog/hp/models/block_storage_v2/snapshot.rb
Normal file
76
lib/fog/hp/models/block_storage_v2/snapshot.rb
Normal file
|
@ -0,0 +1,76 @@
|
|||
require 'fog/core/model'
|
||||
|
||||
module Fog
|
||||
module HP
|
||||
class BlockStorageV2
|
||||
|
||||
class Snapshot < Fog::Model
|
||||
|
||||
identity :id
|
||||
|
||||
attribute :name, :aliases => 'display_name'
|
||||
attribute :description, :aliases => 'display_description'
|
||||
attribute :size
|
||||
attribute :status
|
||||
attribute :created_at
|
||||
attribute :volume_id
|
||||
attribute :metadata
|
||||
|
||||
def initialize(attributes = {})
|
||||
# assign these attributes first to prevent race condition with persisted?
|
||||
self.force = attributes.delete(:force) # force snapshotting of attached volumes
|
||||
super
|
||||
end
|
||||
|
||||
def destroy
|
||||
requires :id
|
||||
service.delete_snapshot(id)
|
||||
true
|
||||
end
|
||||
|
||||
def force=(new_force)
|
||||
@force = new_force
|
||||
end
|
||||
|
||||
def ready?
|
||||
self.status == 'available'
|
||||
end
|
||||
|
||||
def save
|
||||
identity ? update : create
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def create
|
||||
requires :volume_id
|
||||
options = {
|
||||
'display_name' => name,
|
||||
'display_description' => description,
|
||||
'force' => @force,
|
||||
'metadata' => metadata
|
||||
}
|
||||
options = options.reject {|_, value| value.nil?}
|
||||
data = service.create_snapshot(volume_id, options)
|
||||
merge_attributes(data.body['snapshot'])
|
||||
true
|
||||
end
|
||||
|
||||
def update
|
||||
requires :id
|
||||
options = {
|
||||
'display_name' => name,
|
||||
'display_description' => description,
|
||||
'metadata' => metadata
|
||||
}
|
||||
options = options.reject {|_, value| value.nil?}
|
||||
data = service.update_snapshot(id, options)
|
||||
merge_attributes(data.body['snapshot'])
|
||||
true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
37
lib/fog/hp/models/block_storage_v2/snapshots.rb
Normal file
37
lib/fog/hp/models/block_storage_v2/snapshots.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
require 'fog/core/collection'
|
||||
require 'fog/hp/models/block_storage_v2/snapshot'
|
||||
|
||||
module Fog
|
||||
module HP
|
||||
class BlockStorageV2
|
||||
|
||||
class Snapshots < Fog::Collection
|
||||
|
||||
attribute :filters
|
||||
|
||||
model Fog::HP::BlockStorageV2::Snapshot
|
||||
|
||||
def initialize(attributes)
|
||||
self.filters ||= {}
|
||||
super
|
||||
end
|
||||
|
||||
def all(filters = filters)
|
||||
self.filters = filters
|
||||
data = service.list_snapshots(filters).body['snapshots']
|
||||
load(data)
|
||||
end
|
||||
|
||||
def get(snapshot_id)
|
||||
if snapshot = service.get_snapshot_details(snapshot_id).body['snapshot']
|
||||
new(snapshot)
|
||||
end
|
||||
rescue Fog::HP::BlockStorageV2::NotFound
|
||||
nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
22
tests/hp/models/block_storage_v2/snapshot_tests.rb
Normal file
22
tests/hp/models/block_storage_v2/snapshot_tests.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
Shindo.tests("HP::BlockStorageV2 | snapshot model", ['hp', 'v2', 'block_storage', 'snapshots']) do
|
||||
|
||||
@volume = HP[:block_storage_v2].volumes.create(:name => 'testsnap2vol', :size => 1)
|
||||
@volume.wait_for { ready? } unless Fog.mocking?
|
||||
|
||||
model_tests(HP[:block_storage_v2].snapshots, {:name => 'fogsnap2tests', :description => 'fogsnaptests-desc', :volume_id => @volume.id}, true) do
|
||||
test("get(#{@instance.id})") do
|
||||
HP[:block_storage_v2].snapshots.get(@instance.id) != nil?
|
||||
end
|
||||
|
||||
test("update(#{@instance.id}") do
|
||||
@instance.name = 'Updated'
|
||||
@instance.save
|
||||
@instance.reload
|
||||
@instance.name == 'Updated'
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@volume.destroy
|
||||
|
||||
end
|
Loading…
Reference in a new issue