Class: Puppet::Provider::IosConfig::CiscoIos

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

Overview

Execute and arbitary command against the cicso_ios device with or without a check for idempotency

Instance Method Summary collapse

Instance Method Details

#canonicalize(context, resources) ⇒ Object

We use canonicalize here to check for idempotency as these are dynamic resources



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

def canonicalize(context, resources)
  resources.each do |resource|
    # We strip leading and trailing whitespace to help ensure valid commands are sent
    resource[:command].strip!
    next unless resource[:idempotent_regex]
    output = context.transport.run_command_enable_mode('show running-config')
    idempotent_regex = if resource[:idempotent_regex_options]
                         # Gather array of regex options, make sure they are unique, map them to Regexp constants and bitwise or them
                         Regexp.new(resource[:idempotent_regex], resource[:idempotent_regex_options].uniq.map { |x| Regexp.const_get(x.upcase) }.reduce(:|))
                       else
                         Regexp.new(resource[:idempotent_regex])
                       end
    match = if resource[:negate_idempotent_regex]
              output !~ idempotent_regex
            else
              output =~ idempotent_regex
            end
    # We have matched our idempotency criteria - do not update type
    # This is done by setting the type back to the default value so that Puppet knows it is in desired state
    resource.delete(:command) if match
  end
  resources
end

#get(_context) ⇒ Object



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

def get(_context)
  new_instance = [{ name: 'default' }]
  new_instance
end

#set(context, changes) ⇒ Object



36
37
38
39
40
41
# File 'lib/puppet/provider/ios_config/cisco_ios.rb', line 36

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

#update(context, name, should) ⇒ Object



43
44
45
46
47
48
# File 'lib/puppet/provider/ios_config/cisco_ios.rb', line 43

def update(context, name, should)
  # command mode is only conf_t for now.
  context.updating(name) do
    context.transport.run_command_conf_t_mode(should[:command])
  end
end