Skip to content

Commit

Permalink
Add config param floating_ip_pool_always_allocate
Browse files Browse the repository at this point in the history
Fix #61
  • Loading branch information
julienvey committed Sep 5, 2014
1 parent 7523749 commit 4de7ae7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ vagrant will authenticate against the UK authentication endpoint.
* `image` - The name of the image to use for the VM
* `floating_ip` - The floating IP to associate with the VM. This IP must be formerly allocated.
* `floating_ip_pool` - The floating IP Pool from which a floating IP will be allocated to be associated with the VM. alternative to the `floating_ip` option.
* `floating_ip_pool_always_allocate` - if set to true, vagrant will always reallocate floating ip instead of trying to reuse unassigned ones
* `availability_zone` - Nova Availability zone used when creating VM

#### Networks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def resolve_floating_ip(env)
if config.floating_ip_pool
floating_ips.each do |single|
return single.ip if single.pool == config.floating_ip_pool && single.instance_id.nil?
end
end unless config.floating_ip_pool_always_allocate
return nova.allocate_floating_ip(env, config.floating_ip_pool).ip
else
floating_ips.each do |ip|
Expand Down
8 changes: 8 additions & 0 deletions source/lib/vagrant-openstack-provider/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class Config < Vagrant.plugin('2', :config)
# @return [String]
attr_accessor :floating_ip_pool

# if set to true, vagrant will always reallocate floating ip instead of trying to reuse unassigned ones
# default to false
#
# @return [Boolean]
attr_accessor :floating_ip_pool_always_allocate

# Sync folder method. Can be either "rsync" or "none"
#
# @return [String]
Expand Down Expand Up @@ -112,6 +118,7 @@ def initialize
@ssh_timeout = UNSET_VALUE
@floating_ip = UNSET_VALUE
@floating_ip_pool = UNSET_VALUE
@floating_ip_pool_always_allocate = UNSET_VALUE
@sync_method = UNSET_VALUE
@availability_zone = UNSET_VALUE
@networks = []
Expand All @@ -132,6 +139,7 @@ def finalize!
@rsync_includes = nil if @rsync_includes.empty?
@floating_ip = nil if @floating_ip == UNSET_VALUE
@floating_ip_pool = nil if @floating_ip_pool == UNSET_VALUE
@floating_ip_pool_always_allocate = false if floating_ip_pool_always_allocate == UNSET_VALUE
@sync_method = 'rsync' if @sync_method == UNSET_VALUE
@keypair_name = nil if @keypair_name == UNSET_VALUE
@public_key_path = nil if @public_key_path == UNSET_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
config.stub(:availability_zone) { 'AZ-01' }
config.stub(:floating_ip) { nil }
config.stub(:floating_ip_pool) { nil }
config.stub(:floating_ip_pool_always_allocate) { false }
config.stub(:keypair_name) { nil }
config.stub(:public_key_path) { nil }
config.stub(:networks) { nil }
Expand Down Expand Up @@ -116,13 +117,31 @@

context 'with config.floating_pool specified' do
context 'if any ip in the same pool is available' do
it 'return one of the available ips' do
nova.stub(:get_all_floating_ips).with(anything) do
[FloatingIP.new('80.81.82.84', 'pool-1', '1234'),
FloatingIP.new('80.81.82.83', 'pool-1', nil)]
context 'with config.floating_pool_always_allocate true' do
it 'allocate a new floating_ip from the pool' do
config.stub(:floating_ip_pool_always_allocate) { true }
nova.stub(:get_all_floating_ips).with(anything) do
[FloatingIP.new('80.81.82.84', 'pool-1', '1234'),
FloatingIP.new('80.81.82.83', 'pool-1', nil)]
end
nova.stub(:allocate_floating_ip).with(env, 'pool-1') do
FloatingIP.new('80.81.82.84', 'pool-1', nil)
end
config.stub(:floating_ip_pool) { 'pool-1' }
@action.resolve_floating_ip(env).should eq('80.81.82.84')
end
end

context 'with config.floating_pool_always_allocate false' do
it 'return one of the available ips' do
config.stub(:floating_ip_pool_always_allocate) { false }
nova.stub(:get_all_floating_ips).with(anything) do
[FloatingIP.new('80.81.82.84', 'pool-1', '1234'),
FloatingIP.new('80.81.82.83', 'pool-1', nil)]
end
config.stub(:floating_ip_pool) { 'pool-1' }
@action.resolve_floating_ip(env).should eq('80.81.82.83')
end
config.stub(:floating_ip_pool) { 'pool-1' }
@action.resolve_floating_ip(env).should eq('80.81.82.83')
end
end

Expand Down
1 change: 1 addition & 0 deletions source/spec/vagrant-openstack-provider/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
its(:public_key_path) { should be_nil }
its(:availability_zone) { should be_nil }
its(:ssh_username) { should be_nil }
its(:floating_ip_pool_always_allocate) { should be_false }
end

describe 'overriding defaults' do
Expand Down

0 comments on commit 4de7ae7

Please sign in to comment.