Class: Rbeapi::Api::Iphosts

Inherits:
Entity
  • Object
show all
Defined in:
lib/rbeapi/api/iphosts.rb

Overview

The Iphosts class manages hosts entries on an EOS node.

Instance Attribute Summary

Attributes inherited from Entity

#config, #error, #node

Instance Method Summary collapse

Methods inherited from Entity

#command_builder, #configure, #configure_interface, #get_block, #initialize, instance

Constructor Details

This class inherits a constructor from Rbeapi::Api::Entity

Instance Method Details

#create(name, opts = {}) ⇒ Boolean

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

Commands

ip host <name> <address>

Parameters:

  • name (String)

    The name of the host.

  • opts (hash) (defaults to: {})

    Optional keyword arguments.

Options Hash (opts):

  • ipaddress (String)

    Configures the host ip address

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Boolean

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.

Commands

no ip host <name>

Parameters:

  • name (String)

    The host name entry to delete from the node.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



152
153
154
# File 'lib/rbeapi/api/iphosts.rb', line 152

def delete(name)
  configure("no ip host #{name}")
end

#get(name) ⇒ Hash<Symbol, Object>

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

Examples:

{
  hosts: array<strings>
}

Returns:

  • (Hash<Symbol, Object>)

    Returns the ip host resource as a hash object from the nodes current configuration.



54
55
56
57
58
# 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

#getallHash<Symbol, Object>

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.

Examples:

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

Returns:

  • (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.



80
81
82
83
84
85
86
87
# 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