class Fog::Compute::Google::Disk

Constants

RUNNING_STATE

Public Instance Methods

create_snapshot(snapshot_name, snapshot_description="") click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 98
def create_snapshot(snapshot_name, snapshot_description="")
  requires :name, :zone

  if snapshot_name.nil? or snapshot_name.empty?
    raise ArgumentError, 'Invalid snapshot name'
  end

  options = {
    'name'        => snapshot_name,
    'description' => snapshot_description,
  }

  data = service.insert_snapshot(name, zone_name, service.project, options)
  operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
  operation.wait_for { !pending? }
  service.snapshots.get(snapshot_name)
end
destroy(async=true) click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 46
def destroy(async=true)
  requires :name, :zone

  data = service.delete_disk(name, zone_name)
  operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
  unless async
    operation.wait_for { ready? }
  end
  operation
end
get_as_boot_disk(writable=true, auto_delete=false) click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 76
def get_as_boot_disk(writable=true, auto_delete=false)
  get_object(writable, true, nil, auto_delete)
end
get_object(writable=true, boot=false, device_name=nil, auto_delete=false) click to toggle source

auto_delete can only be applied to disks created before instance creation. auto_delete = true will automatically delete disk upon instance termination.

# File lib/fog/google/models/compute/disk.rb, line 63
def get_object(writable=true, boot=false, device_name=nil, auto_delete=false)
  mode = writable ? 'READ_WRITE' : 'READ_ONLY'
  value = {
    'autoDelete' => auto_delete,
    'boot' => boot,
    'source' => self_link,
    'mode' => mode,
    'deviceName' => device_name,
    'type' => 'PERSISTENT'
  }.select { |k, v| !v.nil? }
  return Hash[value]
end
ready?() click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 80
def ready?
  self.status == RUNNING_STATE
end
reload() click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 84
def reload
  requires :identity, :zone

  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
save() click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 23
def save
  requires :name, :zone, :size_gb

  options = {}
  my_description = "Created with fog"
  if !source_image.nil?
    my_description = "Created from image: #{source_image}"
  end
  if source_image.nil? && !source_snapshot.nil?
    options['sourceSnapshot'] = source_snapshot
    my_description = "Created from snapshot: #{source_snapshot}"
  end

  options['sizeGb'] = size_gb
  options['description'] = description || my_description
  options['type'] = type

  data = service.insert_disk(name, zone, source_image, options)
  operation = Fog::Compute::Google::Operations.new(:service => service).get(data.body['name'], data.body['zone'])
  operation.wait_for { !pending? }
  reload
end
zone_name() click to toggle source
# File lib/fog/google/models/compute/disk.rb, line 57
def zone_name
  zone.nil? ? nil : zone.split('/')[-1]
end