Class: PuppetX::CiscoIOS::Utility

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/cisco_ios/utility.rb

Overview

Helper functions for the Cisco IOS module

Class Method Summary collapse

Class Method Details

.attribute_safe_to_run(command_hash, attribute) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 152

def self.attribute_safe_to_run(command_hash, attribute)
  device_type = PuppetX::CiscoIOS::Utility.ios_device_type
  exclusions = command_hash.dig('attributes', attribute, 'exclusions')
  # try device specific command
  attribute_is_empty = command_hash.dig('attributes', attribute, device_type).nil?
  # try default command
  attribute_is_empty = command_hash.dig('attributes', attribute, 'default').nil? if attribute_is_empty
  if !exclusions.nil? && (!safe_to_run(exclusions) || attribute_is_empty)
    Puppet.debug "This attribute '#{attribute}', is not available for this device "\
                 "'#{@facts['hardwaremodel']}'"\
                 ", version '#{@facts['operatingsystemrelease']}' "\
                 "and/or OS family '#{@facts['os']['family']}'"
    return false
  end
  true
end

.attribute_value_foraged_from_command_hash(command_hash, attribute, key, attribute_can_be_nil = false) ⇒ Object

for a command_hash entry try to retrive advice specific attribute value first then the default value


attributes:

mtu:
  '4948':
    get_value: 'regex for mtu'
  'default:
    get_value: 'different regex for mtu'


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 113

def self.attribute_value_foraged_from_command_hash(command_hash, attribute, key, attribute_can_be_nil = false)
  device_type = PuppetX::CiscoIOS::Utility.ios_device_type
  # see if there is a device specific entry
  unless device_type.nil?
    return_val = command_hash.dig('attributes', attribute, device_type, key)
  end
  # if not try default
  if return_val.nil?
    return_val = command_hash.dig('attributes', attribute, 'default', key)
  end
  if !attribute_can_be_nil && return_val.nil?
    raise "This key 'command_hash => attributes => #{attribute} => #{device_type}/default => #{key}' is not in the #{command_hash}"
  end
  return_val
end

.build_commmands_from_attribute_set_values(instance, commands_hash) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 234

def self.build_commmands_from_attribute_set_values(instance, commands_hash)
  command_lines = []
  # check to see if the device can run the command at all
  PuppetX::CiscoIOS::Utility.resource_safe_for_device(commands_hash)
  instance.each do |key, value|
    if key != :ensure && !commands_hash.dig('attributes', key.to_s, 'exclusions').nil?
      next unless attribute_safe_to_run(commands_hash, key.to_s)
    end

    command_line = ''
    # if print_key exists then print the key, otherwise dont
    print_key = false
    if value == 'unset'
      command_line = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, key.to_s, 'unset_value', true)
    elsif key != :ensure
      command_line = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, key.to_s, 'set_value', true)
      # if print_key exists then print the key, otherwise dont
      print_key = !PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, key.to_s, 'print_key', true).nil?
    end
    command_line = insert_attribute_into_command_line(command_line, key, value, print_key)
    command_line = command_line.to_s.gsub(%r{<\S*>}, '')
    command_line = command_line.strip
    command_lines << command_line if command_line != ''
  end
  command_lines
end

.commands_from_diff_of_two_arrays(commands_hash, is, should, attribute) ⇒ Object



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 349

