Class: Pvectl::Parsers::DiskConfig

Inherits:
Object
  • Object
show all
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.

Examples:

Parsing a disk config string

config = DiskConfig.parse("storage=local-lvm,size=32G,format=qcow2")
config[:storage] #=> "local-lvm"
config[:size]    #=> "32G"
config[:format]  #=> "qcow2"

Converting to Proxmox API format

config = { storage: "local-lvm", size: "32G", format: "qcow2" }
DiskConfig.to_proxmox(config) #=> "local-lvm:32,format=qcow2"

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

Class Method Details

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

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

Examples:

DiskConfig.parse("storage=local-lvm,size=32G")
#=> { storage: "local-lvm", size: "32G" }

Parameters:

  • string (String)

    disk 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/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.

Examples:

Minimal config

DiskConfig.to_proxmox({ storage: "local-lvm", size: "32G" })
#=> "local-lvm:32,format=raw"

With optional flags

DiskConfig.to_proxmox({ storage: "local-lvm", size: "32G", cache: "writeback" })
#=> "local-lvm:32,format=raw,cache=writeback"

Parameters:

  • config (Hash<Symbol, String>)

    parsed disk configuration

Returns:

  • (String)

    Proxmox API-compatible disk string



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