Class: Pec::Network::Port

Inherits:
Object
  • Object
show all
Extended by:
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

fetch, get_ref

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/pec/network/port.rb', line 6

def name
  @name
end

#subnetObject (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)
  options = { security_groups: security_group_ids }
  options.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"], options)
  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

Returns:

  • (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_portObject



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

#idObject



78
79
80
# File 'lib/pec/network/port.rb', line 78

def id
  @port["id"]
end

#ip_addressObject



86
87
88
# File 'lib/pec/network/port.rb', line 86

def ip_address
  @port["fixed_ips"][0]["ip_address"]
end

#listObject



53
54
55
# File 'lib/pec/network/port.rb', line 53

def list
  Pec::Resource.get.port_list
end

#mac_addressObject



82
83
84
# File 'lib/pec/network/port.rb', line 82

def mac_address
  @port["mac_address"]
end

#netmaskObject



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_idObject



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

Returns:

  • (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

Returns:

  • (Boolean)


74
75
76
# File 'lib/pec/network/port.rb', line 74

def used?
  @port && !@port["device_owner"].empty?
end