Class: Pvectl::Models::NetworkInterface

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/models/network_interface.rb

Overview

Represents a network interface on a Proxmox node.

Immutable domain model containing network interface attributes and helper methods for display. Created by Repositories::Node from API data.

Examples:

Creating a NetworkInterface model

iface = NetworkInterface.new(iface: "vmbr0", type: "bridge")
iface.active? #=> true

From API response

data = { "iface" => "vmbr0", "type" => "bridge", "address" => "192.168.1.10/24" }
iface = NetworkInterface.new(data)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ NetworkInterface

Creates a new NetworkInterface model from attributes.

Parameters:

  • attrs (Hash) (defaults to: {})

    NetworkInterface attributes from API (string or symbol keys)



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pvectl/models/network_interface.rb', line 48

def initialize(attrs = {})
  super
  @iface = attributes[:iface]
  @type = attributes[:type]
  @address = attributes[:address]
  @gateway = attributes[:gateway]
  @active = attributes[:active]
  @autostart = attributes[:autostart]
  @bridge_ports = attributes[:bridge_ports]
  @comments = attributes[:comments]
end

Instance Attribute Details

#activeInteger (readonly)

Returns active flag (0/1).

Returns:

  • (Integer)

    active flag (0/1)



34
35
36
# File 'lib/pvectl/models/network_interface.rb', line 34

def active
  @active
end

#addressString? (readonly)

Returns IP address with CIDR (e.g., “192.168.1.10/24”).

Returns:

  • (String, nil)

    IP address with CIDR (e.g., “192.168.1.10/24”)



28
29
30
# File 'lib/pvectl/models/network_interface.rb', line 28

def address
  @address
end

#autostartInteger (readonly)

Returns autostart flag (0/1).

Returns:

  • (Integer)

    autostart flag (0/1)



37
38
39
# File 'lib/pvectl/models/network_interface.rb', line 37

def autostart
  @autostart
end

#bridge_portsString? (readonly)

Returns bridge ports (for bridge type).

Returns:

  • (String, nil)

    bridge ports (for bridge type)



40
41
42
# File 'lib/pvectl/models/network_interface.rb', line 40

def bridge_ports
  @bridge_ports
end

#commentsString? (readonly)

Returns comments.

Returns:

  • (String, nil)

    comments



43
44
45
# File 'lib/pvectl/models/network_interface.rb', line 43

def comments
  @comments
end

#gatewayString? (readonly)

Returns gateway IP address.

Returns:

  • (String, nil)

    gateway IP address



31
32
33
# File 'lib/pvectl/models/network_interface.rb', line 31

def gateway
  @gateway
end

#ifaceString (readonly)

Returns interface name (e.g., “vmbr0”, “eth0”).

Returns:

  • (String)

    interface name (e.g., “vmbr0”, “eth0”)



22
23
24
# File 'lib/pvectl/models/network_interface.rb', line 22

def iface
  @iface
end

#typeString (readonly)

Returns interface type (bridge, bond, eth, vlan).

Returns:

  • (String)

    interface type (bridge, bond, eth, vlan)



25
26
27
# File 'lib/pvectl/models/network_interface.rb', line 25

def type
  @type
end

Instance Method Details

#active?Boolean

Checks if the interface is active.

Returns:

  • (Boolean)

    true if active flag is 1



63
64
65
# File 'lib/pvectl/models/network_interface.rb', line 63

def active?
  active == 1
end

#has_gateway?Boolean

Checks if the interface has a gateway configured.

Returns:

  • (Boolean)

    true if gateway is present and non-empty



70
71
72
# File 'lib/pvectl/models/network_interface.rb', line 70

def has_gateway?
  !gateway.nil? && !gateway.to_s.empty?
end

#ip_without_cidrString?

Returns IP address without CIDR suffix.

Examples:

iface = NetworkInterface.new(address: "192.168.1.10/24")
iface.ip_without_cidr #=> "192.168.1.10"

Returns:

  • (String, nil)

    IP address without CIDR, or nil if address is nil



80
81
82
# File 'lib/pvectl/models/network_interface.rb', line 80

def ip_without_cidr
  address&.split("/")&.first
end