Class: Pec::Network::Port
- Extended by:
- Query
- Defined in:
- lib/pec/network/port.rb
Constant Summary collapse
- @@use_ip_list =
[]
Class Method Summary collapse
- .admin_state_up?(port) ⇒ Boolean
- .append_assigned_ip(response) ⇒ Object
- .assign(name, ip, subnet, security_group_ids) ⇒ Object
- .assigned_ip?(port) ⇒ Boolean
- .create(ip, subnet, security_group_ids) ⇒ Object
- .delete(ip) ⇒ Object
- .fetch_by_ip(ip_addr) ⇒ Object
- .get_free_port(subnet) ⇒ Object
- .get_free_port_ip(ip, subnet) ⇒ Object
- .ip_from_port(port) ⇒ Object
- .port_from_response(response) ⇒ Object
- .recreate(ip, subnet, security_group_ids) ⇒ Object
- .request_any_address?(ip, subnet) ⇒ Boolean
- .same_subnet?(port, subnet) ⇒ Boolean
- .set_fixed_ip(options, subnet, ip) ⇒ Object
- .set_security_group(security_group_ids) ⇒ Object
- .unused?(port) ⇒ Boolean
Methods included from Query
Class Method Details
.admin_state_up?(port) ⇒ Boolean
98 99 100 |
# File 'lib/pec/network/port.rb', line 98 def admin_state_up?(port) port["admin_state_up"] end |
.append_assigned_ip(response) ⇒ Object
52 53 54 |
# File 'lib/pec/network/port.rb', line 52 def append_assigned_ip(response) @@use_ip_list << ip_from_port(port_from_response(response)) end |
.assign(name, ip, subnet, security_group_ids) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/pec/network/port.rb', line 9 def assign(name, ip, subnet, security_group_ids) ip = get_free_port_ip(ip, subnet) if request_any_address?(ip, subnet) port_state = Pec::Network::PortState.new(name, fetch_by_ip(ip.to_addr)) assign_port = case when port_state.exists? && !port_state.used? recreate(ip, subnet, security_group_ids) when !port_state.exists? create(ip, subnet, security_group_ids) when port_state.used? raise(Pec::Errors::Port, "ip:#{ip.to_addr} is used!") end Pec::Network::PortState.new(name, assign_port) end |
.assigned_ip?(port) ⇒ Boolean
56 57 58 |
# File 'lib/pec/network/port.rb', line 56 def assigned_ip?(port) @@use_ip_list.include?(ip_from_port(port)) end |
.create(ip, subnet, security_group_ids) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/pec/network/port.rb', line 24 def create(ip, subnet, security_group_ids) = set_security_group(security_group_ids) = set_fixed_ip(, subnet, ip) response = Pec::Resource.get.create_port(subnet["network_id"], ) raise(Pec::Errors::Port, "ip:#{ip.to_addr} is not created!") unless response[:status] == 201 append_assigned_ip(response) port_from_response(response) end |
.delete(ip) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/pec/network/port.rb', line 33 def delete(ip) target_port = fetch_by_ip(ip.to_addr) response = Pec::Resource.get.delete_port(target_port["id"]) if target_port raise(Pec::Errors::Host, "ip:#{ip.to_addr} response err status:#{response[:status]}") unless response[:status] == 204 true end |
.fetch_by_ip(ip_addr) ⇒ Object
65 66 67 |
# File 'lib/pec/network/port.rb', line 65 def fetch_by_ip(ip_addr) list.find {|p| ip_from_port(p) == ip_addr } end |
.get_free_port(subnet) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/pec/network/port.rb', line 77 def get_free_port(subnet) list.find do |p| same_subnet?(p, subnet) && unused?(p) && admin_state_up?(p) && !assigned_ip?(p) end end |
.get_free_port_ip(ip, subnet) ⇒ Object
60 61 62 63 |
# File 'lib/pec/network/port.rb', line 60 def get_free_port_ip(ip, subnet) port = get_free_port(subnet) port ? IP.new("#{ip_from_port(port)}/#{ip.pfxlen}") : ip end |
.ip_from_port(port) ⇒ Object
73 74 75 |
# File 'lib/pec/network/port.rb', line 73 def ip_from_port(port) port["fixed_ips"][0]["ip_address"] end |
.port_from_response(response) ⇒ Object
69 70 71 |
# File 'lib/pec/network/port.rb', line 69 def port_from_response(response) response.data[:body]["port"] end |
.recreate(ip, subnet, security_group_ids) ⇒ Object
40 41 42 |
# File 'lib/pec/network/port.rb', line 40 def recreate(ip, subnet, security_group_ids) create(ip, subnet, security_group_ids) if delete(ip) end |
.request_any_address?(ip, subnet) ⇒ Boolean
86 87 88 |
# File 'lib/pec/network/port.rb', line 86 def request_any_address?(ip, subnet) ip.to_s == subnet["cidr"] end |
.same_subnet?(port, subnet) ⇒ Boolean
90 91 92 |
# File 'lib/pec/network/port.rb', line 90 def same_subnet?(port, subnet) port["fixed_ips"][0]["subnet_id"] == subnet["id"] end |
.set_fixed_ip(options, subnet, ip) ⇒ Object
48 49 50 |
# File 'lib/pec/network/port.rb', line 48 def set_fixed_ip(, subnet, ip) ip.to_s != subnet["cidr"] ? .merge({ fixed_ips: [{ subnet_id: subnet["id"], ip_address: ip.to_addr}]}) : end |
.set_security_group(security_group_ids) ⇒ Object
44 45 46 |
# File 'lib/pec/network/port.rb', line 44 def set_security_group(security_group_ids) { security_groups: security_group_ids } end |
.unused?(port) ⇒ Boolean
94 95 96 |
# File 'lib/pec/network/port.rb', line 94 def unused?(port) port["device_owner"].empty? end |