/
vip_network.rb
51 lines (45 loc) · 1.48 KB
/
vip_network.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Copyright (c) 2009-2013 VMware, Inc.
# Copyright (c) 2012 Piston Cloud Computing, Inc.
module Bosh::OpenStackCloud
##
# Represents OpenStack vip network: where users sets VM's IP (floating IP's
# in OpenStack)
class VipNetwork < Network
##
# Creates a new vip network
#
# @param [String] name Network name
# @param [Hash] spec Raw network spec
def initialize(name, spec)
super
end
##
# Configures OpenStack vip network
#
# @param [Fog::Compute::OpenStack] openstack Fog OpenStack Compute client
# @param [Fog::Compute::OpenStack::Server] server OpenStack server to
# configure
def configure(openstack, server)
if @ip.nil?
cloud_error("No IP provided for vip network `#{@name}'")
end
# Check if the OpenStack floating IP is allocated. If true, disassociate
# it from any server before associating it to the new server
with_openstack do
address = openstack.addresses.find { |a| a.ip == @ip }
if address
unless address.instance_id.nil?
@logger.info("Disassociating floating IP `#{@ip}' " \
"from server `#{address.instance_id}'")
address.server = nil
end
@logger.info("Associating server `#{server.id}' " \
"with floating IP `#{@ip}'")
address.server = server
else
cloud_error("Floating IP #{@ip} not allocated")
end
end
end
end
end