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

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

Constant Summary collapse

DISK_SIZE_TO_GB =
1_048_576

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Volume

Returns a new instance of Volume.



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

def initialize(attributes = {})
  super defaults.merge(attributes)
end

Instance Method Details

#destroyObject



47
48
49
50
51
52
# File 'lib/fog/vsphere/models/compute/volume.rb', line 47

def destroy
  requires :server_id, :key, :unit_number

  service.destroy_vm_volume(self)
  true
end

#detachObject



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

def detach
  requires :server_id, :key, :unit_number

  service.remove_vm_volume(self)
  server.volumes -= [self]
  true
end

#saveObject

Raises:

  • (Fog::Errors::Error)


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
82
83
84
85
86
87
88
89
90
91
# File 'lib/fog/vsphere/models/compute/volume.rb', line 54

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

  set_unit_number

  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 }

    # example of "created" =>
    #   <Fog::Vsphere::Compute::Volume
    #     id="6000C295-576f-0e2d-5b70-c778cd108b3a",
    #     datastore="datastore1",
    #     storage_pod=nil,
    #     mode="persistent",
    #     size=10485760,
    #     thin=true,
    #     eager_zero=nil,
    #     name="Hard disk 2",
    #     filename="[datastore1] testvm/testvm_2.vmdk",
    #     size_gb=10,
    #     key=2004,
    #     unit_number=2,
    #     controller_key=1000
    #   >
    self.id = created.id
    self.key = created.key
    self.controller_key = created.controller_key
    self.filename = created.filename

    true
  else
    false
  end
end

#server_idObject



93
94
95
96
# File 'lib/fog/vsphere/models/compute/volume.rb', line 93

def server_id
  requires :server
  server.id
end

#set_keyObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/fog/vsphere/models/compute/volume.rb', line 112

def set_key
  requires :controller_key, :unit_number

  return unless key.nil?

  # controller key is based on 1000 + controller bus
  # disk key is based on 2000 + the SCSI ID + the controller bus * 16
  controller_bus = controller_key - 1000
  self.key = 2000 + (controller_bus * 16) + unit_number
end

#set_unit_numberObject



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/fog/vsphere/models/compute/volume.rb', line 98

def set_unit_number
  requires :server
  # When adding volumes to vsphere, if our unit_number is 7 or higher, vsphere will increment the unit_number
  # This is due to SCSI ID 7 being reserved for the pvscsi controller
  # When referring to a volume that already added using a unit_id of 7 or higher, we must refer to the actual SCSI ID
  if unit_number.nil?
    self.unit_number = calculate_free_unit_number
  else
    if server.volumes.select { |vol| vol.controller_key == controller_key }.any? { |volume| volume.unit_number == self.unit_number && volume.id != id }
      raise "A volume already exists with that unit_number, so we can't save the new volume"
    end
  end
end

#size_gbObject



26
27
28
# File 'lib/fog/vsphere/models/compute/volume.rb', line 26

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

#size_gb=(s) ⇒ Object



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

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

#to_sObject



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

def to_s
  name
end