Class: VagrantPlugins::ProviderLibvirt::Util::DiskDeviceResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-libvirt/util/resolvers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prefix = 'vd') ⇒ DiskDeviceResolver

Returns a new instance of DiskDeviceResolver.



13
14
15
16
17
18
# File 'lib/vagrant-libvirt/util/resolvers.rb', line 13

def initialize(prefix='vd')
  @default_prefix = prefix

  @device_indicies = Hash.new
  @existing = Hash.new
end

Instance Attribute Details

#existingObject (readonly)

Returns the value of attribute existing.



11
12
13
# File 'lib/vagrant-libvirt/util/resolvers.rb', line 11

def existing
  @existing
end

Instance Method Details

#resolve(disks) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/vagrant-libvirt/util/resolvers.rb', line 47

def resolve(disks)
  new_disks = []
  disks.each do |disk|
    new_disks.push disk.dup
  end

  resolve!(new_disks)

  new_disks
end

#resolve!(disks, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vagrant-libvirt/util/resolvers.rb', line 20

def resolve!(disks, options={})
  # check for duplicate device entries and raise an exception if one found
  # with enough details that the user should be able to determine what
  # to do to resolve.
  disks.select { |x| !x[:device].nil? }.each do |x|
    if @existing.has_key?(x[:device])
      raise Errors::DuplicateDiskDevice, new_disk: x, existing_disk: @existing[x[:device]]
    end

    @existing[x[:device]] = x
  end

  disks.each_index do |index|
    dev = disks[index][:device]
    if dev.nil?
      prefix = options.fetch(:prefix, @default_prefix)
      dev = next_device(prefix=prefix)
      if dev.nil?
        raise Errors::NoDiskDeviceAvailable, prefix: prefix
      end

      disks[index][:device] = dev
      @existing[dev] = disks[index]
    end
  end
end