devopsgroup-io / vagrant-digitalocean

:droplet: A Vagrant provider plugin that manages DigitalOcean droplets.
Mozilla Public License 2.0
1.71k stars 182 forks source link

`bad_request` (400) Error - Your request body was malformed. #288

Closed HelgeCPH closed 4 years ago

HelgeCPH commented 4 years ago

Since some days I am having an issue with Vagrant in combination with the DigitalOcean Vagrant Provider for Vagrantfiles that I used some weeks ago to create droplets at DigitalOcean.

I made the following minimal example (The SSH key is registered at DigitalOcean, its name is stored in the environment variable SSH_KEY_NAME and my DO token is in DIGITAL_OCEAN_TOKEN):

# -*- mode: ruby -*-
# vi: set ft=ruby :

raise "SSH_KEY_NAME environment variable must be set" if ENV['SSH_KEY_NAME'].nil?
raise "DIGITAL_OCEAN_TOKEN environment variable must be set" if ENV['DIGITAL_OCEAN_TOKEN'].nil?

Vagrant.configure("2") do |config|
  config.vm.box = 'digital_ocean'
  config.vm.box_url = "https://github.com/devopsgroup-io/vagrant-digitalocean/raw/master/box/digital_ocean.box"
  config.ssh.private_key_path = '~/.ssh/id_rsa'

  config.vm.synced_folder ".", "/vagrant", type: "rsync"

  config.vm.define "experimentserver", primary: true do |server|

    server.vm.provider :digital_ocean do |provider|
      provider.ssh_key_name = ENV["SSH_KEY_NAME"]
      provider.token = ENV["DIGITAL_OCEAN_TOKEN"]
      provider.image = 'ubuntu-19-10-x64'
      provider.region = 'fra1'
      provider.size = '1gb'
    end

    server.vm.hostname = "experiment"
    server.vm.provision "shell", inline: <<-SHELL
    sudo apt-get update
    SHELL
  end
end

When I try to create the droplet, I receive the following error message (which I did not receive earlier for the same Vagrantfile):

$ vagrant up
Bringing machine 'experimentserver' up with 'digital_ocean' provider...
==> experimentserver: Using existing SSH key: LinuxNotebook
There was an issue with the request made to the DigitalOcean
API at:

Path: /v2/droplets
URI Params: {:size=>"1gb", :region=>"fra1", :image=>"ubuntu-19-10-x64", :name=>"experiment", :ssh_keys=>[26965292], :private_networking=>false, :ipv6=>false, :monitoring=>false}

The response status from the API was:

Status: 400
Response: {"id"=>"bad_request", "message"=>"Your request body was malformed."}

I am a bit puzzled by that error. When I run vagrant up with the --debug option, I do not see anything more useful.

Since the error is about the request that is sent to the DigitalOcean API, I tried to send the same payload that is reported to be erroneous manually via curl:

curl -X POST \
  -H 'Content-Type: application/json' \
  -H "Authorization: Bearer $DIGITAL_OCEAN_TOKEN" \
  -d '{"size":"1gb","region":"fra1","image":"ubuntu-19-10-x64", "name":"experiment","ssh_keys":[26965292],"private_networking":null,"ipv6":true,"backups":false,"monitoring":null}' \
  "https://api.digitalocean.com/v2/droplets"

That request works and creates the droplet.

What is it that is wrong in what I am doing or is it a bug in any of the tools?

I am experiencing the same error on Ubuntu (18.04.3) and on MacOS with the following versions of Vagrant and the plugin:

$ vagrant --version
Vagrant 2.2.7
$ vagrant plugin list
vagrant-digitalocean (0.9.3, global)
vagrant-scp (0.5.7, global)
  - Version Constraint: > 0
rdarida commented 4 years ago

I had the same problem. The vagrant plugin update vagrant-digitalocean helped me out. It updated the vagrant-digitalocean plugin to 0.9.5

seth-reeser commented 4 years ago

Hello, yes this issue has been resolved with the latest version of the plugin.