Class: Puppet::Provider::TacacsGlobal::CiscoIos

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

Overview

Configure the domain name of the device

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(instance) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/provider/tacacs_global/cisco_ios.rb', line 32

def self.commands_from_instance(instance)
  commands = []
  # raise an exception if a vrf is given
  raise 'cisco_ios does not support the use of the vrf attribute with tacacs_global' if instance[:vrf]
  # if key exists but not key_format, we need to fail
  raise 'tacacs_global requires key_format to be set if setting key' if !instance[:key].nil? && instance[:key_format].nil?
  unless instance[:key].nil?
    # build a single command for key_format + key
    if PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, 'key') && PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, 'key_format')
      command = PuppetX::CiscoIOS::Utility.attribute_value_foraged_from_command_hash(commands_hash, 'key', 'set_value')
      command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(command, 'key_format', instance[:key_format], false)
      command = PuppetX::CiscoIOS::Utility.insert_attribute_into_command_line(command, 'key', instance[:key], false)
      commands.push(command)
    end
    # remove key and key_format, so we dont add twice
    instance.delete(:key_format)
    instance.delete(:key)
  end
  raise 'tacacs_global only accepts a single source_interface' if !instance[:source_interface].nil? && instance[:source_interface].size != 1
  instance[:directed_request] = 'unset' if instance[:directed_request] == false
  instance[:source_interface] = instance[:source_interface].first unless instance[:source_interface].nil?
  commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(instance, commands_hash)
  commands
end

.commands_hashObject



8
9
10
# File 'lib/puppet/provider/tacacs_global/cisco_ios.rb', line 8

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

.instances_from_cli(output) ⇒ Object



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

def self.instances_from_cli(output)
  new_instance_fields = []
  new_instance = PuppetX::CiscoIOS::Utility.parse_resource(output, commands_hash)
  new_instance[:name] = 'default'
  # On certain OS `no tacacs-server directed-request` appears in the config rather than simply
  #   being left out.
  if new_instance[:directed_request].nil?
    new_instance[:directed_request] = false
  elsif new_instance[:directed_request] == 'no tacacs-server directed-request'
    new_instance[:directed_request] = false
  elsif new_instance[:directed_request] == 'tacacs-server directed-request'
    new_instance[:directed_request] = true
  end

  new_instance[:source_interface] = [].push(new_instance[:source_interface]) if new_instance[:source_interface].is_a?(String)
  new_instance.delete_if { |_k, v| v.nil? }
  new_instance_fields << new_instance
  new_instance_fields
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



84
85
86
87
88
89
# File 'lib/puppet/provider/tacacs_global/cisco_ios.rb', line 84

def canonicalize(_context, resources)
  resources.each do |resource|
    resource[:source_interface] = resource[:source_interface].sort if resource[:source_interface]
  end
  resources
end

#commands_hashObject



57
58
59
# File 'lib/puppet/provider/tacacs_global/cisco_ios.rb', line 57

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

#get(context, _names = nil) ⇒ Object



61
62
63
64
65
66
# File 'lib/puppet/provider/tacacs_global/cisco_ios.rb', line 61

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::TacacsGlobal::CiscoIos.instances_from_cli(output)
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_value)
end

#set(context, changes) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/puppet/provider/tacacs_global/cisco_ios.rb', line 68

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

#update(context, _name, should) ⇒ Object



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

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