Class: Cisco::NtpServer

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/ntp_server.rb

Overview

NtpServer - node utility class for NTP Server configuration management

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

#client, client, #config_get, config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, node, #node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(opts, instantiate = true) ⇒ NtpServer


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cisco_node_utils/ntp_server.rb', line 28

def initialize(opts, instantiate=true)
  @ntpserver_id = opts['name']
  @key = opts['key']
  @minpoll = opts['minpoll']
  @maxpoll = opts['maxpoll']
  @prefer = opts['prefer'].nil? ? false : true
  @vrf = opts['vrf'].nil? ? 'default' : opts['vrf']

  hostname_regex = /^(?=.{1,255}$)[0-9A-Za-z]
  (?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?
  (?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/x

  unless @ntpserver_id =~ Resolv::AddressRegex ||
         @ntpserver_id =~ hostname_regex
    fail ArgumentError,
         "Invalid value '#{@ntpserver_id}' \
    (Must be valid IPv4/IPv6 address or hostname)"
  end

  create if instantiate
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


26
27
28
# File 'lib/cisco_node_utils/ntp_server.rb', line 26

def key
  @key
end

#maxpollObject (readonly)

Returns the value of attribute maxpoll


26
27
28
# File 'lib/cisco_node_utils/ntp_server.rb', line 26

def maxpoll
  @maxpoll
end

#minpollObject (readonly)

Returns the value of attribute minpoll


26
27
28
# File 'lib/cisco_node_utils/ntp_server.rb', line 26

def minpoll
  @minpoll
end

#preferObject (readonly)

Returns the value of attribute prefer


26
27
28
# File 'lib/cisco_node_utils/ntp_server.rb', line 26

def prefer
  @prefer
end

#vrfObject (readonly)

Returns the value of attribute vrf


26
27
28
# File 'lib/cisco_node_utils/ntp_server.rb', line 26

def vrf
  @vrf
end

Class Method Details

.ntpserversObject


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cisco_node_utils/ntp_server.rb', line 50

def self.ntpservers
  keys = %w(name prefer vrf key minpoll maxpoll)
  hash = {}
  ntpservers_list = config_get('ntp_server', 'server')
  return hash if ntpservers_list.empty?

  ntpservers_list.each do |id|
    hash[id[0]] = NtpServer.new(Hash[keys.zip(id)], false)
  end

  hash
end

Instance Method Details

#==(other) ⇒ Object


63
64
65
# File 'lib/cisco_node_utils/ntp_server.rb', line 63

def ==(other)
  name == other.name && prefer == other.prefer
end

#createObject


67
68
69
70
71
72
73
74
# File 'lib/cisco_node_utils/ntp_server.rb', line 67

def create
  config_set('ntp_server', 'server', state: '', ip: @ntpserver_id,
              prefer: (['true', true].include? @prefer) ? 'prefer' : '',
              vrf: @vrf ? "use-vrf #{@vrf}" : '',
              key: @key ? "key #{@key}" : '',
              minpoll: @minpoll ? "minpoll #{@minpoll}" : '',
              maxpoll: @maxpoll ? "maxpoll #{@maxpoll}" : '')
end

#destroyObject


76
77
78
79
80
# File 'lib/cisco_node_utils/ntp_server.rb', line 76

def destroy
  config_set('ntp_server', 'server',
             state: 'no', ip: @ntpserver_id, prefer: '', vrf: '',
             key: '', minpoll: '', maxpoll: '')
end

#nameObject


82
83
84
# File 'lib/cisco_node_utils/ntp_server.rb', line 82

def name
  @ntpserver_id
end