From 7fe779641262ce2f98927d4129c0084d6c358fba Mon Sep 17 00:00:00 2001 From: Dmitry Dedov Date: Tue, 26 Mar 2013 18:27:42 +0300 Subject: [PATCH] [cloudstack] add snapshot model --- lib/fog/cloudstack/compute.rb | 3 ++ lib/fog/cloudstack/models/compute/snapshot.rb | 46 +++++++++++++++++++ .../cloudstack/models/compute/snapshots.rb | 25 ++++++++++ .../requests/compute/create_snapshot.rb | 24 ++++++++++ .../compute/models/snapshot_tests.rb | 34 ++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 lib/fog/cloudstack/models/compute/snapshot.rb create mode 100644 lib/fog/cloudstack/models/compute/snapshots.rb create mode 100644 tests/cloudstack/compute/models/snapshot_tests.rb diff --git a/lib/fog/cloudstack/compute.rb b/lib/fog/cloudstack/compute.rb index 8623a9b69..09a24380e 100644 --- a/lib/fog/cloudstack/compute.rb +++ b/lib/fog/cloudstack/compute.rb @@ -33,6 +33,8 @@ module Fog collection :security_group_rules model :volume collection :volumes + model :snapshot + collection :snapshots model :zone collection :zones @@ -420,6 +422,7 @@ module Fog :jobs => {}, :volumes => {}, :security_groups => {}, + :snapshots => {} } end end diff --git a/lib/fog/cloudstack/models/compute/snapshot.rb b/lib/fog/cloudstack/models/compute/snapshot.rb new file mode 100644 index 000000000..022adaa11 --- /dev/null +++ b/lib/fog/cloudstack/models/compute/snapshot.rb @@ -0,0 +1,46 @@ +module Fog + module Compute + class Cloudstack + class Snapshot < Fog::Model + identity :id, :aliases => 'id' + + attribute :name, :aliases => 'name' + attribute :volume_type, :aliases => 'volumetype' + attribute :volume_name, :aliases => 'volumename' + attribute :volume_id, :aliases => 'volumeid' + attribute :created, :aliases => 'created' + attribute :state, :aliases => 'state' + attribute :account, :aliases => 'account' + attribute :domain_id, :aliases => 'domainid' + attribute :domain, :aliases => 'domain' + attribute :snapshot_type, :aliases => 'snapshot_type' + attribute :interval_type, :aliases => 'interval_type' + + def save + requires :volume_id + + options = { + 'volumeid' => volume_id, + 'domainid' => domain_id + } + data = service.create_snapshot(options) + merge_attributes(data['createsnapshotresponse']) + end + + def ready? + state == 'BackedUp' + end + + def volume + service.volumes.get(volume_id) if volume_id + end + + def destroy + requires :id + service.delete_snapshot('id' => id) + true + end + end # Snapshot + end # Cloudstack + end # Compute +end # Fog diff --git a/lib/fog/cloudstack/models/compute/snapshots.rb b/lib/fog/cloudstack/models/compute/snapshots.rb new file mode 100644 index 000000000..7ad0b2888 --- /dev/null +++ b/lib/fog/cloudstack/models/compute/snapshots.rb @@ -0,0 +1,25 @@ +require 'fog/core/collection' +require 'fog/cloudstack/models/compute/snapshot' + +module Fog + module Compute + class Cloudstack + + class Snapshots < Fog::Collection + + model Fog::Compute::Cloudstack::Snapshot + + def all + data = service.list_snapshots["listsnapshotsresponse"]["snapshot"] || [] + load(data) + end + + def get(snapshot_id) + snapshot = service.list_snapshots('id' => snapshot_id)["listsnapshotsresponse"]["snapshot"].first + new(snapshot) if snapshot + end + end + + end + end +end diff --git a/lib/fog/cloudstack/requests/compute/create_snapshot.rb b/lib/fog/cloudstack/requests/compute/create_snapshot.rb index b48a2e386..feed4f79d 100644 --- a/lib/fog/cloudstack/requests/compute/create_snapshot.rb +++ b/lib/fog/cloudstack/requests/compute/create_snapshot.rb @@ -15,6 +15,30 @@ module Fog end end + + class Mock + def create_snapshot(options={}) + snapshot_id = Fog::Cloudstack.uuid + + unless volume_id = options['volumeid'] + raise Fog::Compute::Cloudstack::BadRequest.new('Unable to execute API command createsnapshot due to missing parameter volumeid') + end + + snapshot = { + "id" => snapshot_id, + "name" => "ROOT-6", + "created" => "2013-05-22T14:52:55-0500", + "state" => "BackedUp", + "account" => "accountname", + "domainid" => "6023b6fe-5bef-4358-bc76-9f4e75afa52f", + "domain" => "ROOT", + "intervaltype" => "weekly" + } + + self.data[:snapshots][snapshot_id]= snapshot + {'createsnapshotresponse' => snapshot} + end + end end end end diff --git a/tests/cloudstack/compute/models/snapshot_tests.rb b/tests/cloudstack/compute/models/snapshot_tests.rb new file mode 100644 index 000000000..00c5472ab --- /dev/null +++ b/tests/cloudstack/compute/models/snapshot_tests.rb @@ -0,0 +1,34 @@ +def snapshot_tests(connection, params, mocks_implemented = true) + model_tests(connection.snapshots, params[:snapshot_attributes], mocks_implemented) do + if !Fog.mocking? || mocks_implemented + @instance.wait_for { ready? } + end + + @volume = @instance.connection.volumes.create(params[:volumes_attributes]) + @volume.wait_for { ready? } + + tests('create').succeeds do + @instance.create volume_id: @volume.id + end + + tests('destroy').succeeds do + @instance.destroy + end + + @volume.destroy + end +end + +Shindo.tests("Fog::Compute[:cloudstack] | snapshot", "cloudstack") do + + config = compute_providers[:cloudstack] + + snapshot_tests(Fog::Compute[:cloudstack], config, config[:mocked]) do + if Fog.mocking? && !mocks_implemented + pending + else + responds_to(:ready?) + responds_to(:volume) + end + end +end