Class: Pec::Network::Port

Inherits:
Object
  • Object
show all
Includes:
Query
Defined in:
lib/pec/network/port.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Query

#get_adapter, #get_ref, #list

Constructor Details

#initialize(name, ip_addr, subnet) ⇒ Port

Returns a new instance of Port.



7
8
9
10
11
# File 'lib/pec/network/port.rb', line 7

def initialize(name, ip_addr, subnet)
  @name = name
  @subnet = subnet
  @config = fetch(ip_addr)
end

Instance Attribute Details

#name ⇒ Object (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#subnet ⇒ Object (readonly)

Returns the value of attribute subnet.



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

def subnet
  @subnet
end

Instance Method Details

#create(ip) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pec/network/port.rb', line 45

def create(ip)
  options = {}
  ## parameter is network address to dhcp
  if ip.to_s != subnet["cidr"]
    options = { fixed_ips: [{ subnet_id: @subnet["id"], ip_address: ip.to_addr}]}
  end

  res = Fog::Network[:openstack].create_port(@subnet["network_id"], options)

  @config = res.data[:body]["port"] if res
  @@_list['port'] ||= []
  @@_list['port'] << @config
  true
  rescue Excon::Errors::Conflict => e
    JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
    false
end

#delete(port_id) ⇒ Object



63
64
65
66
67
68
# File 'lib/pec/network/port.rb', line 63

def delete(port_id)
  Fog::Network[:openstack].delete_port(port_id)
  rescue Excon::Errors::Conflict => e
    JSON.parse(e.response[:body]).each { |e,m| puts "#{e}:#{m["message"]}" }
    false
end

#exists? ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/pec/network/port.rb', line 17

def exists?
  !@config.nil?
end

#fetch(ip_addr) ⇒ Object



13
14
15
# File 'lib/pec/network/port.rb', line 13

def fetch(ip_addr)
  list.find {|p| p["fixed_ips"][0]["ip_address"] == ip_addr }
end

#id ⇒ Object



25
26
27
# File 'lib/pec/network/port.rb', line 25

def id
  @config["id"]
end

#ip_address ⇒ Object



33
34
35
# File 'lib/pec/network/port.rb', line 33

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

#mac_address ⇒ Object



29
30
31
# File 'lib/pec/network/port.rb', line 29

def mac_address
  @config["mac_address"]
end

#netmask ⇒ Object



41
42
43
# File 'lib/pec/network/port.rb', line 41

def netmask
  IP.new(@config["fixed_ips"][0]["ip_address"]).netmask.to_s
end

#network_id ⇒ Object



37
38
39
# File 'lib/pec/network/port.rb', line 37

def network_id
  @config["network_id"]
end

#replace(ip) ⇒ Object



70
71
72
73
74
# File 'lib/pec/network/port.rb', line 70

def replace(ip)
  delete(id)
  @@_list['port'] = nil
  create(ip)
end

#used? ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/pec/network/port.rb', line 21

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