class Rbeapi::Api::System

The System class configures the node system services such as hostname and domain name.

Public Class Methods

new(node) click to toggle source
Calls superclass method Rbeapi::Api::Entity::new
# File lib/rbeapi/api/system.rb, line 44
def initialize(node)
  super(node)
  @banners_re = Regexp.new(/^banner\s+(login|motd)\s?$\n(.*?)$\nEOF$\n/m)
end

Public Instance Methods

get() click to toggle source

Returns the system settings for hostname, iprouting, and banners.

@example

{
  hostname: <string>,
  iprouting: <boolean>,
  banner_motd: <string>,
  banner_login: <string>
}

@return [Hash] A Ruby hash object that provides the system settings as

key/value pairs.
# File lib/rbeapi/api/system.rb, line 62
def get
  response = {}
  response.merge!(parse_hostname(config))
  response.merge!(parse_iprouting(config))
  response.merge!(parse_timezone(config))
  response.merge!(parse_banners(config))
  response
end
set_banner(banner_type, opts = {}) click to toggle source

Configures system banners.

@param banner_type [String] Banner to be changed (likely either

login or motd).

@param opts [Hash] The configuration parameters.

@option opts value [string] The value to set for the banner.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] If true configure the command using

the default keyword. Default is false.

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

# File lib/rbeapi/api/system.rb, line 213
def set_banner(banner_type, opts = {})
  value = opts[:value]
  cmd_string = "banner #{banner_type}"
  if value
    value += "\n" if value[-1, 1] != "\n"
    cmd = [{ cmd: cmd_string, input: value }]
  else
    cmd = command_builder(cmd_string, opts)
  end
  configure(cmd)
end
set_hostname(opts = {}) click to toggle source

Configures the system hostname value in the running-config.

@param opts [Hash] The configuration parameters.

@option opts value [string] The value to set the hostname to.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] If true configure the command using

the default keyword. Default is false.

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

# File lib/rbeapi/api/system.rb, line 155
def set_hostname(opts = {})
  cmd = command_builder('hostname', opts)
  configure(cmd)
end
set_iprouting(opts = {}) click to toggle source

Configures the state of global ip routing.

@param opts [Hash] The configuration parameters.

@option opts enable [Boolean] True if ip routing should be enabled

or False if ip routing should be disabled. Default is true.

@option opts default [Boolean] If true configure the command using

the default keyword. Default is false.

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

# File lib/rbeapi/api/system.rb, line 172
def set_iprouting(opts = {})
  cmd = command_builder('ip routing', opts)
  configure(cmd)
end
set_timezone(opts = {}) click to toggle source

Configures the value of clock timezone in the running-config.

@param opts [Hash] The configuration parameters.

@option opts value [string] The value to set the clock timezone to.

@option opts enable [Boolean] If false then the command is

negated. Default is true.

@option opts default [Boolean] If true configure the command using

the default keyword. Default is false.

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

# File lib/rbeapi/api/system.rb, line 191
def set_timezone(opts = {})
  cmd = command_builder('clock timezone', opts)
  configure(cmd)
end

Private Instance Methods

parse_banners(config) click to toggle source

Parses the global config and returns the value for both motd and login banners.

@api private

@param config [String] The configuration block returned

from the node's running configuration.

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

banner is not set it will return a value of None for that key.
# File lib/rbeapi/api/system.rb, line 127
def parse_banners(config)
  motd_value = login_value = ''
  entries = config.scan(@banners_re)
  entries.each do |type, value|
    if type == 'motd'
      motd_value = value
    elsif type == 'login'
      login_value = value
    end
  end
  { banner_motd: motd_value, banner_login: login_value }
end
parse_hostname(config) click to toggle source

parse_hostname parses hostname values from the provided config.

@api private

@param config [String] The configuration block returned

from the node's running configuration.

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

# File lib/rbeapi/api/system.rb, line 80
def parse_hostname(config)
  mdata = /(?<=^hostname\s)(.+)$/.match(config)
  { hostname: mdata.nil? ? '' : mdata[1] }
end
parse_iprouting(config) click to toggle source

parse_iprouting parses ip routing from the provided config.

@api private

@param config [String] The configuration block returned

from the node's running configuration.

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

# File lib/rbeapi/api/system.rb, line 95
def parse_iprouting(config)
  mdata = /no\sip\srouting$/.match(config)
  { iprouting: mdata.nil? ? true : false }
end
parse_timezone(config) click to toggle source

parse_timezone parses the value of clock timezone.

@api private

@param config [String] The configuration block returned

from the node's running configuration.

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

# File lib/rbeapi/api/system.rb, line 110
def parse_timezone(config)
  mdata = /(?<=^clock timezone\s)(.+)$/.match(config)
  { timezone: mdata.nil? ? '' : mdata[1] }
end