Class: Pvectl::Commands::Get::Handlers::Containers

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

Overview

Handler for listing LXC containers.

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

Registered with ResourceRegistry on file load for "containers", "container", "ct", and "cts".

Examples:

Using via ResourceRegistry

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

See Also:

Constant Summary collapse

CTID_PATTERN =

CTID validation pattern (100-999999999)

Returns:

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

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

Returns:

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository: nil) ⇒ Containers

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/containers.rb', line 44

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

Instance Attribute Details

#repositoryRepositories::Container (readonly)

Returns repository, creating it if necessary.

Returns:

  • Container repository



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

def repository
  @repository ||= build_repository
end

Instance Method Details

#apply_sort(containers, sort_field) ⇒ Array<Models::Container>

Applies sorting to containers collection.

Parameters:

  • containers to sort

  • field to sort by

Returns:

  • sorted containers



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

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

  containers.sort_by(&sort_proc)
end

#build_repositoryRepositories::Container

Builds repository with connection from config.

Returns:

  • configured Container repository



106
107
108
109
110
111
# File 'lib/pvectl/commands/get/handlers/containers.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::Container.new(connection)
end

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

Describes a single container with comprehensive details.

Parameters:

  • CTID as string (consistent with handler interface)

  • (defaults to: nil)

    unused, for API consistency

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

Returns:

  • Container model with full details

Raises:

  • if CTID format is invalid

  • if container not found



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

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

  ctid = name.to_i
  container = repository.describe(ctid)
  raise Pvectl::ResourceNotFoundError, "Container not found: #{ctid}" if container.nil?

  container
end

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

Lists containers with optional filtering and sorting.

Parameters:

  • (defaults to: nil)

    filter by node name

  • (defaults to: nil)

    filter by container 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 Container models



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

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

#presenterPresenters::Container

Returns presenter for containers.

Returns:

  • Container presenter instance



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

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

#selector_classClass

Returns selector class for container filtering.

Returns:

  • Selectors::Container



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

def selector_class
  Pvectl::Selectors::Container
end

#valid_ctid?(ctid) ⇒ Boolean

Validates CTID format.

CTID must be a positive integer between 100 and 999999999. The minimum CTID in Proxmox is 100 (unlike VMID which can be 1).

Parameters:

  • CTID to validate

Returns:

  • true if valid



132
133
134
135
136
# File 'lib/pvectl/commands/get/handlers/containers.rb', line 132

def valid_ctid?(ctid)
  return false if ctid.nil? || ctid.to_s.empty?

  ctid.to_s.match?(CTID_PATTERN)
end