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

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

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 Attribute Summary collapse

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 Attribute Details

#repositoryRepositories::Hosts (readonly)

Returns repository, creating it if necessary.

Returns:



71
72
73
# File 'lib/pvectl/commands/get/handlers/hosts.rb', line 71

def repository
  @repository ||= build_repository
end

Instance Method Details

#build_repositoryRepositories::Hosts

Builds repository with connection from config.

Returns:



78
79
80
81
82
83
# File 'lib/pvectl/commands/get/handlers/hosts.rb', line 78

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

#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

  • name: (String, nil)
  • node: (String, nil) (defaults to: nil)
  • opts (Object)

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

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

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