Sha256: d11ac484e6ce1ec1d7b2d429d5782ae4f1d267a6098647c4a9971ea0275cfdd6

Contents?: true

Size: 1.96 KB

Versions: 3

Compression:

Stored size: 1.96 KB

Contents

require 'log4r'

module VagrantPlugins
  module OVirtProvider
    module Action
      class SnapshotList
        def initialize(app, env)
          @logger = Log4r::Logger.new("vagrant_ovirt4::action::snapshot_list")
          @app = app
        end

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

          system_service = env[:connection].system_service

          #Find all storage domains and store the id and name in a
          # hash, so that looking them up later will be faster:
          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']]
          vm_service = env[:vms_service].vm_service(env[:machine].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

          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
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vagrant-ovirt4-2.2.0 lib/vagrant-ovirt4/action/snapshot_list.rb
vagrant-ovirt4-2.1.3 lib/vagrant-ovirt4/action/snapshot_list.rb
vagrant-ovirt4-2.1.0 lib/vagrant-ovirt4/action/snapshot_list.rb