diff --git a/plugins/commands/snapshot/command/save.rb b/plugins/commands/snapshot/command/save.rb index 7ec71f9c6..bd4e56c68 100644 --- a/plugins/commands/snapshot/command/save.rb +++ b/plugins/commands/snapshot/command/save.rb @@ -15,6 +15,9 @@ module VagrantPlugins o.separator "can be restored via `vagrant snapshot restore` at any point in the" o.separator "future to get back to this exact machine state." o.separator "" + o.separator "If no vm-name is given, Vagrant will take a snapshot of" + o.separator "the entire environment with the same snapshot name." + o.separator "" o.separator "Snapshots are useful for experimenting in a machine and being able" o.separator "to rollback quickly." @@ -31,20 +34,22 @@ module VagrantPlugins help: opts.help.chomp end - # If no snapshot name is given, the backup name is the same as the machine name. - # If there is a name given, we need to remove it and save it as `name`. Otherwise - # `with_target_vms` will treat the snapshot name as a guest name. - if argv.size < 2 - name = argv.first - else - name = argv.pop - end + name = argv.pop with_target_vms(argv) do |vm| if !vm.provider.capability?(:snapshot_list) raise Vagrant::Errors::SnapshotNotSupported end + # In this case, no vm name was given, and we are iterating over the + # entire environment. If a vm hasn't been created yet, we can't list + # its snapshots + if vm.id.nil? + @env.ui.warn(I18n.t("vagrant.commands.snapshot.save.vm_not_created", + name: vm.name)) + next + end + snapshot_list = vm.provider.capability(:snapshot_list) if !snapshot_list.include? name diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 49d99187d..b72176ab6 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -2053,6 +2053,9 @@ en: No pushed snapshot found! Use `vagrant snapshot push` to push a snapshot to restore to. + save: + vm_not_created: |- + Machine '%{name}' has not been created yet, and therefore cannot save snapshots. Skipping... status: aborted: |- The VM is in an aborted state. This means that it was abruptly diff --git a/test/unit/plugins/commands/snapshot/command/save_test.rb b/test/unit/plugins/commands/snapshot/command/save_test.rb index 942102c6b..eef624745 100644 --- a/test/unit/plugins/commands/snapshot/command/save_test.rb +++ b/test/unit/plugins/commands/snapshot/command/save_test.rb @@ -92,8 +92,6 @@ describe VagrantPlugins::CommandSnapshot::Command::Save do it "doesn't snapshot a non-existent machine" do machine.id = nil - expect(subject).to receive(:with_target_vms){} - expect(machine).to_not receive(:action) expect(subject.execute).to eq(0) end