Class: Cisco::ItdService

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

Overview

node_utils class for itd_device_group

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) ⇒ ItdService

Returns a new instance of ItdService.



24
25
26
27
28
29
30
31
# File 'lib/cisco_node_utils/itd_service.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_service.rb', line 22

def name
  @name
end

Class Method Details

.itdsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cisco_node_utils/itd_service.rb', line 33

def self.itds
  hash = {}
  list = []
  services = config_get('itd_service',
                        'all_itds')
  return hash if services.nil?

  services.each do |service|
    # The show cmd shows more than service,
    # we get other things like device-groups etc.
    # so filter it out to just get the service
    next if service.include?('device-group')
    next if service.include?('session')
    next if service.include?('statistics')
    list << service
  end

  list.each do |id|
    hash[id] = ItdService.new(id, false)
  end
  hash
end

Instance Method Details

#access_listObject



83
84
85
# File 'lib/cisco_node_utils/itd_service.rb', line 83

def access_list
  config_get('itd_service', 'access_list', @get_args)
end

#access_list=(val) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cisco_node_utils/itd_service.rb', line 87

def access_list=(val)
  if val.empty?
    @set_args[:state] = 'no'
    @set_args[:al] = access_list
    config_set('itd_service', 'access_list', @set_args) unless
    access_list.empty?
  else
    @set_args[:state] = ''
    @set_args[:al] = val
    config_set('itd_service', 'access_list', @set_args)
  end
  set_args_keys_default
end

#createObject

PROPERTIES #



60
61
62
63
# File 'lib/cisco_node_utils/itd_service.rb', line 60

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

#default_access_listObject



101
102
103
# File 'lib/cisco_node_utils/itd_service.rb', line 101

def default_access_list
  config_get_default('itd_service', 'access_list')
end

#default_device_groupObject



123
124
125
# File 'lib/cisco_node_utils/itd_service.rb', line 123

def default_device_group
  config_get_default('itd_service', 'device_group')
end

#default_exclude_access_listObject



145
146
147
# File 'lib/cisco_node_utils/itd_service.rb', line 145

def default_exclude_access_list
  config_get_default('itd_service', 'exclude_access_list')
end

#default_fail_actionObject



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

def default_fail_action
  config_get_default('itd_service', 'fail_action')
end

#default_ingress_interfaceObject



215
216
217
# File 'lib/cisco_node_utils/itd_service.rb', line 215

def default_ingress_interface
  config_get_default('itd_service', 'ingress_interface')
end

#default_load_bal_bucketsObject



258
259
260
# File 'lib/cisco_node_utils/itd_service.rb', line 258

def default_load_bal_buckets
  config_get_default('itd_service', 'load_bal_buckets')
end

#default_load_bal_enableObject



358
359
360
# File 'lib/cisco_node_utils/itd_service.rb', line 358

def default_load_bal_enable
  config_get_default('itd_service', 'load_bal_enable')
end

#default_load_bal_mask_posObject



272
273
274
# File 'lib/cisco_node_utils/itd_service.rb', line 272

def default_load_bal_mask_pos
  config_get_default('itd_service', 'load_bal_mask_pos')
end

#default_load_bal_method_bundle_hashObject



287
288
289
# File 'lib/cisco_node_utils/itd_service.rb', line 287

def default_load_bal_method_bundle_hash
  config_get_default('itd_service', 'load_bal_method_bundle_hash')
end

#default_load_bal_method_bundle_selectObject



302
303
304
# File 'lib/cisco_node_utils/itd_service.rb', line 302

def default_load_bal_method_bundle_select
  config_get_default('itd_service', 'load_bal_method_bundle_select')
end

#default_load_bal_method_end_portObject



317
318
319
# File 'lib/cisco_node_utils/itd_service.rb', line 317

def default_load_bal_method_end_port
  config_get_default('itd_service', 'load_bal_method_end_port')
end

#default_load_bal_method_protoObject



346
347
348
# File 'lib/cisco_node_utils/itd_service.rb', line 346

def default_load_bal_method_proto
  config_get_default('itd_service', 'load_bal_method_proto')
end

#default_load_bal_method_start_portObject



331
332
333
# File 'lib/cisco_node_utils/itd_service.rb', line 331

