Class: Fog::Vsphere::Compute::Cdrom

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

Instance Method Summary collapse

Instance Method Details

#destroyObject



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

def destroy
  requires :instance_uuid, :key, :unit_number

  service.destroy_vm_cdrom(self)
  true
end

#saveObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/fog/vsphere/models/compute/cdrom.rb', line 28

def save
  requires :instance_uuid

  if unit_number.nil?
    used_unit_numbers = server.cdroms.map(&:unit_number)
    max_unit_number = used_unit_numbers.max

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

  data = service.add_vm_cdrom(self)

  if data['task_state'] == 'success'
    # We have to query vSphere to get the cdrom attributes since the task handle doesn't include that info.
    created = server.cdroms.get(unit_number)

    self.key = created.key
    self.filename = created.filename
    self.unit_number = created.unit_number

    true
  else
    false
  end
end

#to_sObject



17
18
19
# File 'lib/fog/vsphere/models/compute/cdrom.rb', line 17

def to_s
  name
end