Class: Puppet::Provider::IosAaaAccounting::CiscoIos

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

Overview

Configure AAA Accounting on the device

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(instance) ⇒ Object



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
# File 'lib/puppet/provider/ios_aaa_accounting/cisco_ios.rb', line 41

def self.commands_from_instance(instance)
  # if service is commands exists but enable level is not set, we need to fail
  if instance[:accounting_service].to_s == 'commands' && instance[:commands_enable_level].nil?
    raise "ios_aaa_accounting requires commands_enable_level to be set if accounting_service is 'commands'"
  end

  if instance[:accounting_service].to_s == 'commands' && instance[:commands_enable_level]
    instance[:accounting_service] = "#{instance[:accounting_service]} #{instance[:commands_enable_level]}"
  end
  commands = []
  if instance[:accounting_service].casecmp('update').zero?
    instance.delete(:accounting_list)
  end
  if instance[:update_newinfo]
    instance[:update_newinfo] = 'newinfo'
  end
  if instance[:update_periodic]
    instance[:update_periodic] = "periodic #{instance[:update_periodic]}"
  end
  if instance[:update_newinfo_periodic]
    instance[:update_newinfo_periodic] = "newinfo periodic #{instance[:update_newinfo_periodic]}"
  end
  instance[:server_groups] = PuppetX::CiscoIOS::Utility.generate_server_groups_command_string(instance)
  command = PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash)
  if instance[:ensure].to_s == 'absent'
    command = 'no ' + command
  end
  commands << command
  commands
end

.commands_hashObject



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

def self.commands_hash
  @commands_hash ||= PuppetX::CiscoIOS::Utility.load_yaml(File.expand_path(__dir__) + '/command.yaml')
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
34
35
36
37
38
39
# File 'lib/puppet/provider/ios_aaa_accounting/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[:name] = (new_instance[:accounting_service]).to_s
    if new_instance[:accounting_service] == 'commands' && new_instance[:commands_enable_level]
      new_instance[:name] = new_instance[:name] + " #{new_instance[:commands_enable_level]}"
    end
    if new_instance[:update_newinfo]
      new_instance[:update_newinfo] = true
    end
    if new_instance[:accounting_service] == 'update'
      if new_instance[:update_newinfo]
        new_instance[:name] = new_instance[:name] + ' newinfo'
      elsif new_instance[:update_periodic]
        new_instance[:name] = new_instance[:name] + ' periodic'
      elsif new_instance[:update_newinfo_periodic]
        new_instance[:name] = new_instance[:name] + ' newinfo periodic'
      end
    else
      new_instance[:name] = new_instance[:name] + " #{new_instance[:accounting_list]}"
    end
    # Convert any single items to expected array
    new_instance[:server_groups] = [new_instance[:server_groups]].flatten(1) unless new_instance[:server_groups].nil?
    new_instance[:ensure] = 'present'
    new_instance.delete_if { |_k, v| v.nil? }
    new_instance_fields << new_instance
  end
  new_instance_fields
end

Instance Method Details

#commands_hashObject



72
73
74
# File 'lib/puppet/provider/ios_aaa_accounting/cisco_ios.rb', line 72

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

#delete(context, _name, is) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/puppet/provider/ios_aaa_accounting/cisco_ios.rb', line 106

def delete(context, _name, is)
  is[:ensure] = 'absent'
  array_of_commands_to_run = Puppet::Provider::IosAaaAccounting::CiscoIos.commands_from_instance(is)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end

#get(context) ⇒ Object



76
77
78
79
80
81
# File 'lib/puppet/provider/ios_aaa_accounting/cisco_ios.rb', line 76

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

#set(context, changes) ⇒ Object



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

def set(context, changes)
  changes.each do |name, change|
    is = change.key?(:is) ? change[:is] : (get(context) || []).find { |key| key[:name] == name }
    should = change[:should]
    if should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(context, name, is)
      end
    else
      context.updating(name) do
        update(context, name, should)
      end
    end
  end
end

#update(context, _name, should) ⇒ Object



99
100
101
102
103
104
# File 'lib/puppet/provider/ios_aaa_accounting/cisco_ios.rb', line 99

def update(context, _name, should)
  array_of_commands_to_run = Puppet::Provider::IosAaaAccounting::CiscoIos.commands_from_instance(should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end