1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Create a Snapshot based on a Disk

This commit is contained in:
Akshay Moghe 2013-08-14 18:47:06 -07:00
parent 17a3b98c60
commit dd175d9f94
4 changed files with 74 additions and 0 deletions

View file

@ -42,6 +42,7 @@ module Fog
request :insert_image
request :insert_network
request :insert_server
request :insert_snapshot
request :set_metadata

View file

@ -84,6 +84,29 @@ module Fog
self
end
def create_snapshot(snap_name, snap_desc)
requires :name
requires :zone_name
if snap_name.nil? or snap_name.empty?
raise ArgumentError, 'Invalid snapshot name'
end
options = {
'name' => snap_name,
'description' => snap_desc,
}
service.insert_snapshot(name, self.zone, service.project, options)
data = service.backoff_if_unfound {
service.get_snapshot(snap_name, service.project).body
}
service.snapshots.merge_attributes(data)
# Try to return the representation of the snapshot we created
service.snapshots.get(snap_name)
end
RUNNING_STATE = "READY"
end

View file

@ -15,6 +15,7 @@ module Fog
attribute :source_disk , :aliases => 'sourceDisk'
attribute :source_disk_id , :aliases => 'sourceDiskId'
attribute :description
attribute :status
def reload
requires :name

View file

@ -0,0 +1,49 @@
module Fog
module Compute
class Google
class Mock
def insert_snapshot(snap_name)
Fog::Mock.not_implemented
end
end
class Real
def insert_snapshot(disk_name, zone_name, project=@project, opts={})
# This is unfortunate, since we might be called from 2 contexts
# 1. disk.snapshot <-- here validation of disk_name is not needed
# 2. snapshot.create <-- here we must validate the disk_name
#
# Validation would involve 'get'ing the disk by that name. This is
# redundant (and expensive) for case (1) which is likely the common
# codepath. So we won't do it.
api_method = @compute.disks.create_snapshot
parameters = {
'disk' => disk_name,
'zone' => zone_name,
'project' => @project,
}
snap_name = opts.delete('name')
raise ArgumentError.new('Must specify snapshot name') unless snap_name
body_object = { 'name' => snap_name }
# Merge in any remaining options (description)
body_object.merge(opts)
result = self.build_result(api_method, parameters,
body_object)
response = self.build_response(result)
end
end
end
end
end