Class: Rbeapi::Api::Ntp

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

Overview

The Ntp class provides an instance for working with the nodes NTP configuration.

Constant Summary collapse

DEFAULT_TRST_KEY =
''.freeze
DEFAULT_SRC_INTF =
''.freeze
SERVER_REGEXP =

Regular expression to extract a NTP server’s attributes from the running-configuration text. The explicit [ ] spaces enable line wrapping and indentation with the /x flag.

/^(?:ntp[ ]server)
(?:(?:[ ]vrf[ ])([^\s]+))?
[ ]([^\s]+)
([ ]prefer)?
(?:(?:[ ]minpoll[ ])(\d+))?
(?:(?:[ ]maxpoll[ ])(\d+))?
(?:(?:[ ]source[ ])([^\s]+))?
(?:(?:[ ]key[ ])(\d+))?/x
AUTH_KEY_REGEXP =

Regular expression to extract NTP authentication-keys from the running-configuration text. The explicit [ ] spaces enable line wrapping and indentation with the /x flag.

/^(?:ntp[ ]authentication-key[ ])
(\d+)[ ](\w+)[ ](\d+)[ ](\w+)/x

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

#add_server(server, prefer = false, opts = {}) ⇒ Boolean

add_server configures a new ntp server destination hostname or ip address to the list of ntp destinations. The optional prefer argument configures the server as a preferred (true) or not (false) ntp destination.

Parameters:

  • server (String)

    The IP address or FQDN of the NTP server to be removed from the configuration.

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

    Optional keyword arguments.

  • opts (defaults to: {})

    vrf [String] The VRF instance this server is bound to

  • opts (defaults to: {})

    minpoll [Integer] The minimum poll interval

  • opts (defaults to: {})

    maxpoll [Integer] The maximum poll interval

  • opts (defaults to: {})

    source [String] The source interface used to reach server

  • opts (defaults to: {})

    key [Integer] The authentication key used to communicate with server

  • prefer (Boolean) (defaults to: false)

    Appends the prefer keyword argument to the command if this value is true.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.



361
362
363
364
365
366
367
368
369
370
371
# File 'lib/rbeapi/api/ntp.rb', line 361

def add_server(server, prefer = false, opts = {})
  cmd = 'ntp server '
  cmd << "vrf #{opts[:vrf]} " if opts[:vrf]
  cmd << server.to_s
  cmd << ' prefer' if prefer || opts[:prefer].to_s == 'true'
  cmd << " minpoll #{opts[:minpoll]} " if opts[:minpoll]
  cmd << " maxpoll #{opts[:maxpoll]} " if opts[:maxpoll]
  cmd << " source #{opts[:source_interface]} " if opts[:source_interface]
  cmd << " key #{opts[:key]} " if opts[:key]
  configure(cmd)
end

#getnil, Hash<Symbol, Object>

get returns the nodes current ntp configure as a resource hash.

Examples:

{
  authenticate: [true, false],
  source_interface: <string>,
  servers: {
    prefer: [true, false]
  }
}

Returns:

  • (nil, Hash<Symbol, Object>)

    Returns the ntp resource as a Hash.



81
82
83
84
85
86
87
88
89
# File 'lib/rbeapi/api/ntp.rb', line 81

def get
  response = {}
  response.merge!(parse_authenticate)
  response.merge!(parse_source_interface)
  response.merge!(parse_servers)
  response.merge!(parse_trusted_key)
  response.merge!(parse_auth_keys)
  response
end

#remove_server(server, vrf = nil) ⇒ Boolean

remove_server deletes the provided server destination from the list of ntp server destinations. If the ntp server does not exist in the list of servers, this method will return successful

Parameters:

  • server (String)

    The IP address or FQDN of the NTP server to be removed from the configuration.

  • vrf (String) (defaults to: nil)

    The VRF of the NTP server to be removed from the configuration.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.



385
386
387
# File 'lib/rbeapi/api/ntp.rb', line 385

