Skip to content

Commit

Permalink
Merge pull request #2106 from maginatics/gce_create_snapshot
Browse files Browse the repository at this point in the history
[google|compute] Create a Snapshot based on a Disk
  • Loading branch information
icco committed Oct 26, 2013
2 parents 052768c + dd175d9 commit 861b15a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/fog/google/compute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Google < Fog::Service
request :insert_image
request :insert_network
request :insert_server
request :insert_snapshot

request :set_metadata

Expand Down
23 changes: 23 additions & 0 deletions lib/fog/google/models/compute/disk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ def reload
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
Expand Down
1 change: 1 addition & 0 deletions lib/fog/google/models/compute/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Snapshot < Fog::Model
attribute :source_disk , :aliases => 'sourceDisk'
attribute :source_disk_id , :aliases => 'sourceDiskId'
attribute :description
attribute :status

def reload
requires :name
Expand Down
49 changes: 49 additions & 0 deletions lib/fog/google/requests/compute/insert_snapshot.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 861b15a

Please sign in to comment.