Class: Rbeapi::Api::Ntp
Overview
The Ntp class provides an intstance for working with the nodes NTP configuraiton.
Constant Summary collapse
- DEFAULT_SRC_INTF =
''
Instance Attribute Summary
Attributes inherited from Entity
Instance Method Summary collapse
-
#add_server(server, prefer = false) ⇒ Boolean
add_server configures a new ntp server destination hostname or ip address to the list of ntp destinations.
-
#get ⇒ nil, Hash<Symbol, Object>
get returns the nodes current ntp configure as a resource hash.
-
#parse_servers ⇒ Hash<Symbol, Object>
private
parse_servers scans the nodes configuration and parses the configured ntp server host names and/or addresses.
-
#remove_server(server) ⇒ Boolean
remove_server deletes the provided server destination from the list of ntp server destinations.
-
#set_prefer(srv, value) ⇒ Boolean
set_prefer will set the prefer keyword for the specified ntp server.
-
#set_source_interface(opts = {}) ⇒ Boolean
set_source_interface configures the ntp source value in the nodes running configuration.
Methods inherited from Entity
#configure, #get_block, #initialize, instance
Constructor Details
This class inherits a constructor from Rbeapi::Api::Entity
Instance Method Details
#add_server(server, prefer = false) ⇒ 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.
152 153 154 155 156 |
# File 'lib/rbeapi/api/ntp.rb', line 152 def add_server(server, prefer = false) cmd = "ntp server #{server}" cmd << ' prefer' if prefer configure cmd end |
#get ⇒ nil, Hash<Symbol, Object>
get returns the nodes current ntp configure as a resource hash
61 62 63 64 65 66 |
# File 'lib/rbeapi/api/ntp.rb', line 61 def get response = {} response.merge!(parse_source_interface) response.merge!(parse_servers) response end |
#parse_servers ⇒ Hash<Symbol, Object>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
parse_servers scans the nodes configuration and parses the configured ntp server host names and/or addresses. This method will also return the value of prefer. If no servers are configured, the value will be set to an empty array. The return hash is inteded to be merged into the resource hash
93 94 95 96 97 98 99 |
# File 'lib/rbeapi/api/ntp.rb', line 93 def parse_servers servers = config.scan(/(?:ntp server\s)([^\s]+)\s(prefer)?/) values = servers.each_with_object({}) do |(srv, prefer), hsh| hsh[srv] = { prefer: !prefer.nil? } end { servers: values } end |
#remove_server(server) ⇒ 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
167 168 169 |
# File 'lib/rbeapi/api/ntp.rb', line 167 def remove_server(server) configure("no ntp server #{server}") 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.
190 191 192 193 194 195 196 197 198 |
# File 'lib/rbeapi/api/ntp.rb', line 190 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 no value is provided in the options, 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 value keyword if both optiosn are specified.
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rbeapi/api/ntp.rb', line 125 def set_source_interface(opts = {}) value = opts[:value] default = opts[:default] || false case default when true cmds = 'default ntp source' when false cmds = (value ? "ntp source #{value}" : \ 'no ntp source') end configure(cmds) end |