Class: VagrantPlugins::OVirtProvider::Action::SnapshotList

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-ovirt4/action/snapshot_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ SnapshotList

Returns a new instance of SnapshotList.



7
8
9
10
# File 'lib/vagrant-ovirt4/action/snapshot_list.rb', line 7

def initialize(app, env)
  @logger = Log4r::Logger.new("vagrant_ovirt4::action::snapshot_list")
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
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
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant-ovirt4/action/snapshot_list.rb', line 12

def call(env)
  env[:ui].info(I18n.t("vagrant_ovirt4.snapshot_list"))

  system_service = env[:connection].system_service

  # Find all the virtual machines and store the id and name in a
  # hash, so that looking them up later will be faster:
  vms_map = Hash[env[:vms_service].list.map { |vm| [vm.id, vm.name] }]
  
  # Same for storage domains:
  sds_service = system_service.storage_domains_service
  sds_map = Hash[sds_service.list.map { |sd| [sd.id, sd.name] }]
  
  # For each virtual machine find its snapshots, then for each snapshot
  # find its disks:
  xs = [['id', 'description', 'date']]
  vms_map.each do |vm_id, vm_name|
    vm_service = env[:vms_service].vm_service(vm_id)
    snaps_service = vm_service.snapshots_service
    snaps_map = Hash[snaps_service.list.map { |snap| [snap.id, { description: snap.description, date: snap.date }] }]
    snaps_map.each do |snap_id, |
      snap_description = [:description]
      snap_date = [:date]
      snap_service = snaps_service.snapshot_service(snap_id)
      disks_service = snap_service.disks_service
      disks_service.list.each do |disk|
        next unless disk.storage_domains.any?
        sd_id = disk.storage_domains.first.id
        sd_name = sds_map[sd_id]
        xs.push([snap_id, snap_description, snap_date.to_s])
      end
    end
  end
  widths = xs.transpose.map { |column_arr| column_arr.map(&:size).max }
  env[:machine_snapshot_list] = 
      xs.map { |row_arr| 
        row_arr.map.with_index { |str, idx| 
                "%#{widths[idx]}s" % str 
        } .join(" " * 5)
      }

  @app.call(env)
end