Class: Pvectl::Repositories::Hosts

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/repositories/hosts.rb,
sig/pvectl/repositories/hosts.rbs

Overview

Repository for per-node /etc/hosts file contents.

Uses the /nodes/{node}/hosts endpoint to fetch and update the raw contents of /etc/hosts on a single node.

The Proxmox API returns a digest pair. The digest is used for optimistic concurrency control — the same digest must be sent back with the POST update.

Examples:

Fetching hosts file

repo = Hosts.new(connection)
hosts = repo.fetch("pve1")
hosts.data   #=> "127.0.0.1 localhost\n..."
hosts.digest #=> "abc123def..."

Updating hosts file with digest

repo.update("pve1", "127.0.0.1 localhost\n", "abc123def...")

See Also:

Instance Attribute Summary

Attributes inherited from Base

#connection

Instance Method Summary collapse

Methods inherited from Base

#extract_data, #get, #initialize, #list, #models_from, #unwrap

Constructor Details

This class inherits a constructor from Pvectl::Repositories::Base

Instance Method Details

#build_model(data) ⇒ Models::HostsFile

Builds HostsFile model from API data.



58
59
60
# File 'lib/pvectl/repositories/hosts.rb', line 58

def build_model(data)
  Models::HostsFile.new(data)
end

#fetch(node_name) ⇒ Models::HostsFile

Fetches the /etc/hosts contents for a node.



30
31
32
33
34
# File 'lib/pvectl/repositories/hosts.rb', line 30

def fetch(node_name)
  response = connection.client["nodes/#{node_name}/hosts"].get
  data = extract_data(response) || {}
  build_model(data.merge(node: node_name))
end

#update(node_name, data, digest = nil) ⇒ void

This method returns an undefined value.

Updates the /etc/hosts contents for a node.

The Proxmox POST endpoint requires data and optionally accepts digest for optimistic locking. If the on-server digest no longer matches, Proxmox returns an error which is propagated verbatim.



46
47
48
49
50
# File 'lib/pvectl/repositories/hosts.rb', line 46

def update(node_name, data, digest = nil)
  params = { data: data }
  params[:digest] = digest if digest && !digest.empty?
  connection.client["nodes/#{node_name}/hosts"].post(params)
end