def default_load_bal_method_start_port
  config_get_default('itd_service', 'load_bal_method_start_port')
end

#default_nat_destinationObject



403
404
405
# File 'lib/cisco_node_utils/itd_service.rb', line 403

def default_nat_destination
  config_get_default('itd_service', 'nat_destination')
end

#default_peer_localObject



426
427
428
# File 'lib/cisco_node_utils/itd_service.rb', line 426

def default_peer_local
  config_get_default('itd_service', 'peer_local')
end

#default_peer_vdcObject



455
456
457
# File 'lib/cisco_node_utils/itd_service.rb', line 455

def default_peer_vdc
  config_get_default('itd_service', 'peer_vdc')
end

#default_shutdownObject



473
474
475
# File 'lib/cisco_node_utils/itd_service.rb', line 473

def default_shutdown
  config_get_default('itd_service', 'shutdown')
end

#default_virtual_ipObject



507
508
509
# File 'lib/cisco_node_utils/itd_service.rb', line 507

def default_virtual_ip
  config_get_default('itd_service', 'virtual_ip')
end

#destroyObject



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

def destroy
  # the service MUST be shutdown before it can be destroyed
  config_set('itd_service', 'shutdown', name: @name, state: '')
  config_set('itd_service', 'destroy', name: @name)
end

#device_groupObject



105
106
107
# File 'lib/cisco_node_utils/itd_service.rb', line 105

def device_group
  config_get('itd_service', 'device_group', @get_args)
end

#device_group=(val) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/cisco_node_utils/itd_service.rb', line 109

def device_group=(val)
  if val.empty?
    @set_args[:state] = 'no'
    @set_args[:dg] = device_group
    config_set('itd_service', 'device_group', @set_args) unless
    device_group.empty?
  else
    @set_args[:state] = ''
    @set_args[:dg] = val
    config_set('itd_service', 'device_group', @set_args)
  end
  set_args_keys_default
end

#exclude_access_listObject



127
128
129
# File 'lib/cisco_node_utils/itd_service.rb', line 127

def exclude_access_list
  config_get('itd_service', 'exclude_access_list', @get_args)
end

#exclude_access_list=(val) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/cisco_node_utils/itd_service.rb', line 131

def exclude_access_list=(val)
  if val.empty?
    @set_args[:state] = 'no'
    @set_args[:al] = exclude_access_list
    config_set('itd_service', 'exclude_access_list', @set_args) unless
    exclude_access_list.empty?
  else
    @set_args[:state] = ''
    @set_args[:al] = val
    config_set('itd_service', 'exclude_access_list', @set_args)
  end
  set_args_keys_default
end

#fail_actionObject



149
150
151
# File 'lib/cisco_node_utils/itd_service.rb', line 149

def fail_action
  config_get('itd_service', 'fail_action', @get_args)
end

#fail_action=(state) ⇒ Object



153
154
155
156
157
158
# File 'lib/cisco_node_utils/itd_service.rb', line 153

def fail_action=(state)
  no_cmd = (state ? '' : 'no')
  @set_args[:state] = no_cmd
  config_set('itd_service', 'fail_action', @set_args)
  set_args_keys_default
end

#ingress_interfaceObject

this is an array like: [[‘ethernet 1/1’, ‘1.1.1.1’], [‘port-channel 100’, ‘2.2.2.2’],

‘vlan 2’, ‘3.3.3.3’]

show command output is like: Eth1/1, Po100, Vlan2 so translate back to the input format



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/cisco_node_utils/itd_service.rb', line 169

def ingress_interface
  list = config_get('itd_service', 'ingress_interface', @get_args)
  rlist = []
  list.each do |intf, next_hop|
    intf.gsub!('Eth', 'ethernet ')
    intf.gsub!('Po', 'port-channel ')
    intf.gsub!('Vlan', 'vlan ')
    next_hop = '' if next_hop.nil?
    rlist << [intf, next_hop]
  end
  rlist
end

#ingress_interface=(list) ⇒ Object

only one next-hop is allowed per interface but due to nxos issues, it allows more than one; so the workaround is to clean up the current ingress intf and configure all of them again



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/cisco_node_utils/itd_service.rb', line 199

