class VagrantPlugins::OVirtProvider::Action::SnapshotList

Public Class Methods

new(app, env) click to toggle source
# 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

Public Instance Methods

call(env) click to toggle source
# 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, metadata|
      snap_description = metadata[:description]
      snap_date = metadata[: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