class Rbeapi::Api::Alias

The Alias class manages aliass entries on an EOS node.

Public Instance Methods

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

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

@since eos_version 4.13.7M

Commands

alias <name> <address>

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

@param opts [hash] Optional keyword arguments.

@option opts command [String] Configures the alias ip address

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

# File lib/rbeapi/api/alias.rb, line 129
def create(name, opts = {})
  raise ArgumentError, 'a command must be provided' unless \
      opts[:command] =~ /.+/
  command = opts.fetch(:command)
  cmd = ["alias #{name} "]
  if command =~ /\\n/
    command.split('\\n').each { |a| cmd << a }
  else
    cmd[0] << command
  end
  configure(cmd)
end
delete(name) click to toggle source

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

@since eos_version 4.13.7M

Commands

no alias <name>

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

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

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

get returns the current alias configuration hash extracted from the nodes running configuration.

@example

{
  alias: array<strings>
}

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

object from the nodes current configuration.
# File lib/rbeapi/api/alias.rb, line 54
def get(name)
  # Complex regex handles the following cases:
  #  All aliases start with 'alian <name>' followed by
  #    <space><single-line command>
  #    <carriage return><multiple lines of commands>
  pattern = /^alias #{name}((?:(?= )(?:.+?)(?=\n)|\n(?:.+?)(?=\n\!)))/m
  aliases = config.scan(pattern)
  return nil unless aliases[0]
  parse_alias_entry(name, aliases[0])
end
getall() click to toggle source

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

@example

[
  <alias>: {
    command: <string>
  },
  <alias>: {
    command: <string>
  },
  ...
]

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

entire alias collection from the nodes running configuration.  If
there are no aliass configured, this method will return an empty
hash.
# File lib/rbeapi/api/alias.rb, line 85
def getall
  entries = config.scan(/^alias (\w+)(.+)?/)
  entries.inspect
  response = {}
  entries.each do |aliases|
    response[aliases[0]] = get aliases[0]
  end
  response
end

Private Instance Methods

parse_alias_entry(name, command) click to toggle source

parse_alias_entry maps the tokens found to the hash entries.

@api private

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

expression scan of the aliass configuration.

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

# File lib/rbeapi/api/alias.rb, line 104
def parse_alias_entry(name, command)
  hsh = {}
  hsh[:name] = name
  com = command[0]
  hsh[:command] = com.strip
  hsh
end