def ingress_interface=(list)
  ingress_interface_cleanup
  @set_args[:state] = ''
  list.each do |intf, next_hop|
    @set_args[:interface] = intf
    @set_args[:next] = ''
    @set_args[:nhop] = ''
    unless next_hop == '' || next_hop == 'default'
      @set_args[:next] = 'next-hop'
      @set_args[:nhop] = next_hop
    end
    config_set('itd_service', 'ingress_interface', @set_args)
  end
  set_args_keys_default
end

#ingress_interface_cleanupObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cisco_node_utils/itd_service.rb', line 182

def ingress_interface_cleanup
  cur_list = ingress_interface
  return if cur_list.empty?
  @set_args[:state] = 'no'
  @set_args[:next] = ''
  @set_args[:nhop] = ''
  # clean up the current list first
  cur_list.each do |intf, _next_hop|
    @set_args[:interface] = intf
    config_set('itd_service', 'ingress_interface', @set_args)
  end
end

#lb_getObject

the load-balance command can take several forms like: load-balance method dst ip load-balance method dst ip-l4port tcp range 3 6 load-balance method dst ip-l4port tcp range 3 6 buckets 8 mask-position 2 load-balance buckets 8 load-balance mask-position 2



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_service.rb', line 225

def lb_get
  str = config_get('itd_service', 'load_balance', @get_args)
  return nil if str.nil?
  if str.include?('method') && str.include?('range')
    regexp = Regexp.new('load-balance *(?<bundle_select>method \S+)?'\
             ' *(?<bundle_hash>\S+)?'\
             ' *(?<proto>\S+)?'\
             ' *(?<start_port>range \d+)?'\
             ' *(?<end_port>\d+)?'\
             ' *(?<buckets>buckets \d+)?'\
             ' *(?<mask>mask-position \d+)?')
  elsif str.include?('method')
    regexp = Regexp.new('load-balance *(?<bundle_select>method \S+)?'\
             ' *(?<bundle_hash>\S+)?'\
             ' *(?<buckets>buckets \d+)?'\
             ' *(?<mask>mask-position \d+)?') unless str.include?('range')
  else
    regexp = Regexp.new('load-balance *(?<buckets>buckets \d+)?'\
             ' *(?<mask>mask-position \d+)?')
  end
  regexp.match(str)
end

#load_bal_bucketsObject



248
249
250
251
252
# File 'lib/cisco_node_utils/itd_service.rb', line 248

def load_bal_buckets
  val = Utils.extract_value(lb_get, 'buckets')
  return default_load_bal_buckets if val.nil?
  val.to_i
end

#load_bal_buckets=(buckets) ⇒ Object



254
255
256
# File 'lib/cisco_node_utils/itd_service.rb', line 254

def load_bal_buckets=(buckets)
  @set_args[:buckets] = Utils.attach_prefix(buckets, :buckets)
end

#load_bal_enableObject



350
351
352
# File 'lib/cisco_node_utils/itd_service.rb', line 350

def load_bal_enable
  lb_get.nil? ? default_load_bal_enable : true
end

#load_bal_enable=(enable) ⇒ Object



354
355
356
# File 'lib/cisco_node_utils/itd_service.rb', line 354

def load_bal_enable=(enable)
  @set_args[:state] = enable ? '' : 'no'
end

#load_bal_mask_posObject



262
263
264
265
266
# File 'lib/cisco_node_utils/itd_service.rb', line 262

def load_bal_mask_pos
  val = Utils.extract_value(lb_get, 'mask', 'mask-position')
  return default_load_bal_mask_pos if val.nil?
  val.to_i
end

#load_bal_mask_pos=(mask) ⇒ Object



268
269
270
# File 'lib/cisco_node_utils/itd_service.rb', line 268

def load_bal_mask_pos=(mask)
  @set_args[:mask] = Utils.attach_prefix(mask, :mask, 'mask-position')
end

#load_bal_method_bundle_hashObject



276
277
278
279
280
281
# File 'lib/cisco_node_utils/itd_service.rb', line 276

def load_bal_method_bundle_hash
  val = default_load_bal_method_bundle_hash
  match = lb_get
  return val if match.nil?
  match.names.include?('bundle_hash') ? match[:bundle_hash] : val
