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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fog/google/models/compute/disk.rb', line 106

def create_snapshot(snapshot_name, snapshot_description="")
  requires :name
  requires :zone_name

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

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

  service.insert_snapshot(name, self.zone, service.project, options)
  data = service.backoff_if_unfound {
    service.get_snapshot(snapshot_name, service.project).body
  }
  service.snapshots.merge_attributes(data)

  # Try to return the representation of the snapshot we created
  service.snapshots.get(snapshot_name)
end

#destroy(async = true) ⇒ Object



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

def destroy(async=true)
  requires :name, :zone_name
  operation = service.delete_disk(name, zone_name)
  # wait until "DONE" to ensure the operation doesn't fail, raises exception on error
  if not async
    Fog.wait_for do
      operation = service.get_zone_operation(zone_name, operation.body["name"])
      operation.body["status"] == "DONE"
    end
  end
  operation
end

#get_as_boot_disk(writable = true, auto_delete = false) ⇒ Object



83
84
85
# File 'lib/fog/google/models/compute/disk.rb', line 83

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.



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fog/google/models/compute/disk.rb', line 70

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



87
88
89
# File 'lib/fog/google/models/compute/disk.rb', line 87

def ready?
  self.status == RUNNING_STATE
end

#reloadObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/google/models/compute/disk.rb', line 91

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

#saveObject



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

def save
  requires :name
  requires :zone_name

  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_name, source_image, options).body
  data = service.backoff_if_unfound {service.get_disk(name, zone_name).body}
  service.disks.merge_attributes(data)
end

#zoneObject



58
59
60
61
62
63
64
65
66
# File 'lib/fog/google/models/compute/disk.rb', line 58

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