Class: Cisco::ItdDeviceGroup

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

Overview

node_utils class for itd_device_group

Direct Known Subclasses

ItdDeviceGroupNode

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(name, instantiate = true) ⇒ ItdDeviceGroup

Returns a new instance of ItdDeviceGroup.



24
25
26
27
28
29
30
31
# File 'lib/cisco_node_utils/itd_device_group.rb', line 24

def initialize(name, instantiate=true)
  fail TypeError unless name.is_a?(String)
  fail ArgumentError unless name.length > 0
  @name = name

  set_args_keys_default
  create if instantiate
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



22
23
24
# File 'lib/cisco_node_utils/itd_device_group.rb', line 22

def name
  @name
end

Class Method Details

.itdsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cisco_node_utils/itd_device_group.rb', line 33

def self.itds
  hash = {}
  groups = config_get('itd_device_group',
                      'all_itd_device_groups')
  return hash if groups.nil?

  groups.each do |id|
    hash[id] = ItdDeviceGroup.new(id, false)
  end
  hash
end

Instance Method Details

#attach_prefix(val, prop, prefix = nil) ⇒ Object

prepend property name prefix/keyword to value



89
90
91
92
# File 'lib/cisco_node_utils/itd_device_group.rb', line 89

def attach_prefix(val, prop, prefix=nil)
  prefix = prop.to_s if prefix.nil?
  @set_args[prop] = val.to_s.empty? ? val : "#{prefix} #{val}"
end

#createObject

PROPERTIES #



49
50
51
52
# File 'lib/cisco_node_utils/itd_device_group.rb', line 49

def create
  Feature.itd_enable
  config_set('itd_device_group', 'create', name: @name)
end

#default_probe_controlObject



129
130
131
# File 'lib/cisco_node_utils/itd_device_group.rb', line 129

def default_probe_control
  config_get_default('itd_device_group', 'probe_control')
end

#default_probe_frequencyObject



151
152
153
# File 'lib/cisco_node_utils/itd_device_group.rb', line 151

def default_probe_frequency
  config_get_default('itd_device_group', 'probe_frequency')
end

#default_probe_retry_downObject



174
175
176
# File 'lib/cisco_node_utils/itd_device_group.rb', line 174

def default_probe_retry_down
  config_get_default('itd_device_group', 'probe_retry_down')
end

#default_probe_retry_upObject



188
189
190
# File 'lib/cisco_node_utils/itd_device_group.rb', line 188

def default_probe_retry_up
  config_get_default('itd_device_group', 'probe_retry_up')
end

#default_probe_timeoutObject



202
203
204
# File 'lib/cisco_node_utils/itd_device_group.rb', line 202

def default_probe_timeout
  config_get_default('itd_device_group', 'probe_timeout')
end

#default_probe_typeObject



216
217
218
# File 'lib/cisco_node_utils/itd_device_group.rb', line 216

def default_probe_type
  config_get_default('itd_device_group', 'probe_type')
end

#destroyObject



54
55
56
# File 'lib/cisco_node_utils/itd_device_group.rb', line 54

def destroy
  config_set('itd_device_group', 'destroy', name: @name)
end

#extract_value(prop, prefix = nil) ⇒ Object

extract value of property from probe



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cisco_node_utils/itd_device_group.rb', line 71

def extract_value(prop, prefix=nil)
  prefix = prop if prefix.nil?
  probe_match = probe_get

  # matching probe not found
  return nil if probe_match.nil? # no matching probe found

  # property not defined for matching probe
  return nil unless probe_match.names.include?(prop)

  # extract and return value that follows prefix + <space>
  regexp = Regexp.new("#{Regexp.escape(prefix)} (?<extracted>.*)")
  value_match = regexp.match(probe_match[prop])
  return nil if value_match.nil?
  value_match[:extracted]
end

#probe_controlObject



115
116
117
118
119
# File 'lib/cisco_node_utils/itd_device_group.rb', line 115

def probe_control
  val = extract_value('control')
  return default_probe_control if val.nil?
  val == 'enable' ? true : default_probe_control
end

#probe_control=(control) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/cisco_node_utils/itd_device_group.rb', line 121

def probe_control=(control)
  if control
    @set_args[:control] = 'control enable'
  else
    @set_args[:control] = ''
  end
end

#probe_dns_hostObject



133
134
135
# File 'lib/cisco_node_utils/itd_device_group.rb', line 133

def probe_dns_host
  extract_value('dns_host', 'host')
end

#probe_dns_host=(dns_host) ⇒ Object



137
138
139
# File 'lib/cisco_node_utils/itd_device_group.rb', line 137

def probe_dns_host=(dns_host)
  attach_prefix(dns_host, :dns_host, 'host')
end

#probe_frequencyObject



141
142
143
144
145
# File 'lib/cisco_node_utils/itd_device_group.rb', line 141

def probe_frequency
  val = extract_value('frequency')
  return default_probe_frequency if val.nil?
  val.to_i
end

#probe_frequency=(frequency) ⇒ Object



147
148
149
# File 'lib/cisco_node_utils/itd_device_group.rb', line 147