end

#load_bal_method_bundle_hash=(bh) ⇒ Object



283
284
285
# File 'lib/cisco_node_utils/itd_service.rb', line 283

def load_bal_method_bundle_hash=(bh)
  @set_args[:bundle_hash] = bh
end

#load_bal_method_bundle_selectObject



291
292
293
294
295
# File 'lib/cisco_node_utils/itd_service.rb', line 291

def load_bal_method_bundle_select
  val = Utils.extract_value(lb_get, 'bundle_select', 'method')
  return default_load_bal_method_bundle_select if val.nil?
  val
end

#load_bal_method_bundle_select=(bs) ⇒ Object



297
298
299
300
# File 'lib/cisco_node_utils/itd_service.rb', line 297

def load_bal_method_bundle_select=(bs)
  @set_args[:bundle_select] =
      Utils.attach_prefix(bs, :bundle_select, 'method')
end

#load_bal_method_end_portObject



306
307
308
309
310
311
# File 'lib/cisco_node_utils/itd_service.rb', line 306

def load_bal_method_end_port
  val = default_load_bal_method_end_port
  match = lb_get
  return val if match.nil?
  match.names.include?('end_port') ? match[:end_port].to_i : val
end

#load_bal_method_end_port=(enport) ⇒ Object



313
314
315
# File 'lib/cisco_node_utils/itd_service.rb', line 313

def load_bal_method_end_port=(enport)
  @set_args[:endPort] = enport
end

#load_bal_method_protoObject



335
336
337
338
339
340
# File 'lib/cisco_node_utils/itd_service.rb', line 335

def load_bal_method_proto
  val = default_load_bal_method_proto
  match = lb_get
  return val if match.nil?
  match.names.include?('proto') ? match[:proto] : val
end

#load_bal_method_proto=(proto) ⇒ Object



342
343
344
# File 'lib/cisco_node_utils/itd_service.rb', line 342

def load_bal_method_proto=(proto)
  @set_args[:proto] = proto
end

#load_bal_method_start_portObject



321
322
323
324
325
# File 'lib/cisco_node_utils/itd_service.rb', line 321

def load_bal_method_start_port
  val = Utils.extract_value(lb_get, 'start_port', 'range')
  return default_load_bal_method_start_port if val.nil?
  val.to_i
end

#load_bal_method_start_port=(start) ⇒ Object



327
328
329
# File 'lib/cisco_node_utils/itd_service.rb', line 327

def load_bal_method_start_port=(start)
  @set_args[:start_port] = Utils.attach_prefix(start, :start_port, 'range')
end

#load_balance_set(attrs) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/cisco_node_utils/itd_service.rb', line 362

def load_balance_set(attrs)
  set_args_keys_default
  set_args_keys(attrs)
  [:load_bal_buckets,
   :load_bal_mask_pos,
   :load_bal_method_bundle_hash,
   :load_bal_method_bundle_select,
   :load_bal_method_end_port,
   :load_bal_method_start_port,
   :load_bal_method_proto,
   :load_bal_enable,
  ].each do |p|
    attrs[p] = '' if attrs[p].nil? || attrs[p] == false
    send(p.to_s + '=', attrs[p])
  end
  # for boolean we need to do this
  send('load_bal_enable=', false) if attrs[:load_bal_enable] == ''
  if Platform.image_version[/9.3\(1\)/] && @set_args[:state] == 'no'
    # In the 9.3(1) release image, the load_balance config cannot
    # be removed using 'no load-balance'.  It requires a valid
    # parameter but it can be any parameter, even if not configured.
    # For this version only we send the following command to
    # remove load-balance config: 'no load-balance mask-position 0'
    @set_args[:mask] = 'mask-position 0'
  end
  @get_args = @set_args
  config_set('itd_service', 'load_balance', @set_args)
  set_args_keys_default
end

#nat_destinationObject



392
393
394
# File 'lib/cisco_node_utils/itd_service.rb', line 392

def nat_destination
  config_get('itd_service', 'nat_destination', @get_args)
end

#nat_destination=(state) ⇒ Object



396
397
398
399
400
401
# File 'lib/cisco_node_utils/itd_service.rb', line 396

