Class: Pec::Network::Port
- Inherits:
-
Object
- Object
- Pec::Network::Port
show all
- Includes:
- Query
- Defined in:
- lib/pec/network/port.rb
Constant Summary
collapse
- @@use_ip_list =
[]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Query
#get_adapter, #get_ref, #list
Constructor Details
#initialize(name, ip_addr, subnet, security_groups) ⇒ Port
Returns a new instance of Port.
8
9
10
11
12
13
|
# File 'lib/pec/network/port.rb', line 8
def initialize(name, ip_addr, subnet, security_groups)
@name = name
@subnet = subnet
@security_groups = security_groups
@config = fetch(ip_addr)
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
6
7
8
|
# File 'lib/pec/network/port.rb', line 6
def name
@name
end
|
#subnet ⇒ Object
Returns the value of attribute subnet.
6
7
8
|
# File 'lib/pec/network/port.rb', line 6
def subnet
@subnet
end
|
Instance Method Details
#assign!(ip) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/pec/network/port.rb', line 15
def assign!(ip)
if request_any_address?(ip)
free = fetch_free_port
if free
ip = IP.new("#{free["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}")
@config = free
end
end
case
when exists? && !used?
replace(ip)
when !exists?
create(ip)
when used?
raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!")
end
end
|
#create(ip) ⇒ Object
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/pec/network/port.rb', line 80
def create(ip)
options = { security_groups: @security_groups }
if ip.to_s != subnet["cidr"]
options.merge!({ fixed_ips: [{ subnet_id: @subnet["id"], ip_address: ip.to_addr}]})
end
response = Fog::Network[:openstack].create_port(@subnet["network_id"], options)
if response
@config = response.data[:body]["port"]
@@use_ip_list << response.data[:body]["port"]["fixed_ips"][0]["ip_address"]
response.data[:body]["port"]["id"]
end
end
|
#delete(ip) ⇒ Object
94
95
96
97
|
# File 'lib/pec/network/port.rb', line 94
def delete(ip)
port = fetch(ip.to_addr)
response = Fog::Network[:openstack].delete_port(port["id"]) if port
end
|
#exists? ⇒ Boolean
52
53
54
|
# File 'lib/pec/network/port.rb', line 52
def exists?
!@config.nil?
end
|
#fetch(ip_addr) ⇒ Object
39
40
41
|
# File 'lib/pec/network/port.rb', line 39
def fetch(ip_addr)
list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
end
|
#fetch_free_port ⇒ Object
43
44
45
46
47
48
49
50
|
# File 'lib/pec/network/port.rb', line 43
def fetch_free_port
list.find do |p|
p["fixed_ips"][0]["subnet_id"] == @subnet["id"] &&
p["device_owner"].empty? &&
p["admin_state_up"] &&
!@@use_ip_list.include?(p["fixed_ips"][0]["ip_address"])
end
end
|
#id ⇒ Object
60
61
62
|
# File 'lib/pec/network/port.rb', line 60
def id
@config["id"]
end
|
#ip_address ⇒ Object
68
69
70
|
# File 'lib/pec/network/port.rb', line 68
def ip_address
@config["fixed_ips"][0]["ip_address"]
end
|
#mac_address ⇒ Object
64
65
66
|
# File 'lib/pec/network/port.rb', line 64
def mac_address
@config["mac_address"]
end
|
#netmask ⇒ Object
76
77
78
|
# File 'lib/pec/network/port.rb', line 76
def netmask
IP.new(@config["fixed_ips"][0]["ip_address"]).netmask.to_s
end
|
#network_id ⇒ Object
72
73
74
|
# File 'lib/pec/network/port.rb', line 72
def network_id
@config["network_id"]
end
|
#replace(ip) ⇒ Object
99
100
101
|
# File 'lib/pec/network/port.rb', line 99
def replace(ip)
create(ip) if delete(ip)
end
|
#request_any_address?(ip) ⇒ Boolean
35
36
37
|
# File 'lib/pec/network/port.rb', line 35
def request_any_address?(ip)
ip.to_s == subnet["cidr"]
end
|
#used? ⇒ Boolean
56
57
58
|
# File 'lib/pec/network/port.rb', line 56
def used?
@config && !@config["device_owner"].empty?
end
|