class Rbeapi::Api::Iphosts

The Iphosts class manages hosts entries on an EOS node.

Public Instance Methods

create(name, opts = {}) click to toggle source

create will create a ip host entry in the nodes current configuration with the specified address.

@since eos_version 4.13.7M

Commands

ip host <name> <address>

@param name [String] The name of the host.

@param opts [hash] Optional keyword arguments.

@option opts ipaddress [String] Configures the host ip address

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/iphosts.rb, line 122
def create(name, opts = {})
  ipaddress = opts.fetch(:ipaddress, NIL)
  # rubocop:disable Style/GuardClause, Style/ClassCheck
  if ipaddress.kind_of?(Array)
    if ipaddress.all? { |x| x =~ /(\w+\.\d+\.\d+\.\d+)/ }
      ips = opts[:ipaddress].join(' ')
      cmd = "ip host #{name} #{ips}"
      configure(cmd)
    else
      fail ArgumentError, 'option ipaddress must be a valid IP'
    end
  else
    fail ArgumentError, 'no argument given'
  end
  # rubocop:enable Style/GuardClause, Style/ClassCheck
end
delete(name) click to toggle source

delete will delete an existing ip host entry from the nodes current running configuration. If the delete method is called and the host entry does not exist, this method will succeed.

@since eos_version 4.13.7M

Commands

no ip host <name>

@param name [String] The host name entry to delete from the node.

@return [Boolean] Returns true if the command completed successfully.

# File lib/rbeapi/api/iphosts.rb, line 152
def delete(name)
  configure("no ip host #{name}")
end
get(name) click to toggle source

get returns the current ip host configuration hash extracted from the nodes running configuration.

@example

{
  hosts: array<strings>
}

@return [Hash<Symbol, Object>] Returns the ip host resource as a hash

object from the nodes current configuration.
# File lib/rbeapi/api/iphosts.rb, line 54
def get(name)
  iphost = config.scan(/^ip host #{name} ((?:\d+\.\d+\.\d+\.\d+[ ]?)*)/)
  return nil unless iphost && iphost[0]
  parse_host_entry(name, iphost[0])
end
getall() click to toggle source

getall returns a collection of ip host resource hashes from the nodes running configuration. The ip host resource collection hash is keyed by the unique host name.

@example

[
  <host>: {
    ipaddress: <string>
  },
  <host>: {
    ipaddress: <string>
  },
  ...
]

@return [Hash<Symbol, Object>] Returns a hash that represents the

entire ip host collection from the nodes running configuration.  If
there are no ip hosts configured, this method will return an empty
hash.
# File lib/rbeapi/api/iphosts.rb, line 80
def getall
  entries = config.scan(/^ip host ([^\s]+) (\d+\.\d+\.\d+\.\d+)/)
  response = {}
  entries.each do |host|
    response[host[0]] = get host[0]
  end
  response
end

Private Instance Methods

parse_host_entry(host, ipaddress) click to toggle source

parse_host_entry maps the tokens found to the hash entries.

@api private

@param host [Array] An array of values returned from the regular

expression scan of the hosts configuration.

@return [Hash<Symbol, Object>] Returns the resource hash attribute.

# File lib/rbeapi/api/iphosts.rb, line 98
def parse_host_entry(host, ipaddress)
  hsh = {}
  hsh[:name] = host
  hsh[:ipaddress] = ipaddress[0].split
  hsh
end