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

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

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.

Returns:

  • (Hash[String, String])
{
  "vm" => "qemu",
  "qemu" => "qemu",
  "ct" => "lxc",
  "container" => "lxc",
  "lxc" => "lxc"
}.freeze
SORT_FIELDS =

Sort field mappings for template listing.

Returns:

{
  "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

#apply_sort(templates, sort_field) ⇒ Array

Applies sorting to templates collection.

Parameters:

  • templates (Array)

    templates to sort

  • sort_field (String)

    field to sort by

Returns:

  • (Array)

    sorted templates



158
159
160
161
162
163
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 158

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

  templates.sort_by(&sort_proc)
end

#build_container_repositoryRepositories::Container

Builds Container repository with connection from config.



126
127
128
129
130
131
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 126

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

#build_vm_repositoryRepositories::Vm

Builds VM repository with connection from config.

Returns:



116
117
118
119
120
121
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 116

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

#container_repositoryRepositories::Container

Returns Container repository, creating it if necessary.



109
110
111
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 109

def container_repository
  @container_repository ||= build_container_repository
end

#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

#skip_containers?(type_filter) ⇒ Boolean

Checks if containers should be skipped based on type filter.

Parameters:

  • type_filter (String, nil)

    type filter value

Returns:

  • (Boolean)


147
148
149
150
151
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 147

def skip_containers?(type_filter)
  return false if type_filter.nil?

  TYPE_MAP[type_filter] == "qemu"
end

#skip_vms?(type_filter) ⇒ Boolean

Checks if VMs should be skipped based on type filter.

Parameters:

  • type_filter (String, nil)

    type filter value

Returns:

  • (Boolean)


137
138
139
140
141
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 137

def skip_vms?(type_filter)
  return false if type_filter.nil?

  TYPE_MAP[type_filter] == "lxc"
end

#validate_type_filter!(type_filter) ⇒ void

This method returns an undefined value.

Validates type_filter value against known types.

Parameters:

  • type_filter (String, nil)

    type filter value

Raises:

  • (ArgumentError)

    if type_filter is not a recognized type



91
92
93
94
95
96
97
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 91

def validate_type_filter!(type_filter)
  return if type_filter.nil?
  return if TYPE_MAP.key?(type_filter)

  valid_types = TYPE_MAP.keys.join(", ")
  raise ArgumentError, "Unknown type: #{type_filter}. Valid types: #{valid_types}"
end

#vm_repositoryRepositories::Vm

Returns VM repository, creating it if necessary.

Returns:



102
103
104
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 102

def vm_repository
  @vm_repository ||= build_vm_repository
end