def remove_server(server, vrf = nil)
  configure("no ntp server #{vrf.nil? ? '' : "vrf #{vrf} "}#{server}")
end

#set_authenticate(opts = {}) ⇒ Boolean

set_authenticate configures ntp authentication in the nodes running configuration. If the enable keyword is false, then ntp authentication is configured with the no keyword argument. If the default keyword argument is provided and set to true, the value is configured used the default keyword. The default keyword takes precedence over the enable keyword if both options are specified.

Commands

ntp authenticate
no ntp authenticate
default ntp authenticate

Parameters:

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

    Optional keyword arguments.

Options Hash (opts):

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the ntp source value using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



209
210
211
212
# File 'lib/rbeapi/api/ntp.rb', line 209

def set_authenticate(opts = {})
  cmd = command_builder('ntp authenticate', opts)
  configure(cmd)
end

#set_authentication_key(opts = {}) ⇒ Boolean

set_authentication_key configures the ntp authentication-keys in the device running configuration. If the enable keyword is false, then the ntp source is configured with the no keyword argument. If the default keyword argument is provided and set to true, the value is configured used the default keyword. The default keyword takes precedence over the enable keyword if both options are specified.

Commands

ntp trusted-key <key> <algorithm> <mode> <password>
no ntp trusted-key <key>
default ntp trusted-key <key>

default value is 7

Parameters:

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

    Optional keyword arguments.

Options Hash (opts):

  • algorithm (String)

    Encryption algorithm to use, md5/sha1

  • default (Boolean)

    Configure the ntp source value using the default keyword.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • key (Integer)

    The authentication-key to configure

  • mode (Integer)

    Password mode: 0 plain-text, 7 encrypted

  • password (String)

    Password to use for authentication-key

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/rbeapi/api/ntp.rb', line 247

def set_authentication_key(opts = {})
  cmd = command_builder('ntp authentication-key', opts)
  configure(cmd)

  algorithm = opts[:algorithm]
  default = opts[:default] || false
  enable = opts.fetch(:enable, true)
  key = opts[:key]
  mode = opts.fetch(:mode, 7)
  password = opts[:password]

  case default
  when true
    cmds = "default ntp authentication-key #{key}"
  when false
    cmds = if enable
             "ntp authentication-key #{key} #{algorithm} #{mode} "\
             "#{password}"
           else
             "no ntp authentication-key #{key}"
           end
  end
  configure cmds
end

#set_prefer(srv, value) ⇒ Boolean

set_prefer will set the prefer keyword for the specified ntp server. If the server does not already exist in the configuration, it will be added and the prefer keyword will be set.

Commands

ntp server <srv> prefer
no ntp server <srv> prefer

Parameters:

  • srv (String)

    The IP address or hostname of the ntp server to configure with the prefer value.

  • value (Boolean)

    The value to configure for prefer. If true the prefer value is configured for the server. If false, then the prefer value is removed.

Returns:

  • (Boolean)

    Returns true if the commands completed successfully.

Since:

  • eos_version 4.13.7M



408
409
410
411
412
413
414
415
416
# File 'lib/rbeapi/api/ntp.rb', line 408

def set_prefer(srv, value)
  case value
  when true
    cmds = "ntp server #{srv} prefer"
  when false
    cmds = ["no ntp server #{srv} prefer", "ntp server #{srv}"]
  end
  configure cmds
end

#set_source_interface(opts = {}) ⇒ Boolean

set_source_interface configures the ntp source value in the nodes running configuration. If the enable keyword is false, then the ntp source is configured with the no keyword argument. If the default keyword argument is provided and set to true, the value is configured used the default keyword. The default keyword takes precedence over the enable keyword if both options are specified.

Commands

ntp source <value>
no ntp source
default ntp source

Parameters:

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

    Optional keyword arguments.

Options Hash (opts):

  • value (String)

    The value to configure the ntp source in the nodes configuration.

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the ntp source value using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



299
300
301
302
# File 'lib/rbeapi/api/ntp.rb', line 299

def set_source_interface(opts = {})
  cmd = command_builder('ntp source', opts)
  configure(cmd)
end

#set_trusted_key(opts = {}) ⇒ Boolean

set_trusted_key configures the ntp trusted-keys in the device running configuration. If the enable keyword is false, then the ntp authentication-key is configured with the no keyword argument. If the default keyword argument is provided and set to true, the value is configured using the default keyword. The default keyword takes precedence over the enable keyword if both options are specified.

Commands

ntp authentication-key <key> <algorithm> <mode> <password>
no ntp authentication-key <key>
default ntp trusted-key <key>

Parameters:

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

    Optional keyword arguments.

Options Hash (opts):

  • value (Integer)

    authentication-key id

  • enable (Boolean)

    If false then the command is negated. Default is true.

  • default (Boolean)

    Configure the ntp source value using the default keyword.

Returns:

  • (Boolean)

    Returns true if the command completed successfully.

Since:

  • eos_version 4.13.7M



330
331
332
333
# File 'lib/rbeapi/api/ntp.rb', line 330

def set_trusted_key(opts = {})
  cmd = command_builder('ntp trusted-key', opts)
  configure(cmd)
end