From 7480b65e9dff0cbb1ada8f8fe4d7086882b23e28 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 8 Oct 2015 08:46:36 -0400 Subject: [PATCH] providers/virtualbox: use caps for snapshot list --- plugins/commands/snapshot/command/list.rb | 23 +++++++++++++--- plugins/providers/virtualbox/action.rb | 14 ---------- .../virtualbox/action/snapshot_list.rb | 27 ------------------- plugins/providers/virtualbox/cap.rb | 7 +++++ plugins/providers/virtualbox/plugin.rb | 5 ++++ templates/locales/en.yml | 7 +++++ 6 files changed, 39 insertions(+), 44 deletions(-) delete mode 100644 plugins/providers/virtualbox/action/snapshot_list.rb diff --git a/plugins/commands/snapshot/command/list.rb b/plugins/commands/snapshot/command/list.rb index af91e76f5..d0c433b94 100644 --- a/plugins/commands/snapshot/command/list.rb +++ b/plugins/commands/snapshot/command/list.rb @@ -5,8 +5,6 @@ module VagrantPlugins module Command class List < Vagrant.plugin("2", :command) def execute - options = {} - opts = OptionParser.new do |o| o.banner = "Usage: vagrant snapshot list [options] [vm-name]" o.separator "" @@ -18,7 +16,26 @@ module VagrantPlugins return if !argv with_target_vms(argv) do |vm| - vm.action(:snapshot_list) + if !vm.id + vm.ui.info(I18n.t("vagrant.commands.common.vm_not_created")) + next + end + + if !vm.provider.capability?(:snapshot_list) + vm.ui.info(I18n.t("vagrant.commands.snapshot.not_supported")) + next + end + + snapshots = vm.provider.capability(:snapshot_list) + if snapshots.empty? + vm.ui.output(I18n.t("vagrant.actions.vm.snapshot.list_none")) + vm.ui.detail(I18n.t("vagrant.actions.vm.snapshot.list_none_detail")) + next + end + + snapshots.each do |snapshot| + vm.ui.output(snapshot, prefix: false) + end end # Success, exit status 0 diff --git a/plugins/providers/virtualbox/action.rb b/plugins/providers/virtualbox/action.rb index 12773736d..9c4d68c72 100644 --- a/plugins/providers/virtualbox/action.rb +++ b/plugins/providers/virtualbox/action.rb @@ -43,7 +43,6 @@ module VagrantPlugins autoload :SetName, File.expand_path("../action/set_name", __FILE__) autoload :SetupPackageFiles, File.expand_path("../action/setup_package_files", __FILE__) autoload :SnapshotDelete, File.expand_path("../action/snapshot_delete", __FILE__) - autoload :SnapshotList, File.expand_path("../action/snapshot_list", __FILE__) autoload :SnapshotRestore, File.expand_path("../action/snapshot_restore", __FILE__) autoload :SnapshotSave, File.expand_path("../action/snapshot_save", __FILE__) autoload :Suspend, File.expand_path("../action/suspend", __FILE__) @@ -239,19 +238,6 @@ module VagrantPlugins end end - def self.action_snapshot_list - Vagrant::Action::Builder.new.tap do |b| - b.use CheckVirtualbox - b.use Call, Created do |env, b2| - if env[:result] - b2.use SnapshotList - else - b2.use MessageNotCreated - end - end - end - end - # This is the action that is primarily responsible for saving a snapshot def self.action_snapshot_restore Vagrant::Action::Builder.new.tap do |b| diff --git a/plugins/providers/virtualbox/action/snapshot_list.rb b/plugins/providers/virtualbox/action/snapshot_list.rb deleted file mode 100644 index 27c1e3d12..000000000 --- a/plugins/providers/virtualbox/action/snapshot_list.rb +++ /dev/null @@ -1,27 +0,0 @@ -module VagrantPlugins - module ProviderVirtualBox - module Action - class SnapshotList - def initialize(app, env) - @app = app - end - - def call(env) - snapshots = env[:machine].provider.driver.list_snapshots( - env[:machine].id) - - snapshots.each do |snapshot| - env[:machine].ui.output(snapshot, prefix: false) - end - - if snapshots.empty? - env[:machine].ui.output(I18n.t("vagrant.actions.vm.snapshot.list_none")) - env[:machine].ui.detail(I18n.t("vagrant.actions.vm.snapshot.list_none_detail")) - end - - @app.call(env) - end - end - end - end -end diff --git a/plugins/providers/virtualbox/cap.rb b/plugins/providers/virtualbox/cap.rb index e459c97bf..77f8ee1ad 100644 --- a/plugins/providers/virtualbox/cap.rb +++ b/plugins/providers/virtualbox/cap.rb @@ -22,6 +22,13 @@ module VagrantPlugins def self.nic_mac_addresses(machine) machine.provider.driver.read_mac_addresses end + + # Returns a list of the snapshots that are taken on this machine. + # + # @return [Array] Snapshot Name + def self.snapshot_list(machine) + machine.provider.driver.list_snapshots(machine.id) + end end end end diff --git a/plugins/providers/virtualbox/plugin.rb b/plugins/providers/virtualbox/plugin.rb index 18e33d4bb..84f86ba51 100644 --- a/plugins/providers/virtualbox/plugin.rb +++ b/plugins/providers/virtualbox/plugin.rb @@ -33,6 +33,11 @@ module VagrantPlugins require_relative "cap" Cap end + + provider_capability(:virtualbox, :snapshot_list) do + require_relative "cap" + Cap + end end autoload :Action, File.expand_path("../action", __FILE__) diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 5d4e4d912..c57f78688 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1501,6 +1501,13 @@ en: Post install message from the '%{name}' plugin: %{message} + snapshot: |- + not_supported: |- + This provider doesn't support snapshots. + + This may be intentional or this may be a bug. If this provider + should support snapshots, then please report this as a bug to the + maintainer of the provider. status: aborted: |- The VM is in an aborted state. This means that it was abruptly