Class: Pvectl::Commands::Get::Handlers::Templates

Inherits:
Object
  • Object
show all
Includes:
ResourceHandler
Defined in:
lib/pvectl/commands/get/handlers/templates.rb

Overview

Handler for listing templates (both VM and container).

Queries both VM and Container repositories, filters by template? == true, and merges into a single list. Supports –type flag for filtering by resource type (vm/ct/qemu/lxc).

Examples:

Using via ResourceRegistry

handler = ResourceRegistry.for("templates")
templates = handler.list(type_filter: "vm")

See Also:

Constant Summary collapse

TYPE_MAP =

Type filter mapping — normalizes user input to API type values.

{
  "vm" => "qemu",
  "qemu" => "qemu",
  "ct" => "lxc",
  "container" => "lxc",
  "lxc" => "lxc"
}.freeze
SORT_FIELDS =

Sort field mappings for template listing.

{
  "name" => ->(t) { t.name || "" },
  "node" => ->(t) { t.node || "" },
  "type" => ->(t) { t.type || "" },
  "disk" => ->(t) { -(t.maxdisk || 0) }
}.freeze

Instance Method Summary collapse

Methods included from ResourceHandler

#selector_class

Constructor Details

#initialize(vm_repository: nil, container_repository: nil) ⇒ Templates

Creates handler with optional repositories for dependency injection.

Parameters:



45
46
47
48
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 45

def initialize(vm_repository: nil, container_repository: nil)
  @vm_repository = vm_repository
  @container_repository = container_repository
end

Instance Method Details

#list(node: nil, name: nil, type_filter: nil, sort: nil, **_options) ⇒ Array<Models::Vm, Models::Container>

Lists templates with optional filtering.

Parameters:

  • node (String, nil) (defaults to: nil)

    filter by node name

  • type_filter (String, nil) (defaults to: nil)

    filter by type (vm, ct, qemu, lxc)

  • sort (String, nil) (defaults to: nil)

    sort field

  • name (String, nil) (defaults to: nil)

    unused, interface compatibility

Returns:

Raises:

  • (ArgumentError)

    if type_filter is not a recognized type



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 58

def list(node: nil, name: nil, type_filter: nil, sort: nil, **_options)
  validate_type_filter!(type_filter)

  templates = []

  unless skip_vms?(type_filter)
    vms = vm_repository.list(node: node)
    templates.concat(vms.select(&:template?))
  end

  unless skip_containers?(type_filter)
    containers = container_repository.list(node: node)
    templates.concat(containers.select(&:template?))
  end

  templates = apply_sort(templates, sort) if sort
  templates
end

#presenterPresenters::Template

Returns presenter for templates.

Returns:



80
81
82
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 80

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