Skip to content

Commit

Permalink
Merge pull request fog#1698 from dm1try/master
Browse files Browse the repository at this point in the history
[cloudstack] add snapshot model
  • Loading branch information
geemus committed Mar 26, 2013
2 parents bd28a09 + 9bd6b53 commit a42d7c7
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/fog/cloudstack/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Unauthorized < Fog::Compute::Cloudstack::Error; end
collection :security_group_rules
model :volume
collection :volumes
model :snapshot
collection :snapshots
model :zone
collection :zones

Expand Down Expand Up @@ -420,6 +422,7 @@ def self.data
:jobs => {},
:volumes => {},
:security_groups => {},
:snapshots => {}
}
end
end
Expand Down
46 changes: 46 additions & 0 deletions lib/fog/cloudstack/models/compute/snapshot.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions lib/fog/cloudstack/models/compute/snapshots.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions lib/fog/cloudstack/requests/compute/create_snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ def create_snapshot(options={})
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
34 changes: 34 additions & 0 deletions tests/cloudstack/compute/models/snapshot_tests.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a42d7c7

Please sign in to comment.