diff --git a/lib/vagrant/util/curl_helper.rb b/lib/vagrant/util/curl_helper.rb index 67def5a82..2d49cba8a 100644 --- a/lib/vagrant/util/curl_helper.rb +++ b/lib/vagrant/util/curl_helper.rb @@ -34,6 +34,7 @@ module Vagrant redirect_notify = false logger.info("download redirected to #{location}") source_uri = URI.parse(source) + # TODO: shouldn't this be....`.first`? source_host = source_uri.host.to_s.split(".", 2).last location_host = location_uri.host.to_s.split(".", 2).last if !redirect_notify && location_host != source_host && !SILENCED_HOSTS.include?(location_host) diff --git a/plugins/commands/cloud/publish.rb b/plugins/commands/cloud/publish.rb index 5b9047500..a1294ff41 100644 --- a/plugins/commands/cloud/publish.rb +++ b/plugins/commands/cloud/publish.rb @@ -67,7 +67,7 @@ module VagrantPlugins def publish_box(org, box_name, version, provider_name, box_file, options, access_token) server_url = VagrantPlugins::CloudCommand::Util.api_server_url - @env.ui.warn("You are about to create a box on Vagrant Cloud with the following options:\n") + @env.ui.warn("You are about to publish a box on Vagrant Cloud with the following options:\n") box_opts = " #{org}/#{box_name}: (v#{version}) for provider '#{provider_name}'\n" box_opts << " Private: true\n" if options[:private] box_opts << " Automatic Release: true\n" if options[:release] @@ -89,13 +89,50 @@ module VagrantPlugins provider = VagrantCloud::Provider.new(cloud_version, provider_name, nil, options[:url], org, box_name, access_token) ui = Vagrant::UI::Prefixed.new(@env.ui, "cloud") + begin ui.info(I18n.t("cloud_command.publish.box_create")) box.create + rescue VagrantCloud::ClientError => e + if e.error_code == 422 + ui.warn("Box already exists, updating instead...") + box.update(options) + else + @env.ui.error(I18n.t("cloud_command.errors.publish.fail", org: org, box_name: box_name)) + @env.ui.error(e) + return 1 + end + end + + begin ui.info(I18n.t("cloud_command.publish.version_create")) cloud_version.create_version + rescue VagrantCloud::ClientError => e + if e.error_code == 422 + ui.warn("Version already exists, updating instead...") + cloud_version.update + else + @env.ui.error(I18n.t("cloud_command.errors.publish.fail", org: org, box_name: box_name)) + @env.ui.error(e) + return 1 + end + end + + begin ui.info(I18n.t("cloud_command.publish.provider_create")) provider.create_provider + rescue VagrantCloud::ClientError => e + if e.error_code == 422 + ui.warn("Provider already exists, updating instead...") + provider.update + else + @env.ui.error(I18n.t("cloud_command.errors.publish.fail", org: org, box_name: box_name)) + @env.ui.error(e) + return 1 + end + end + + begin if !options[:url] box_file = File.absolute_path(box_file) ui.info(I18n.t("cloud_command.publish.upload_provider", file: box_file)) diff --git a/test/unit/plugins/commands/cloud/auth/whoami_test.rb b/test/unit/plugins/commands/cloud/auth/whoami_test.rb index ff10691b6..4a0c33f31 100644 --- a/test/unit/plugins/commands/cloud/auth/whoami_test.rb +++ b/test/unit/plugins/commands/cloud/auth/whoami_test.rb @@ -47,7 +47,7 @@ describe VagrantPlugins::CloudCommand::AuthCommand::Command::Whoami do it "returns 1 if encountering an error making request" do allow(account).to receive(:validate_token). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/box/create_test.rb b/test/unit/plugins/commands/cloud/box/create_test.rb index 206e21f38..e9981531f 100644 --- a/test/unit/plugins/commands/cloud/box/create_test.rb +++ b/test/unit/plugins/commands/cloud/box/create_test.rb @@ -54,7 +54,7 @@ describe VagrantPlugins::CloudCommand::BoxCommand::Command::Create do .and_return(box) allow(box).to receive(:create). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 422)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/box/delete_test.rb b/test/unit/plugins/commands/cloud/box/delete_test.rb index 09343d1a6..2ecdc95c4 100644 --- a/test/unit/plugins/commands/cloud/box/delete_test.rb +++ b/test/unit/plugins/commands/cloud/box/delete_test.rb @@ -55,7 +55,7 @@ describe VagrantPlugins::CloudCommand::BoxCommand::Command::Delete do .and_return(box) allow(box).to receive(:delete). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/box/show_test.rb b/test/unit/plugins/commands/cloud/box/show_test.rb index 22eba219d..20c3aa2c4 100644 --- a/test/unit/plugins/commands/cloud/box/show_test.rb +++ b/test/unit/plugins/commands/cloud/box/show_test.rb @@ -56,7 +56,7 @@ describe VagrantPlugins::CloudCommand::BoxCommand::Command::Show do .and_return(box) allow(box).to receive(:read). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/box/update_test.rb b/test/unit/plugins/commands/cloud/box/update_test.rb index f0fb55961..5fb42fc5f 100644 --- a/test/unit/plugins/commands/cloud/box/update_test.rb +++ b/test/unit/plugins/commands/cloud/box/update_test.rb @@ -57,7 +57,7 @@ describe VagrantPlugins::CloudCommand::BoxCommand::Command::Update do allow(box).to receive(:update). with(organization: "vagrant", name: "box-name", description: "update", short_description: "short"). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/provider/create_test.rb b/test/unit/plugins/commands/cloud/provider/create_test.rb index bc6eafa59..770b88ee6 100644 --- a/test/unit/plugins/commands/cloud/provider/create_test.rb +++ b/test/unit/plugins/commands/cloud/provider/create_test.rb @@ -63,7 +63,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Create do and_return(provider) allow(provider).to receive(:create_provider). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 422)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/provider/delete_test.rb b/test/unit/plugins/commands/cloud/provider/delete_test.rb index d1cba4223..7573b54f8 100644 --- a/test/unit/plugins/commands/cloud/provider/delete_test.rb +++ b/test/unit/plugins/commands/cloud/provider/delete_test.rb @@ -63,7 +63,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Delete do and_return(provider) allow(provider).to receive(:delete). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/provider/update_test.rb b/test/unit/plugins/commands/cloud/provider/update_test.rb index ad0f7629c..0a03d61c3 100644 --- a/test/unit/plugins/commands/cloud/provider/update_test.rb +++ b/test/unit/plugins/commands/cloud/provider/update_test.rb @@ -63,7 +63,7 @@ describe VagrantPlugins::CloudCommand::ProviderCommand::Command::Update do and_return(provider) allow(provider).to receive(:update). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/publish_test.rb b/test/unit/plugins/commands/cloud/publish_test.rb index b6decbddc..af1c13941 100644 --- a/test/unit/plugins/commands/cloud/publish_test.rb +++ b/test/unit/plugins/commands/cloud/publish_test.rb @@ -67,9 +67,21 @@ describe VagrantPlugins::CloudCommand::Command::Publish do and_return(uploader) allow(uploader).to receive(:upload!) allow(box).to receive(:create). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end + + it "calls update if entity already exists" do + allow(provider).to receive(:upload_url).and_return("http://upload.here/there") + allow(Vagrant::Util::Uploader).to receive(:new). + with("http://upload.here/there", "/full/path/to/the/virtualbox.box", {ui: anything}). + and_return(uploader) + allow(uploader).to receive(:upload!) + allow(box).to receive(:create). + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 422)) + expect(box).to receive(:update) + expect(subject.execute).to eq(0) + end end context "with arguments and releasing a box" do diff --git a/test/unit/plugins/commands/cloud/search_test.rb b/test/unit/plugins/commands/cloud/search_test.rb index f34025a5c..924f7b0e3 100644 --- a/test/unit/plugins/commands/cloud/search_test.rb +++ b/test/unit/plugins/commands/cloud/search_test.rb @@ -45,7 +45,7 @@ describe VagrantPlugins::CloudCommand::Command::Search do allow(VagrantCloud::Search).to receive(:new). and_return(search) allow(search).to receive(:search). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end diff --git a/test/unit/plugins/commands/cloud/version/create_test.rb b/test/unit/plugins/commands/cloud/version/create_test.rb index d367360d4..f6451cce7 100644 --- a/test/unit/plugins/commands/cloud/version/create_test.rb +++ b/test/unit/plugins/commands/cloud/version/create_test.rb @@ -58,7 +58,7 @@ describe VagrantPlugins::CloudCommand::VersionCommand::Command::Create do and_return(version) allow(version).to receive(:create_version). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 422)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/version/delete_test.rb b/test/unit/plugins/commands/cloud/version/delete_test.rb index abbcc09a2..33a77aff4 100644 --- a/test/unit/plugins/commands/cloud/version/delete_test.rb +++ b/test/unit/plugins/commands/cloud/version/delete_test.rb @@ -59,7 +59,7 @@ describe VagrantPlugins::CloudCommand::VersionCommand::Command::Delete do and_return(version) allow(version).to receive(:delete). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/version/release_test.rb b/test/unit/plugins/commands/cloud/version/release_test.rb index 77d1b788f..bc5ddc131 100644 --- a/test/unit/plugins/commands/cloud/version/release_test.rb +++ b/test/unit/plugins/commands/cloud/version/release_test.rb @@ -59,7 +59,7 @@ describe VagrantPlugins::CloudCommand::VersionCommand::Command::Release do and_return(version) allow(version).to receive(:release). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/version/revoke_test.rb b/test/unit/plugins/commands/cloud/version/revoke_test.rb index c75f064fa..f933de404 100644 --- a/test/unit/plugins/commands/cloud/version/revoke_test.rb +++ b/test/unit/plugins/commands/cloud/version/revoke_test.rb @@ -59,7 +59,7 @@ describe VagrantPlugins::CloudCommand::VersionCommand::Command::Revoke do and_return(version) expect(version).to receive(:revoke). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end diff --git a/test/unit/plugins/commands/cloud/version/update_test.rb b/test/unit/plugins/commands/cloud/version/update_test.rb index 5d5b24877..d964fb364 100644 --- a/test/unit/plugins/commands/cloud/version/update_test.rb +++ b/test/unit/plugins/commands/cloud/version/update_test.rb @@ -58,7 +58,7 @@ describe VagrantPlugins::CloudCommand::VersionCommand::Command::Update do and_return(version) allow(version).to receive(:update). - and_raise(VagrantCloud::ClientError.new("Fail Message", "Message")) + and_raise(VagrantCloud::ClientError.new("Fail Message", "Message", 404)) expect(subject.execute).to eq(1) end end