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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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) ⇒ Object



76
77
78
# 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) ⇒ Object

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



63
64
65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/fog/google/models/compute/disk.rb', line 80

def ready?
  self.status == RUNNING_STATE
end

#reloadObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# 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

#saveObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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_nameObject



57
58
59
# File 'lib/fog/google/models/compute/disk.rb', line 57

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