Class: Fog::Compute::Vsphere::Volume

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

Constant Summary collapse

DISK_SIZE_TO_GB =
1048576

Instance Attribute Summary

Attributes inherited from Model

#collection, #service

Instance Method Summary collapse

Methods inherited from Model

#inspect, #reload, #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

#initialize(attributes = {}) ⇒ Volume

Returns a new instance of Volume.



21
22
23
24
25
26
# File 'lib/fog/vsphere/models/compute/volume.rb', line 21

def initialize(attributes={} )
  # Assign server first to prevent race condition with persisted?
  self.server_id = attributes.delete(:server_id)

  super defaults.merge(attributes)
end

Instance Method Details

#destroyObject



40
41
42
43
44
45
# File 'lib/fog/vsphere/models/compute/volume.rb', line 40

def destroy
  requires :server_id, :key, :unit_number

  service.destroy_vm_volume(self)
  true
end

#saveObject

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fog/vsphere/models/compute/volume.rb', line 47

def save
  raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if persisted?
  requires :server_id, :size, :datastore

  if unit_number.nil?
    used_unit_numbers = server.volumes.collect { |volume| volume.unit_number }
    max_unit_number = used_unit_numbers.max

    if max_unit_number > server.volumes.size
      # If the max ID exceeds the number of volumes, there must be a hole in the range. Find a hole and use it.
      self.unit_number = max_unit_number.times.to_a.find { |i| used_unit_numbers.exclude?(i) }
    else
      self.unit_number = max_unit_number + 1
    end
  else
    if server.volumes.any? { |volume| volume.unit_number == self.unit_number && volume.id != self.id }
      raise "A volume already exists with that unit_number, so we can't save the new volume"
    end
  end

  data = service.add_vm_volume(self)

  if data['task_state'] == 'success'
    # We have to query vSphere to get the volume attributes since the task handle doesn't include that info.
    created = server.volumes.all.find { |volume| volume.unit_number == self.unit_number }

    self.id = created.id
    self.key = created.key
    self.filename = created.filename

    true
  else
    false
  end
end

#serverObject



83
84
85
86
# File 'lib/fog/vsphere/models/compute/volume.rb', line 83

def server
  requires :server_id
  service.servers.get(server_id)
end

#size_gbObject



28
29
30
# File 'lib/fog/vsphere/models/compute/volume.rb', line 28

def size_gb
  attributes[:size_gb] ||= attributes[:size].to_i / DISK_SIZE_TO_GB if attributes[:size]
end

#size_gb=(s) ⇒ Object



32
33
34
# File 'lib/fog/vsphere/models/compute/volume.rb', line 32

def size_gb= s
  attributes[:size] = s.to_i * DISK_SIZE_TO_GB if s
end

#to_sObject



36
37
38
# File 'lib/fog/vsphere/models/compute/volume.rb', line 36

def to_s
  name
end