From cc638bc7aa7f1446e37b01875935a1cd6d786880 Mon Sep 17 00:00:00 2001 From: Paul Williams Date: Thu, 14 Mar 2019 20:26:37 +0000 Subject: [PATCH] Add support for floating IP Automatically assign a floating IP after the droplet is created. --- README.md | 2 ++ lib/vagrant-digitalocean/actions/create.rb | 8 ++++++++ lib/vagrant-digitalocean/config.rb | 3 +++ 3 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 8d588d5..a370256 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ The following attributes are available to further configure the provider: * A boolean flag indicating whether to enable IPv6 - `provider.region` * A string representing the region to create the new Droplet in. It defaults to `nyc2`. List available regions with the `vagrant digitalocean-list regions $DIGITAL_OCEAN_TOKEN` command. +- `provider.floating_ip` + * Automatically assigns a DigitalOcean [Floating IP](https://www.digitalocean.com/docs/networking/floating-ips/) to a Droplet after it is created. - `provider.size` * A string representing the size to use when creating a new Droplet (e.g. `1gb`). It defaults to `512mb`. List available sizes with the `vagrant digitalocean-list sizes $DIGITAL_OCEAN_TOKEN` command. - `provider.private_networking` diff --git a/lib/vagrant-digitalocean/actions/create.rb b/lib/vagrant-digitalocean/actions/create.rb index 80318c5..506276b 100644 --- a/lib/vagrant-digitalocean/actions/create.rb +++ b/lib/vagrant-digitalocean/actions/create.rb @@ -40,6 +40,14 @@ def call(env) # assign the machine id for reference in other commands @machine.id = result['droplet']['id'].to_s + # assign a specific floating IP to this droplet + if @machine.provider_config.floating_ip + result = @client.post("/v2/floating_ips/#{@machine.provider_config.floating_ip}/actions", { + :type => 'assign', + :droplet_id => @machine.id, + }) + end + # refresh droplet state with provider and output ip address droplet = Provider.droplet(@machine, :refresh => true) public_network = droplet['networks']['v4'].find { |network| network['type'] == 'public' } diff --git a/lib/vagrant-digitalocean/config.rb b/lib/vagrant-digitalocean/config.rb index eda9e55..ef4748c 100644 --- a/lib/vagrant-digitalocean/config.rb +++ b/lib/vagrant-digitalocean/config.rb @@ -15,6 +15,7 @@ class Config < Vagrant.plugin('2', :config) attr_accessor :monitoring attr_accessor :tags attr_accessor :volumes + attr_accessor :floating_ip alias_method :setup?, :setup @@ -33,6 +34,7 @@ def initialize @monitoring = UNSET_VALUE @tags = UNSET_VALUE @volumes = UNSET_VALUE + @floating_ip = UNSET_VALUE end def finalize! @@ -50,6 +52,7 @@ def finalize! @monitoring = false if @monitoring == UNSET_VALUE @tags = nil if @tags == UNSET_VALUE @volumes = nil if @volumes == UNSET_VALUE + @floating_ip = nil if @floating_ip == UNSET_VALUE end def validate(machine)