Class: Pvectl::Parsers::NetConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/parsers/net_config.rb

Overview

Parses and formats network configuration strings for Proxmox VMs.

NetConfig handles the conversion between user-friendly key=value network specifications and the format required by the Proxmox API.

Examples:

Parsing a net config string

config = NetConfig.parse("bridge=vmbr0,model=virtio,tag=100")
config[:bridge] #=> "vmbr0"
config[:model]  #=> "virtio"
config[:tag]    #=> "100"

Converting to Proxmox API format

config = { bridge: "vmbr0", tag: "100" }
NetConfig.to_proxmox(config) #=> "virtio,bridge=vmbr0,tag=100"

Constant Summary collapse

VALID_KEYS =

All recognized network configuration keys.

%w[bridge model tag firewall mtu queues].freeze
REQUIRED_KEYS =

Keys that must be present in every network configuration.

%w[bridge].freeze
OPTIONAL_FLAGS =

Optional flags appended to the Proxmox API string.

%w[tag firewall mtu queues].freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ Hash<Symbol, String>

Parses a comma-separated key=value net config string into a Hash.

Examples:

NetConfig.parse("bridge=vmbr0,model=virtio,tag=100")
#=> { bridge: "vmbr0", model: "virtio", tag: "100" }

Parameters:

  • string (String)

    net config in “key=value,key=value” format

Returns:

  • (Hash<Symbol, String>)

    parsed configuration

Raises:

  • (ArgumentError)

    if unknown keys are present or required keys are missing



39
40
41
42
43
44
45
# File 'lib/pvectl/parsers/net_config.rb', line 39

def self.parse(string)
  pairs = string.split(",").map { |pair| pair.strip.split("=", 2).map(&:strip) }
  config = pairs.to_h { |k, v| [k.to_sym, v] }

  validate!(config)
  config
end

.to_proxmox(config) ⇒ String

Converts a parsed net config Hash to a Proxmox API string.

The Proxmox API expects network specifications in the format “model,bridge=name,flag=val”. Model defaults to “virtio” when not specified.

Examples:

Minimal config

NetConfig.to_proxmox({ bridge: "vmbr0" })
#=> "virtio,bridge=vmbr0"

With optional flags

NetConfig.to_proxmox({ bridge: "vmbr0", tag: "100", firewall: "1" })
#=> "virtio,bridge=vmbr0,tag=100,firewall=1"

Parameters:

  • config (Hash<Symbol, String>)

    parsed network configuration

Returns:

  • (String)

    Proxmox API-compatible network string



63
64
65
66
67
68
69
70
71
72
# File 'lib/pvectl/parsers/net_config.rb', line 63

def self.to_proxmox(config)
  model = config[:model] || "virtio"
  parts = [model, "bridge=#{config[:bridge]}"]

  OPTIONAL_FLAGS.each do |flag|
    parts << "#{flag}=#{config[flag.to_sym]}" if config[flag.to_sym]
  end

  parts.join(",")
end