2013-07-01 19:20:25 -04:00
|
|
|
require 'fog/core/model'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
|
|
|
class Google
|
|
|
|
|
|
|
|
class Disk < Fog::Model
|
|
|
|
|
|
|
|
identity :name
|
|
|
|
|
|
|
|
attribute :kind, :aliases => 'kind'
|
|
|
|
attribute :id, :aliases => 'id'
|
|
|
|
attribute :creation_timestamp, :aliases => 'creationTimestamp'
|
2013-07-01 21:57:32 -04:00
|
|
|
attribute :zone_name, :aliases => 'zone'
|
2013-07-01 19:20:25 -04:00
|
|
|
attribute :status, :aliases => 'status'
|
|
|
|
attribute :description, :aliases => 'description'
|
|
|
|
attribute :size_gb, :aliases => 'sizeGb'
|
|
|
|
attribute :self_link, :aliases => 'selfLink'
|
2013-08-11 19:17:41 -04:00
|
|
|
attribute :source_image, :aliases => 'sourceImage'
|
|
|
|
attribute :source_snapshot, :aliases => 'sourceSnapshot'
|
|
|
|
attribute :source_snapshot_id, :aliases => 'sourceSnapshot'
|
2013-07-01 21:57:32 -04:00
|
|
|
|
|
|
|
def save
|
2013-08-11 19:17:41 -04:00
|
|
|
requires :name
|
|
|
|
requires :zone_name
|
|
|
|
|
|
|
|
options = {}
|
2013-12-18 21:15:02 -05:00
|
|
|
if source_image.nil? && !source_snapshot.nil?
|
2013-08-11 19:17:41 -04:00
|
|
|
options['sourceSnapshot'] = source_snapshot
|
|
|
|
end
|
|
|
|
|
2013-10-11 00:08:17 -04:00
|
|
|
options['sizeGb'] = size_gb
|
|
|
|
|
2013-08-11 19:17:41 -04:00
|
|
|
data = service.insert_disk(name, zone_name, source_image, options).body
|
2013-07-11 13:30:54 -04:00
|
|
|
data = service.backoff_if_unfound {service.get_disk(name, zone_name).body}
|
2013-07-01 21:57:32 -04:00
|
|
|
service.disks.merge_attributes(data)
|
|
|
|
end
|
2013-07-01 19:20:25 -04:00
|
|
|
|
2013-07-12 17:36:41 -04:00
|
|
|
def destroy
|
2013-08-11 19:17:41 -04:00
|
|
|
requires :name, :zone_name
|
2013-11-27 07:01:59 -05:00
|
|
|
operation = service.delete_disk(name, zone_name)
|
|
|
|
# wait until "RUNNING" or "DONE" to ensure the operation doesn't fail, raises exception on error
|
|
|
|
Fog.wait_for do
|
|
|
|
operation = service.get_zone_operation(zone_name, operation.body["name"])
|
|
|
|
operation.body["status"] != "PENDING"
|
|
|
|
end
|
|
|
|
operation
|
2013-07-12 17:36:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def zone
|
|
|
|
if self.zone_name.is_a? String
|
|
|
|
service.get_zone(self.zone_name.split('/')[-1]).body["name"]
|
|
|
|
elsif zone_name.is_a? Excon::Response
|
|
|
|
service.get_zone(zone_name.body["name"]).body["name"]
|
|
|
|
else
|
|
|
|
self.zone_name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-18 07:11:25 -05:00
|
|
|
def get_object(writable=true, boot=false, device_name=nil)
|
2013-07-01 19:20:25 -04:00
|
|
|
mode = writable ? 'READ_WRITE' : 'READ_ONLY'
|
|
|
|
return {
|
2013-12-18 07:11:25 -05:00
|
|
|
'boot' => boot,
|
|
|
|
'source' => self_link,
|
|
|
|
'mode' => mode,
|
2013-12-19 13:39:15 -05:00
|
|
|
'deviceName' => device_name,
|
|
|
|
'type' => 'PERSISTENT'
|
2013-12-18 07:11:25 -05:00
|
|
|
}.select { |k, v| !v.nil? }
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_as_boot_disk(writable=true)
|
|
|
|
get_object(writable, true)
|
2013-07-01 19:20:25 -04:00
|
|
|
end
|
|
|
|
|
2013-07-01 21:57:32 -04:00
|
|
|
def ready?
|
|
|
|
self.status == RUNNING_STATE
|
|
|
|
end
|
|
|
|
|
|
|
|
def reload
|
|
|
|
requires :identity
|
|
|
|
requires :zone_name
|
|
|
|
|
|
|
|
return unless data = begin
|
|
|
|
collection.get(identity, zone_name)
|
|
|
|
rescue Excon::Errors::SocketError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
new_attributes = data.attributes
|
|
|
|
merge_attributes(new_attributes)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-10-27 11:00:25 -04:00
|
|
|
def create_snapshot(snapshot_name, snapshot_description="")
|
2013-08-14 21:47:06 -04:00
|
|
|
requires :name
|
|
|
|
requires :zone_name
|
|
|
|
|
|
|
|
if snap_name.nil? or snap_name.empty?
|
|
|
|
raise ArgumentError, 'Invalid snapshot name'
|
|
|
|
end
|
|
|
|
|
|
|
|
options = {
|
2013-10-27 11:00:25 -04:00
|
|
|
'name' => snapshot_name,
|
|
|
|
'description' => snapshot_description,
|
2013-08-14 21:47:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
service.insert_snapshot(name, self.zone, service.project, options)
|
|
|
|
data = service.backoff_if_unfound {
|
2013-10-27 11:00:25 -04:00
|
|
|
service.get_snapshot(snapshot_name, service.project).body
|
2013-08-14 21:47:06 -04:00
|
|
|
}
|
|
|
|
service.snapshots.merge_attributes(data)
|
|
|
|
|
|
|
|
# Try to return the representation of the snapshot we created
|
2013-10-27 11:00:25 -04:00
|
|
|
service.snapshots.get(snapshot_name)
|
2013-08-14 21:47:06 -04:00
|
|
|
end
|
|
|
|
|
2013-07-01 21:57:32 -04:00
|
|
|
RUNNING_STATE = "READY"
|
|
|
|
|
2013-07-01 19:20:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-08-11 19:17:41 -04:00
|
|
|
end
|