Class: Fog::Compute::Google::Disk

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/google/models/compute/disk.rb

Constant Summary collapse

RUNNING_STATE =
"READY"

Instance Method Summary collapse

Instance Method Details

#create_snapshot(snapshot_name, snapshot_description = "") ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fog/google/models/compute/disk.rb', line 96

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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/google/models/compute/disk.rb', line 44

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) ⇒ Object



74
75
76
# File 'lib/fog/google/models/compute/disk.rb', line 74

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) ⇒ Object

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



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fog/google/models/compute/disk.rb', line 61

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?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/fog/google/models/compute/disk.rb', line 78

def ready?
  self.status == RUNNING_STATE
end

#reloadObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/fog/google/models/compute/disk.rb', line 82

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

#saveObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fog/google/models/compute/disk.rb', line 22

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

  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_nameObject



55
56
57
# File 'lib/fog/google/models/compute/disk.rb', line 55

def zone_name
  zone.nil? ? nil : zone.split('/')[-1]
end