Class: Pvectl::Parsers::DiskConfig
- Inherits:
-
Object
- Object
- Pvectl::Parsers::DiskConfig
- Defined in:
- lib/pvectl/parsers/disk_config.rb
Overview
Parses and formats disk configuration strings for Proxmox VMs.
DiskConfig handles the conversion between user-friendly key=value disk specifications and the format required by the Proxmox API.
Constant Summary collapse
- VALID_KEYS =
All recognized disk configuration keys.
%w[storage size format cache discard ssd iothread backup].freeze
- REQUIRED_KEYS =
Keys that must be present in every disk configuration.
%w[storage size].freeze
- OPTIONAL_FLAGS =
Optional flags appended to the Proxmox API string.
%w[cache discard ssd iothread backup].freeze
Class Method Summary collapse
-
.parse(string) ⇒ Hash<Symbol, String>
Parses a comma-separated key=value disk config string into a Hash.
-
.to_proxmox(config) ⇒ String
Converts a parsed disk config Hash to a Proxmox API string.
Class Method Details
.parse(string) ⇒ Hash<Symbol, String>
Parses a comma-separated key=value disk config string into a Hash.
39 40 41 42 43 44 45 |
# File 'lib/pvectl/parsers/disk_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 disk config Hash to a Proxmox API string.
The Proxmox API expects disk specifications in the format “storage:size,format=fmt,flag=val”. Size is extracted as a numeric value (without the “G” suffix). Format defaults to “raw” when not specified.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pvectl/parsers/disk_config.rb', line 64 def self.to_proxmox(config) size_num = config[:size].to_s.gsub(/[^0-9]/, "") format = config[:format] || "raw" parts = ["#{config[:storage]}:#{size_num}", "format=#{format}"] OPTIONAL_FLAGS.each do |flag| parts << "#{flag}=#{config[flag.to_sym]}" if config[flag.to_sym] end parts.join(",") end |