def nat_destination=(state)
  no_cmd = (state ? '' : 'no')
  @set_args[:state] = no_cmd
  config_set('itd_service', 'nat_destination', @set_args)
  set_args_keys_default
end

#peer_localObject



407
408
409
# File 'lib/cisco_node_utils/itd_service.rb', line 407

def peer_local
  config_get('itd_service', 'peer_local', @get_args)
end

#peer_local=(val) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/cisco_node_utils/itd_service.rb', line 411

def peer_local=(val)
  if val.empty?
    @set_args[:state] = 'no'
    current_peer_local = peer_local
    @set_args[:service] = current_peer_local
    config_set('itd_service', 'peer_local', @set_args) unless
    current_peer_local.nil? || current_peer_local.empty?
  else
    @set_args[:state] = ''
    @set_args[:service] = val
    config_set('itd_service', 'peer_local', @set_args)
  end
  set_args_keys_default
end

#peer_vdcObject

peer_vdc is an array of vdc and service



431
432
433
# File 'lib/cisco_node_utils/itd_service.rb', line 431

def peer_vdc
  config_get('itd_service', 'peer_vdc', @get_args)
end

#peer_vdc=(parray) ⇒ Object

peer_vdc is an array of vdc and service only one peer_vdc is allowed per service ex: [‘switch’, ‘myservice’]



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/cisco_node_utils/itd_service.rb', line 438

def peer_vdc=(parray)
  if parray.empty?
    @set_args[:state] = 'no'
    current_peer_vdc = peer_vdc
    @set_args[:vdc] = current_peer_vdc[0]
    @set_args[:service] = current_peer_vdc[1]
    config_set('itd_service', 'peer_vdc', @set_args) unless
    current_peer_vdc[0].nil? || current_peer_vdc[1].nil?
  else
    @set_args[:state] = ''
    @set_args[:vdc] = parray[0]
    @set_args[:service] = parray[1]
    config_set('itd_service', 'peer_vdc', @set_args)
  end
  set_args_keys_default
end

#set_args_keys(hash = {}) ⇒ Object

rubocop:disable Style/AccessorMethodName



78
79
80
81
# File 'lib/cisco_node_utils/itd_service.rb', line 78

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



72
73
74
75
# File 'lib/cisco_node_utils/itd_service.rb', line 72

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

#shutdownObject

show command shows nothing when the service is shutdown which is default, but it shows “no shut” when it is not shut



462
463
464
# File 'lib/cisco_node_utils/itd_service.rb', line 462

def shutdown
  config_get('itd_service', 'shutdown', @get_args)
end

#shutdown=(state) ⇒ Object



466
467
468
469
470
471
# File 'lib/cisco_node_utils/itd_service.rb', line 466

def shutdown=(state)
  no_cmd = (state ? '' : 'no')
  @set_args[:state] = no_cmd
  config_set('itd_service', 'shutdown', @set_args)
  set_args_keys_default
end

#virtual_ipObject



477
478
479
# File 'lib/cisco_node_utils/itd_service.rb', line 477

def virtual_ip
  config_get('itd_service', 'virtual_ip', @get_args)
end

#virtual_ip=(values) ⇒ Object

VIP is a large string like: virtual ip 2.2.2.2 10.0.0.0 udp 10 advertise enable device-group icmpGroup virtual ip 2.2.2.2 10.0.0.0 udp 10 advertise enable virtual ip 2.2.2.2 10.0.0.0 udp 10 virtual ip 2.2.2.2 10.0.0.0 all of the above are unique and can be added one after the other the entire string is unique but not individual parts of it currently, only one VIP can be configured due to nxos issue else, the switch crashes, this limitation will be set in puppet manifest. Also remove the current VIPs before configuring more



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/cisco_node_utils/itd_service.rb', line 491

def virtual_ip=(values)
  @set_args[:state] = 'no'
  list = virtual_ip
  # remove all the virtual configs first
  list.each do |line|
    @set_args[:string] = line
    config_set('itd_service', 'virtual_ip', @set_args)
  end
  @set_args[:state] = ''
  values.each do |value|
    @set_args[:string] = value
    config_set('itd_service', 'virtual_ip', @set_args)
  end
  set_args_keys_default
end