Class: Puppet::Provider::IosInterface::CiscoIos

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/provider/ios_interface/cisco_ios.rb

Overview

IOS Interface Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clean_logging_event(instance) ⇒ Object

Clears ‘link-status’ from ‘logging-event’. This is done as it’s default nature is

different than the remaining logging events.


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 81

def self.clean_logging_event(instance)
  if instance[:logging_event]
    if instance[:logging_event].class == Array
      instance[:logging_event] -= ['link-status']
    elsif instance[:logging_event].class == String
      instance.delete(:logging_event) if instance[:logging_event] == 'link-status'
    end
  end
  if instance[:logging_event] && instance[:logging_event].class == String
    instance[:logging_event] = [instance[:logging_event]]
  elsif instance[:logging_event].nil?
    instance[:logging_event] = 'unset'
  end
  instance
end

.commands_from_instance(property_hash, current) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 35

def self.commands_from_instance(property_hash, current)
  commands = []
  if property_hash[:vrf] == 'unset' && current[:vrf] && current[:vrf] != 'unset'
    commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ vrf: current[:vrf] }, commands_hash)[0]}"
    property_hash.delete(:vrf)
  elsif property_hash[:vrf] == 'unset' && !current[:vrf]
    property_hash.delete(:vrf)
  end
  # If unset is supplied in logging_event then if a value is currently set is not in the
  #   applied manifest, it needs to be removed.
  if property_hash[:logging_event] == 'unset' && current[:logging_event] && current[:logging_event] != 'unset'
    current[:logging_event].each do |remove|
      commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ logging_event: remove }, commands_hash)[0]}"
    end
  elsif property_hash[:logging_event] && current[:logging_event] && current[:logging_event] != 'unset'
    to_remove = []
    to_remove += current[:logging_event] - property_hash[:logging_event]
    to_remove.each do |remove|
      commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ logging_event: remove }, commands_hash)[0]}"
    end
  end

  # logging_event requires multiple commands to be built in most cases, as such it is handled
  #   seperately from the remaining attributes
  if property_hash[:logging_event] && property_hash[:logging_event].class == Array
    property_hash[:logging_event].each do |event|
      commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ logging_event: event }, commands_hash)
    end
  end
  property_hash.delete(:logging_event)

  property_hash.each do |key, value|
    property_hash[key] = Puppet::Provider::IosInterface::CiscoIos.false_to_unset(value)
  end
  commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(property_hash, commands_hash)
  commands
end

.commands_hashObject



6
7
8
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 6

def self.commands_hash
  @commands_hash ||= PuppetX::CiscoIOS::Utility.load_yaml(File.expand_path(__dir__) + '/command.yaml')
end

.false_to_unset(false_value) ⇒ Object

Returns ‘unset’ if the given calue is false



74
75
76
77
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 74

def self.false_to_unset(false_value)
  return 'unset' if false_value == false
  false_value
end

.instances_from_cli(output) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 10

def self.instances_from_cli(output)
  new_instance_fields = []
  output.scan(%r{#{PuppetX::CiscoIOS::Utility.get_instances(commands_hash)}}).each do |raw_instance_fields|
    new_instance = PuppetX::CiscoIOS::Utility.parse_resource(raw_instance_fields, commands_hash)
    new_instance_fields << PuppetX::CiscoIOS::Utility.device_safe_instance(new_instance, commands_hash)
  end
  cleaned_value = []
  new_instance_fields.each do |instance|
    instance[:mac_notification_added] = instance[:mac_notification_added].nil? ? false : true
    instance[:mac_notification_removed] = instance[:mac_notification_removed].nil? ? false : true
    instance[:link_status_duplicates] = instance[:link_status_duplicates].nil? ? false : true
    instance[:ip_dhcp_snooping_trust] = instance[:ip_dhcp_snooping_trust].nil? ? false : true
    instance[:flowcontrol_receive] = 'off' unless instance[:flowcontrol_receive]
    instance[:ip_dhcp_snooping_limit] = false unless instance[:ip_dhcp_snooping_limit]
    instance[:vrf] = 'unset' unless instance[:vrf]
    instance[:route_cache_cef] = (instance[:route_cache_cef]) ? false : true
    instance = Puppet::Provider::IosInterface::CiscoIos.clean_logging_event(instance)
    # Converts 'logging_event_link_status' to a boolean value. The value only appears when
    #   it is unset as it's set by default, so if it is found it should be set to false.
    instance[:logging_event_link_status] = (instance[:logging_event_link_status]) ? false : true
    cleaned_value << instance
  end
  cleaned_value
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



126
127
128
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 126

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



97
98
99
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 97

def commands_hash
  Puppet::Provider::IosInterface::CiscoIos.commands_hash
end

#get(context, _names = nil) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 101

def get(context, _names = nil)
  output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
  return [] if output.nil?
  return_value = Puppet::Provider::IosInterface::CiscoIos.instances_from_cli(output)
  instances = PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_value)
  instances
end

#set(context, changes) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 109

def set(context, changes)
  changes.each do |name, change|
    should = change[:should]
    is = change[:is]
    context.updating(name) do
      update(context, name, should, is)
    end
  end
end

#update(context, name, should, is) ⇒ Object



119
120
121
122
123
124
# File 'lib/puppet/provider/ios_interface/cisco_ios.rb', line 119

def update(context, name, should, is)
  array_of_commands_to_run = Puppet::Provider::IosInterface::CiscoIos.commands_from_instance(should, is)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_interface_mode(name, command)
  end
end