def probe_frequency=(frequency)
  attach_prefix(frequency, :frequency)
end

#probe_getObject

probe configuration is all done in a single line (like below) probe tcp port 32 frequency 10 timeout 5 retry-down-count 3 … probe udp port 23 frequency 10 timeout 5 retry-down-count 3 … probe icmp frequency 10 timeout 5 retry-down-count 3 retry-up-count 3 probe dns host 8.8.8.8 frequency 10 timeout 5 retry-down-count 3 … also the ‘control enable’ can be set if the type is tcp or udp only probe udp port 23 control enable frequency 10 timeout 5 …



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cisco_node_utils/itd_device_group.rb', line 101

def probe_get
  str = config_get('itd_device_group', 'probe', @get_args)
  return nil if str.nil?
  regexp = Regexp.new('(?<type>\S+)'\
             ' *(?<dns_host>host \S+)?'\
             ' *(?<port>port \d+)?'\
             ' *(?<control>control \S+)?'\
             ' *(?<frequency>frequency \d+)?'\
             ' *(?<timeout>timeout \d+)'\
             ' *(?<retry_down>retry-down-count \d+)'\
             ' *(?<retry_up>retry-up-count \d+)')
  regexp.match(str)
end

#probe_portObject



155
156
157
158
# File 'lib/cisco_node_utils/itd_device_group.rb', line 155

def probe_port
  val = extract_value('port')
  val.to_i unless val.nil?
end

#probe_port=(port) ⇒ Object



160
161
162
# File 'lib/cisco_node_utils/itd_device_group.rb', line 160

def probe_port=(port)
  attach_prefix(port, :port)
end

#probe_retry_downObject



164
165
166
167
168
# File 'lib/cisco_node_utils/itd_device_group.rb', line 164

def probe_retry_down
  val = extract_value('retry_down', 'retry-down-count')
  return default_probe_retry_down if val.nil?
  val.to_i
end

#probe_retry_down=(rdc) ⇒ Object



170
171
172
# File 'lib/cisco_node_utils/itd_device_group.rb', line 170

def probe_retry_down=(rdc)
  attach_prefix(rdc, :retry_down_count, 'retry-down-count')
end

#probe_retry_upObject



178
179
180
181
182
# File 'lib/cisco_node_utils/itd_device_group.rb', line 178

def probe_retry_up
  val = extract_value('retry_up', 'retry-up-count')
  return default_probe_retry_up if val.nil?
  val.to_i
end

#probe_retry_up=(ruc) ⇒ Object



184
185
186
# File 'lib/cisco_node_utils/itd_device_group.rb', line 184

def probe_retry_up=(ruc)
  attach_prefix(ruc, :retry_up_count, 'retry-up-count')
end

#probe_set(attrs) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/cisco_node_utils/itd_device_group.rb', line 220

def probe_set(attrs)
  if attrs[:probe_type] == false
    cmd = 'probe_type'
    @set_args[:state] = 'no'
  else
    cmd = 'probe'
    set_args_keys_default
    set_args_keys(attrs)
    [:probe_type,
     :probe_dns_host,
     :probe_port,
     :probe_control,
     :probe_frequency,
     :probe_timeout,
     :probe_retry_down,
     :probe_retry_up,
    ].each do |p|
      attrs[p] = '' if attrs[p].nil?
      send(p.to_s + '=', attrs[p])
    end
    # for boolean we need to do this
    send('probe_control=', false) if attrs[:probe_control] == ''
    @get_args = @set_args
  end
  config_set('itd_device_group', cmd, @set_args)
  set_args_keys_default
end

#probe_timeoutObject



192
193
194
195
196
# File 'lib/cisco_node_utils/itd_device_group.rb', line 192

def probe_timeout
  val = extract_value('timeout')
  return default_probe_timeout if val.nil?
  val.to_i
end

#probe_timeout=(timeout) ⇒ Object



198
199
200
# File 'lib/cisco_node_utils/itd_device_group.rb', line 198

def probe_timeout=(timeout)
  attach_prefix(timeout, :timeout)
end

#probe_typeObject



206
207
208
209
210
# File 'lib/cisco_node_utils/itd_device_group.rb', line 206

def probe_type
  match = probe_get
  return default_probe_type if match.nil?
  match.names.include?('type') ? match[:type] : default_probe_type
end

#probe_type=(type) ⇒ Object



212
213
214
# File 'lib/cisco_node_utils/itd_device_group.rb', line 212

def probe_type=(type)
  @set_args[:type] = type
end

#set_args_keys(hash = {}) ⇒ Object

rubocop:disable Style/AccessorMethodName



65
66
67
68
# File 'lib/cisco_node_utils/itd_device_group.rb', line 65

def set_args_keys(hash={})
  set_args_keys_default
  @set_args = @get_args.merge!(hash) unless hash.empty?
end

#set_args_keys_defaultObject

Helper method to delete @set_args hash keys



59
60
61
62
# File 'lib/cisco_node_utils/itd_device_group.rb', line 59

def set_args_keys_default
  keys = { name: @name }
  @get_args = @set_args = keys
end