Class: Pec::Network::Port
- Inherits:
-
Object
- Object
- Pec::Network::Port
- Extended by:
- Query
- Defined in:
- lib/pec/network/port.rb
Constant Summary collapse
- @@use_ip_list =
[]
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subnet ⇒ Object
readonly
Returns the value of attribute subnet.
Instance Method Summary collapse
- #assign(name, ip, subnet, security_group_ids) ⇒ Object
- #create(ip, subnet, security_group_ids) ⇒ Object
- #delete(ip) ⇒ Object
- #exists? ⇒ Boolean
- #fetch_by_ip(ip_addr) ⇒ Object
- #fetch_free_port ⇒ Object
- #id ⇒ Object
- #ip_address ⇒ Object
- #list ⇒ Object
- #mac_address ⇒ Object
- #netmask ⇒ Object
- #network_id ⇒ Object
- #recreate(ip, subnet, security_group_ids) ⇒ Object
- #request_any_address?(ip) ⇒ Boolean
- #used? ⇒ Boolean
Methods included from Query
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/pec/network/port.rb', line 6 def name @name end |
#subnet ⇒ Object (readonly)
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(name, ip, subnet, security_group_ids) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pec/network/port.rb', line 9 def assign(name, ip, subnet, security_group_ids) @name = name @subnet = subnet # dhcp ip recycle if request_any_address?(ip) @port = fetch_free_port ip = IP.new("#{@port["fixed_ips"][0]["ip_address"]}/#{ip.pfxlen}") unless @port.nil? end case when exists? && !used? recreate(ip, subnet, security_group_ids) when !exists? create(ip, subnet, security_group_ids) when used? raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!") end self end |
#create(ip, subnet, security_group_ids) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/pec/network/port.rb', line 29 def create(ip, subnet, security_group_ids) = { security_groups: security_group_ids } .merge!({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) if ip.to_s != subnet["cidr"] response = Pec::Resource.get.create_port(subnet["network_id"], ) if response @port = 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
40 41 42 43 |
# File 'lib/pec/network/port.rb', line 40 def delete(ip) target_port = fetch_by_ip(ip.to_addr) response = Pec::Resource.get.delete_port(target_port["id"]) if target_port end |
#exists? ⇒ Boolean
70 71 72 |
# File 'lib/pec/network/port.rb', line 70 def exists? !@port.nil? end |
#fetch_by_ip(ip_addr) ⇒ Object
57 58 59 |
# File 'lib/pec/network/port.rb', line 57 def fetch_by_ip(ip_addr) list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr } end |
#fetch_free_port ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/pec/network/port.rb', line 61 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
78 79 80 |
# File 'lib/pec/network/port.rb', line 78 def id @port["id"] end |
#ip_address ⇒ Object
86 87 88 |
# File 'lib/pec/network/port.rb', line 86 def ip_address @port["fixed_ips"][0]["ip_address"] end |
#list ⇒ Object
53 54 55 |
# File 'lib/pec/network/port.rb', line 53 def list Pec::Resource.get.port_list end |
#mac_address ⇒ Object
82 83 84 |
# File 'lib/pec/network/port.rb', line 82 def mac_address @port["mac_address"] end |
#netmask ⇒ Object
94 95 96 |
# File 'lib/pec/network/port.rb', line 94 def netmask IP.new(@port["fixed_ips"][0]["ip_address"]).netmask.to_s end |
#network_id ⇒ Object
90 91 92 |
# File 'lib/pec/network/port.rb', line 90 def network_id @port["network_id"] end |
#recreate(ip, subnet, security_group_ids) ⇒ Object
45 46 47 |
# File 'lib/pec/network/port.rb', line 45 def recreate(ip, subnet, security_group_ids) create(ip, subnet, security_group_ids) if delete(ip) end |
#request_any_address?(ip) ⇒ Boolean
49 50 51 |
# File 'lib/pec/network/port.rb', line 49 def request_any_address?(ip) ip.to_s == subnet["cidr"] end |
#used? ⇒ Boolean
74 75 76 |
# File 'lib/pec/network/port.rb', line 74 def used? @port && !@port["device_owner"].empty? end |