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 Attribute Summary

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Model

#initialize, #inspect, #symbolize_keys, #to_json, #wait_for

Methods included from Attributes::ClassMethods

#_load, #aliases, #attribute, #attributes, #identity, #ignore_attributes, #ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

This class inherits a constructor from Fog::Model

Instance Method Details

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



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

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

  if snap_name.nil? or snap_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

#destroyObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/fog/google/models/compute/disk.rb', line 39

def destroy
  requires :name, :zone_name
  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
end

#get_as_boot_disk(writable = true) ⇒ Object



71
72
73
# File 'lib/fog/google/models/compute/disk.rb', line 71

def get_as_boot_disk(writable=true)
  get_object(writable, true)
end

#get_object(writable = true, boot = false, device_name = nil) ⇒ Object



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

def get_object(writable=true, boot=false, device_name=nil)
  mode = writable ? 'READ_WRITE' : 'READ_ONLY'
  return {
    'boot' => boot,
    'source' => self_link,
    'mode' => mode,
    'deviceName' => device_name,
    'type' => 'PERSISTENT'
  }.select { |k, v| !v.nil? }
end

#ready?Boolean

Returns:

  • (Boolean)


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

def ready?
  self.status == RUNNING_STATE
end

#reloadObject



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

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
# File 'lib/fog/google/models/compute/disk.rb', line 23

def save
  requires :name
  requires :zone_name

  options = {}
  if source_image.nil? && !source_snapshot.nil?
    options['sourceSnapshot'] = source_snapshot
  end

  options['sizeGb'] = size_gb

  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



50
51
52
53
54
55
56
57
58
# File 'lib/fog/google/models/compute/disk.rb', line 50

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