Class: Pvectl::Commands::Get::Handlers::Vms

Inherits:
Object
  • Object
show all
Includes:
ResourceHandler
Defined in:
lib/pvectl/commands/get/handlers/vms.rb,
sig/pvectl/commands/get/handlers/vms.rbs

Overview

Handler for listing QEMU virtual machines.

Implements ResourceHandler interface for the "vms" resource type. Uses Repositories::Vm for data access and Presenters::Vm for formatting.

Registered with ResourceRegistry on file load for both "vms" and "vm".

Examples:

Using via ResourceRegistry

handler = ResourceRegistry.for("vms")
vms = handler.list(node: "pve1")
presenter = handler.presenter

See Also:

Constant Summary collapse

VMID_PATTERN =

VMID validation pattern (1-999999999)

Returns:

/\A[1-9]\d{0,8}\z/
SORT_FIELDS =

Sort field mappings. Negative values for descending sort (higher values first).

Returns:

{
  "name" => ->(v) { v.name || "" },
  "node" => ->(v) { v.node || "" },
  "cpu" => ->(v) { -(v.cpu || 0) },
  "memory" => ->(v) { -(v.mem || 0) },
  "disk" => ->(v) { -(v.disk || 0) },
  "netin" => ->(v) { -(v.netin || 0) },
  "netout" => ->(v) { -(v.netout || 0) }
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository: nil) ⇒ Vms

Creates handler with optional repository for dependency injection.

Parameters:

  • (defaults to: nil)

    repository (default: create new)

  • (defaults to: nil)


44
45
46
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 44

def initialize(repository: nil)
  @repository = repository
end

Instance Attribute Details

#repositoryRepositories::Vm (readonly)

Returns repository, creating it if necessary.

Returns:

  • VM repository



99
100
101
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 99

def repository
  @repository ||= build_repository
end

Instance Method Details

#apply_sort(vms, sort_field) ⇒ Array<Models::Vm>

Applies sorting to VMs collection.

Parameters:

  • VMs to sort

  • field to sort by

Returns:

  • sorted VMs



118
119
120
121
122
123
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 118

def apply_sort(vms, sort_field)
  sort_proc = SORT_FIELDS[sort_field.to_s]
  return vms unless sort_proc

  vms.sort_by(&sort_proc)
end

#build_repositoryRepositories::Vm

Builds repository with connection from config.

Returns:

  • configured VM repository



106
107
108
109
110
111
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 106

def build_repository
  config_service = Pvectl::Config::Service.new
  config_service.load
  connection = Pvectl::Connection.new(config_service.current_config)
  Pvectl::Repositories::Vm.new(connection)
end

#describe(name:, node: nil, args: [], vmid: nil) ⇒ Models::Vm

Describes a single VM with comprehensive details.

Parameters:

  • VMID as string (consistent with handler interface)

  • (defaults to: nil)

    unused, for API consistency

  • (defaults to: nil)
  • (defaults to: [])
  • (defaults to: nil)

Returns:

  • VM model with full details

Raises:

  • if VMID format is invalid

  • if VM not found



84
85
86
87
88
89
90
91
92
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 84

def describe(name:, node: nil, args: [], vmid: nil)
  raise ArgumentError, "Invalid VMID: must be positive integer (1-999999999)" unless valid_vmid?(name)

  vmid = name.to_i
  vm = repository.describe(vmid)
  raise Pvectl::ResourceNotFoundError, "VM not found: #{vmid}" if vm.nil?

  vm
end

#list(node: nil, name: nil, args: [], storage: nil, sort: nil, **_options) ⇒ Array<Models::Vm>

Lists VMs with optional filtering and sorting.

Parameters:

  • (defaults to: nil)

    filter by node name

  • (defaults to: nil)

    filter by VM name

  • (defaults to: [])

    unused, for interface compatibility

  • (defaults to: nil)

    unused, for interface compatibility

  • (defaults to: nil)

    sort field (name, node, cpu, memory, disk, netin, netout)

  • (defaults to: nil)
  • (defaults to: nil)
  • (defaults to: [])
  • (defaults to: nil)
  • (defaults to: nil)

Returns:

  • collection of VM models



63
64
65
66
67
68
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 63

def list(node: nil, name: nil, args: [], storage: nil, sort: nil, **_options)
  vms = repository.list(node: node)
  vms = vms.select { |vm| vm.name == name } if name
  vms = apply_sort(vms, sort) if sort
  vms
end

#presenterPresenters::Vm

Returns presenter for VMs.

Returns:

  • VM presenter instance



73
74
75
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 73

def presenter
  Pvectl::Presenters::Vm.new
end

#selector_classClass

Returns selector class for VM filtering.

Returns:

  • Selectors::Vm



51
52
53
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 51

def selector_class
  Pvectl::Selectors::Vm
end

#valid_vmid?(vmid) ⇒ Boolean

Validates VMID format.

Parameters:

  • VMID to validate

Returns:

  • true if valid



129
130
131
132
133
# File 'lib/pvectl/commands/get/handlers/vms.rb', line 129

def valid_vmid?(vmid)
  return false if vmid.nil? || vmid.to_s.empty?

  vmid.to_s.match?(VMID_PATTERN)
end