Class: Pvectl::Commands::Get::Handlers::Templates
- Inherits:
-
Object
- Object
- Pvectl::Commands::Get::Handlers::Templates
- 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).
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
-
#apply_sort(templates, sort_field) ⇒ Array
Applies sorting to templates collection.
-
#build_container_repository ⇒ Repositories::Container
Builds Container repository with connection from config.
-
#build_vm_repository ⇒ Repositories::Vm
Builds VM repository with connection from config.
-
#container_repository ⇒ Repositories::Container
Returns Container repository, creating it if necessary.
-
#initialize(vm_repository: nil, container_repository: nil) ⇒ Templates
constructor
Creates handler with optional repositories for dependency injection.
-
#list(node: nil, name: nil, type_filter: nil, sort: nil, **_options) ⇒ Array<Models::Vm, Models::Container>
Lists templates with optional filtering.
-
#presenter ⇒ Presenters::Template
Returns presenter for templates.
-
#skip_containers?(type_filter) ⇒ Boolean
Checks if containers should be skipped based on type filter.
-
#skip_vms?(type_filter) ⇒ Boolean
Checks if VMs should be skipped based on type filter.
-
#validate_type_filter!(type_filter) ⇒ void
Validates type_filter value against known types.
-
#vm_repository ⇒ Repositories::Vm
Returns VM repository, creating it if necessary.
Methods included from ResourceHandler
Constructor Details
#initialize(vm_repository: nil, container_repository: nil) ⇒ Templates
Creates handler with optional repositories for dependency injection.
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.
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_repository ⇒ Repositories::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_repository ⇒ Repositories::Vm
Builds VM repository with connection from config.
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_repository ⇒ Repositories::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.
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, **) 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 |
#presenter ⇒ Presenters::Template
Returns presenter for templates.
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.
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.
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.
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_repository ⇒ Repositories::Vm
Returns VM repository, creating it if necessary.
102 103 104 |
# File 'lib/pvectl/commands/get/handlers/templates.rb', line 102 def vm_repository @vm_repository ||= build_vm_repository end |