Class: Pvectl::Parsers::NetConfig
- Inherits:
-
Object
- Object
- Pvectl::Parsers::NetConfig
- 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.
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
-
.parse(string) ⇒ Hash<Symbol, String>
Parses a comma-separated key=value net config string into a Hash.
-
.to_proxmox(config) ⇒ String
Converts a parsed net config Hash to a Proxmox API string.
Class Method Details
.parse(string) ⇒ Hash<Symbol, String>
Parses a comma-separated key=value net config string into a Hash.
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.
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 |