Class: Pvectl::Commands::Get::Handlers::Hosts

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

Overview

Handler for reading per-node /etc/hosts contents.

Implements ResourceHandler interface for the “hosts” resource type. /etc/hosts is a singleton resource per node — there is no cluster-wide hosts file, so a node name is always required.

Examples:

Using via ResourceRegistry

handler = ResourceRegistry.for("hosts")
hosts = handler.list(node: "pve1") # => [HostsFile]

See Also:

Instance Method Summary collapse

Methods included from ResourceHandler

#selector_class

Constructor Details

#initialize(repository: nil) ⇒ Hosts

Creates handler with optional repository for dependency injection.

Parameters:



26
27
28
# File 'lib/pvectl/commands/get/handlers/hosts.rb', line 26

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

Instance Method Details

#describe(name:, node: nil, **_opts) ⇒ Models::HostsFile

Describes the /etc/hosts contents for a single node.

Accepts either ‘name` (positional argument from describe command) or `node` (–node flag) as the node identifier.

Parameters:

  • name (String, nil)

    node name (positional)

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

    node name (–node flag, fallback)

  • _opts (Hash)

    unused, for interface compatibility

Returns:

Raises:

  • (ArgumentError)

    when no node identifier given



59
60
61
62
63
64
# File 'lib/pvectl/commands/get/handlers/hosts.rb', line 59

def describe(name:, node: nil, **_opts)
  node_name = name || node
  raise ArgumentError, "Node name required: pvectl describe hosts NODE or --node NODE" if node_name.nil? || node_name.to_s.empty?

  repository.fetch(node_name)
end

#list(node: nil, **_options) ⇒ Array<Models::HostsFile>

Returns the /etc/hosts contents for the given node, wrapped in an array.

Parameters:

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

    node name (REQUIRED — hosts is per-node)

  • _options (Hash)

    unused, for interface compatibility

Returns:

Raises:

  • (ArgumentError)

    when no node is provided



36
37
38
39
40
# File 'lib/pvectl/commands/get/handlers/hosts.rb', line 36

def list(node: nil, **_options)
  raise ArgumentError, "hosts requires --node NODE to identify which node's /etc/hosts to read" if node.nil? || node.to_s.empty?

  [repository.fetch(node)]
end

#presenterPresenters::HostsFile

Returns presenter for /etc/hosts.

Returns:



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

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