From e672899ceebd50cc78c9684f784db156b095af18 Mon Sep 17 00:00:00 2001 From: Rupak Ganguly Date: Wed, 22 May 2013 13:48:15 -0400 Subject: [PATCH] [hp|block_storage_v2] Add snapshot model along with tests. --- lib/fog/hp/block_storage_v2.rb | 4 +- .../hp/models/block_storage_v2/snapshot.rb | 76 +++++++++++++++++++ .../hp/models/block_storage_v2/snapshots.rb | 37 +++++++++ .../models/block_storage_v2/snapshot_tests.rb | 22 ++++++ 4 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 lib/fog/hp/models/block_storage_v2/snapshot.rb create mode 100644 lib/fog/hp/models/block_storage_v2/snapshots.rb create mode 100644 tests/hp/models/block_storage_v2/snapshot_tests.rb diff --git a/lib/fog/hp/block_storage_v2.rb b/lib/fog/hp/block_storage_v2.rb index d8b743eea..cfb30f2dc 100644 --- a/lib/fog/hp/block_storage_v2.rb +++ b/lib/fog/hp/block_storage_v2.rb @@ -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 diff --git a/lib/fog/hp/models/block_storage_v2/snapshot.rb b/lib/fog/hp/models/block_storage_v2/snapshot.rb new file mode 100644 index 000000000..07baacae1 --- /dev/null +++ b/lib/fog/hp/models/block_storage_v2/snapshot.rb @@ -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 diff --git a/lib/fog/hp/models/block_storage_v2/snapshots.rb b/lib/fog/hp/models/block_storage_v2/snapshots.rb new file mode 100644 index 000000000..25c8520db --- /dev/null +++ b/lib/fog/hp/models/block_storage_v2/snapshots.rb @@ -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 diff --git a/tests/hp/models/block_storage_v2/snapshot_tests.rb b/tests/hp/models/block_storage_v2/snapshot_tests.rb new file mode 100644 index 000000000..84a98dac4 --- /dev/null +++ b/tests/hp/models/block_storage_v2/snapshot_tests.rb @@ -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