def self.commands_from_diff_of_two_arrays(commands_hash, is, should, attribute)
  is = [] if is.nil?
  should = [] if should.nil?

  new_entities =  should - is
  remove_entities = is - should

  array_of_commands = []

  new_entities.each do |new_entity|
    add_command = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'set_value')
    add_command = add_command.gsub(%r{<#{attribute}>}, new_entity.to_s).strip
    array_of_commands.push(add_command)
  end

  remove_entities.each do |remove_entity|
    remove_command = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'unset_value')
    remove_command = remove_command.gsub(%r{<#{attribute}>}, remove_entity.to_s)
    array_of_commands.push(remove_command)
  end
  array_of_commands
end

.convert_enable_to_string(enable_value) ⇒ Object



291
292
293
294
295
296
297
298
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 291

def self.convert_enable_to_string(enable_value)
  return_value = if enable_value == false
                   'no'
                 else
                   ''
                 end
  return_value
end

.convert_level_name_to_int(level_enum) ⇒ Object



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 300

def self.convert_level_name_to_int(level_enum)
  level = if level_enum == 'debugging'
            7
          elsif level_enum == 'informational'
            6
          elsif level_enum == 'notifications'
            5
          elsif level_enum == 'warnings'
            4
          elsif level_enum == 'errors'
            3
          elsif level_enum == 'critical'
            2
          elsif level_enum == 'alerts'
            1
          elsif level_enum == 'emergencies'
            0
          else
            raise "Cannot convert logging level '#{level_enum}' to an integer."
          end
  level
end

.convert_modelled_speed_value_to_int(speed_value) ⇒ Object



336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 336

def self.convert_modelled_speed_value_to_int(speed_value)
  speed_value = if speed_value == '10m'
                  '10'
                elsif speed_value == '100m'
                  '100'
                elsif speed_value == '1g'
                  '1000'
                else
                  speed_value
                end
  speed_value
end

.convert_network_trunk_mode_cli(trunk_mode_output) ⇒ Object



372
373
374
375
376
377
378
379
380
381
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 372

def self.convert_network_trunk_mode_cli(trunk_mode_output)
  if trunk_mode_output == 'dynamic auto'
    trunk_mode_output = 'dynamic_auto'
  elsif trunk_mode_output == 'dynamic desirable'
    trunk_mode_output = 'dynamic_desirable'
  elsif trunk_mode_output == 'static access'
    trunk_mode_output = 'access'
  end
  trunk_mode_output
end

.convert_network_trunk_mode_modelled(trunk_mode_output) ⇒ Object



383
384
385
386
387
388
389
390
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 383

def self.convert_network_trunk_mode_modelled(trunk_mode_output)
  if trunk_mode_output == 'dynamic_auto'
    trunk_mode_output = 'dynamic auto'
  elsif trunk_mode_output == 'dynamic_desirable'
    trunk_mode_output = 'dynamic desirable'
  end
  trunk_mode_output
end

.convert_no_to_boolean(value) ⇒ Object



282
283
284
285
286
287
288
289
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 282

def self.convert_no_to_boolean(value)
  return_value = if value.nil?
                   true
                 else
                   false
                 end
  return_value
end

.convert_speed_int_to_modelled_value(speed_value) ⇒ Object



323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 323

def self.convert_speed_int_to_modelled_value(speed_value)
  speed = if speed_value == '10'
            '10m'
          elsif speed_value == '100'
            '100m'
          elsif speed_value == '1000'
            '1g'
          else
            speed_value
          end
  speed
end

.convert_to_offsets(widths, line_length, min_field_size) ⇒ Object



448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 448

def self.convert_to_offsets(widths, line_length, min_field_size)
  offsets = []
  position = 0

  total_title_width = 0
  widths.each do |width|
    total_title_width += width
  end

  if total_title_width < line_length
    widths[-1] = line_length - total_title_width + widths.last
  end

  widths.each.with_index do |width, index|
    # If field is less than minimum size, get from previous field.
    # Used for the right hand justified Interface status - Speed column
    while width < min_field_size
      widths[index] = widths[index] + 1
      widths[index - 1] = widths[index - 1] - 1
      width = widths[index]
    end
  end
  widths.each.with_index do |width, _index|
    offsets << (position...(width + position))
    position += width
  end

  offsets
end

.detect_ipv4_or_ipv6(address) ⇒ Object



273
274
275
276
277
278
279
280
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 273

def self.detect_ipv4_or_ipv6(address)
  # Is it IPv4?
  return "ipv4 #{address}" if address =~ Resolv::IPv4::Regex
  # Is it IPv6?
  return "ipv6 #{address}" if address =~ Resolv::IPv6::Regex
  # Some other type of hostname that is neither IPv4 or IPv6, just return
  address
end

.device_match_ok(exclusion_hash) ⇒ Object

Return false if the device is on the exclusion list true otherwise



42
43
44
45
46
47
48
49
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 42

def self.device_match_ok(exclusion_hash)
  exclusion_hash.each do |exclusion|
    next if exclusion['device'].nil?
    next unless PuppetX::CiscoIOS::Utility.ios_device_type =~ %r{#{exclusion['device']}}
    return false
  end
  true
end

.device_safe_instance(change, commands_hash) ⇒ Object



497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 497

def self.device_safe_instance(change, commands_hash)
  new_should = {}
  change.each do |key, value|
    next unless PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, key.to_s)
    new_should[key] = value
  end
  if change[:name]
    new_should[:name] = change[:name]
  end
  if change[:ensure]
    new_should[:ensure] = change[:ensure]
  end
  new_should
end

.enforce_simple_types(context, return_value) ⇒ Object



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 512

def self.enforce_simple_types(context, return_value)
  return_value.each do |individual_value_hash|
    individual_value_hash.each do |k, v|
      next if k == :title
      type_to_use = context.type.definition[:attributes][k][:type]

      string_t = Puppet::Pops::Types::TypeFactory.string
      boolean_t = Puppet::Pops::Types::TypeFactory.boolean

      calculated_type = Puppet::Pops::Types::TypeParser.new.parse(type_to_use)

      # Are you an integer?
      int_result = begin
        Integer(v)
      rescue
        false
      end

      if int_result
        if Puppet::Pops::Types::TypeCalculator.instance?(calculated_type, int_result)
          individual_value_hash[k] = v.to_i
        end
        # Is our type a string?
      elsif Puppet::Pops::Types::TypeCalculator.assignable?(calculated_type, string_t)
        individual_value_hash[k] = v.to_s
        # Is our type a boolean?
      elsif Puppet::Pops::Types::TypeCalculator.assignable?(calculated_type, boolean_t)
        individual_value_hash[k] = if v.to_s.casecmp('true').zero?
                                     true
                                   else
                                     false
                                   end
      end
    end
  end
  return_value
end

.extract_values(line, offsets) ⇒ Object



478
479
480
481
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 478

def self.extract_values(line, offsets)
  return if line.nil? || line.empty?
  offsets.map { |range| line[range].strip }
end

.facts(facts) ⇒ Object

rubocop:disable Style/TrivialAccessors



55
56
57
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 55

def self.facts(facts) # rubocop:disable Style/TrivialAccessors
  @facts = facts
end

.generate_server_groups_command_string(instance) ⇒ Object



434
435
436
437
438
439
440
441
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 434

def self.generate_server_groups_command_string(instance)
  return '' if instance[:server_groups].nil?
  server_groups_string = ''
  instance[:server_groups].each do |server_group|
    server_groups_string += " group #{server_group}"
  end
  server_groups_string
end

.get_instances(command_hash) ⇒ Object



137
138
139
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 137

def self.get_instances(command_hash)
  value_foraged_from_command_hash(command_hash, 'get_instances')
end

.get_interface_names(command_hash) ⇒ Object



129
130
131
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 129

def self.get_interface_names(command_hash)
  value_foraged_from_command_hash(command_hash, 'get_interfaces_command')
end

.get_interface_status_command(commands_hash, name) ⇒ Object



443
444
445
446
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 443

def self.get_interface_status_command(commands_hash, name)
  instance_status_command = PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'get_speed_status')
  instance_status_command.gsub(%r{<name>}, name)
end

.get_values(command_hash) ⇒ Object



133
134
135
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 133

def self.get_values(command_hash)
  value_foraged_from_command_hash(command_hash, 'get_values')
end

.insert_attribute_into_command_line(command_line, key, value, print_key) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 261

def self.insert_attribute_into_command_line(command_line, key, value, print_key)
  command_line = if value.nil?
                   # no value so remove the key from the command_line
                   command_line.to_s.gsub(%r{<#{key}>}, '')
                 elsif print_key
                   command_line.to_s.gsub(%r{<#{key}>}, value ? "#{key} #{value}" : '')
                 else
                   command_line.to_s.gsub(%r{<#{key}>}, value ? value.to_s : '')
                 end
  command_line
end

.instances_contains_name(instances, name) ⇒ Object



425
426
427
428
429
430
431
432
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 425

def self.instances_contains_name(instances, name)
  instances.each do |instance|
    if instance[:name] == name
      return true
    end
  end
  false
end

.ios_device_typeObject



59
60
61
62
63
64
65
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 59

def self.ios_device_type
  unless @facts.nil? || @facts['hardwaremodel'].nil?
    device_type = @facts['hardwaremodel'][%r{(\d\d\d\d)}, 1]
    return device_type
  end
  'default'
end

.ios_os_family_typeObject



67
68
69
70
71
72
73
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 67

def self.ios_os_family_type
  if @facts && !@facts['os']['family'].nil?
    family_type = @facts['os']['family'][%r{(?:,\s*)(([^,]*))}, 1]
    return family_type
  end
  'default'
end

.load_yaml(full_path, replace_double_escapes = true) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 9

def self.load_yaml(full_path, replace_double_escapes = true)
  raise "File #{full_path} doesn't exist." unless File.exist?(full_path)
  yaml_file = File.read(full_path)
  data_hash = YAML.safe_load(yaml_file, [Symbol])
  data_hash = replace_double_escapes(data_hash) if replace_double_escapes
  data_hash
end

.os_family_match_ok(exclusion_hash) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 28

def self.os_family_match_ok(exclusion_hash)
  exclusion_hash.each do |exclusion|
    next if exclusion['os_family'].nil?
    next unless PuppetX::CiscoIOS::Utility.ios_os_family_type =~ %r{#{exclusion['os_family']}}
    return false
  end
  true
end

.parse_attribute(output, commands_hash, attribute) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 169

def self.parse_attribute(output, commands_hash, attribute)
  return unless attribute_safe_to_run(commands_hash, attribute)
  default_value = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'default', true)
  optional_match = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'optional_match', true)
  multiline = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'multiline', true)
  regex = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'get_value')
  returned_value = if regex.nil?
                     []
                   elsif multiline
                     output.scan(%r{#{regex}}m)
                   else
                     output.scan(%r{#{regex}})
                   end
  if returned_value.empty?
    # there is no match
    if optional_match
      # it is ok for this attribute to return nil
      returny = nil
    elsif default_value
      # use the default value
      returny = default_value
    else
      Puppet.debug "Regex for attribute '#{attribute}' failed"
    end
  elsif returned_value.size == 1
    # there is a single match
    returny = returned_value.flatten.first
  else
    # we have an array of matches.
    returny = returned_value.flatten
  end
  returny
end

.parse_multiples(output, commands_hash, attribute, convert_position_to_integer = nil) ⇒ Object

Parses tuple values convert_position_to_integer converts any value at array position ‘x’ to an integer eg. [‘5’, ‘test_value’] with convert_position_to_integer 0 will return [5, ‘test_value’]



395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 395

def self.parse_multiples(output, commands_hash, attribute, convert_position_to_integer = nil)
  return_value = output.scan(%r{#{PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, attribute, 'get_value')}})
  unless convert_position_to_integer.nil?
    return_value.each do |value|
      value[convert_position_to_integer] = value[convert_position_to_integer].to_i
    end
  end
  if return_value.empty?
    return nil
  end
  return_value
end

.parse_resource(output, command_hash) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 141

def self.parse_resource(output, command_hash)
  attributes_hash = {}
  attributes = command_hash.dig('attributes')
  raise "This key 'command_hash => attributes' is not in the #{command_hash}" if attributes.nil?
  attributes.each do |attribute|
    value = parse_attribute(output, command_hash, attribute.first)
    attributes_hash[attribute.first.to_sym] = value
  end
  attributes_hash
end

.read_table(table, min_field_size = 7) ⇒ Object



483
484
485
486
487
488
489
490
491
492
493
494
495
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 483

def self.read_table(table, min_field_size = 7)
  lines = table.lstrip.split "\n"
  return nil unless lines.size >= 5
  fields = lines[2].split %r{\s+}
  title_widths = lines[2].scan(%r{(\S+\s*)}).flatten.map(&:length)

  lines[3..-2].map do |line|
    next if line.empty?
    offsets = convert_to_offsets(title_widths, line.length, min_field_size)
    values = extract_values line, offsets
    Hash[*fields.zip(values).flatten]
  end
end

.replace_double_escapes(data_hash) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 17

def self.replace_double_escapes(data_hash)
  data_hash.each_pair do |key, value|
    if value.is_a?(Hash)
      replace_double_escapes(value)
    elsif value.is_a?(String)
      data_hash[key] = value.gsub(%r{\\\\}, '\\')
    end
  end
  data_hash
end

.resource_safe_for_device(commands_hash) ⇒ Object

Raise an error is a resource wide exclusion, otherwise return true



95
96
97
98
99
100
101
102
103
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 95

def self.resource_safe_for_device(commands_hash)
  if commands_hash['exclusions'].is_a? Array
    device_type = PuppetX::CiscoIOS::Utility.ios_device_type
    commands_hash['exclusions'].each do |exclusion|
      raise "This device #{PuppetX::CiscoIOS::Utility.ios_device_type} is not capable of running this resource and associated commands" if exclusion['device'] == device_type
    end
  end
  true
end

.safe_to_run(exclusion_hash) ⇒ Object



51
52
53
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 51

def self.safe_to_run(exclusion_hash)
  version_match_ok && device_match_ok(exclusion_hash) && os_family_match_ok(exclusion_hash)
end

.set_tuple_values(instance, commands_hash, name, first_value, second_value) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 408

def self.set_tuple_values(instance, commands_hash, name, first_value, second_value)
  commands = []
  unless instance[name.to_sym].nil?
    if PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, name) && PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, name)
      instance[name.to_sym].each do |a, b|
        command = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, name, 'set_value')
        command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(command, first_value, a, false)
        command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(command, second_value, b, false)
        commands.push(command)
      end
    end
    # remove from instance, so we dont add twice
    instance.delete(name.to_sym)
  end
  commands
end

.set_values(instance, commands_hash) ⇒ Object

build a single command_line from attributes



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 204

def self.set_values(instance, commands_hash)
  PuppetX::CiscoIOS::Utility.resource_safe_for_device(commands_hash)
  command_line = if !commands_hash['delete_values'].nil? && instance[:ensure] == 'absent'
                   PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'delete_values')
                 else
                   PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'set_values')
                 end
  # Set the state, of the commandline eg 'no ntp server
  if !commands_hash['ensure_is_state'].nil? && !PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'ensure_is_state').nil?
    command_line = if instance[:ensure] == 'present'
                     command_line.to_s.gsub(%r{<state>}, '')
                   else
                     command_line.to_s.gsub(%r{<state>}, 'no')
                   end
  end
  instance.each do |key, value|
    # if print_key exists then print the key, otherwise dont
    print_key = if key == :ensure
                  false
                else
                  # if print_key exists then print the key, otherwise dont
                  !PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, key.to_s, 'print_key', true).nil?
                end
    command_line = insert_attribute_into_command_line(command_line, key, value, print_key) if key == :ensure || PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, key.to_s)
  end
  command_line = command_line.to_s.gsub(%r{<\S*>}, '')
  command_line = command_line.strip
  command_line
end

.shorthand_to_full(name) ⇒ Object



550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 550

def self.shorthand_to_full(name)
  shorthand = name[%r{(^[a-zA-Z]{2})}, 1]
  port = name[%r{^[a-zA-Z]{2}(.*$)}, 1]
  full = case shorthand
         when 'Gi'
           'GigabitEthernet'
         when 'Te'
           'TenGigabitEthernet'
         when 'Fa'
           'FastEthernet'
         when 'Po'
           'Port-channel'
         end
  full + port
end

.value_foraged_from_command_hash(command_hash, top_level_key) ⇒ Object

for a command_hash entry try to retrive advice specific value first then the default value


delete_values:

'4948': 'no snmp-server host <name> <username> <port> <vrf> <type> <version> <security>'
default: 'no snmp-server host <name> <port> <vrf> <type> <version> <security> <username>'


80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 80

def self.value_foraged_from_command_hash(command_hash, top_level_key)
  device_type = PuppetX::CiscoIOS::Utility.ios_device_type
  # see if there is a device specific entry
  unless device_type.nil?
    return_val = command_hash.dig(top_level_key, device_type)
  end
  # if not try default
  if return_val.nil?
    return_val = command_hash.dig(top_level_key, 'default')
  end
  raise "This key 'command_hash => #{top_level_key} => #{device_type}/default' is not in the #{command_hash}" if return_val.nil?
  return_val
end

.version_match_okObject



37
38
39
# File 'lib/puppet_x/puppetlabs/cisco_ios/utility.rb', line 37

def self.version_match_ok
  true
end