Class: Puppet::Provider::TacacsServer::CiscoIos

Inherits:
ResourceApi::SimpleProvider
  • Object
show all
Defined in:
lib/puppet/provider/tacacs_server/cisco_ios.rb

Overview

Tacacs Server Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(instance) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 52

def self.commands_from_instance(instance)
  array_of_commands = []
  # if we create / delete only send a single command
  if instance[:ensure] == 'absent'
    array_of_commands.push(PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash))
  elsif instance[:ensure] == 'create'
    instance[:ensure] = 'present'
    array_of_commands.push(PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash))
  else
    # if key exists but not key_format, we need to fail
    raise 'tacacs_server requires key_format to be set if setting key' if !instance[:key].nil? && instance[:key_format].nil?
    # key and keyformat go in the same command
    unless instance[:key].nil?
      instance[:key] = instance[:key_format].to_s + " #{instance[:key]}" unless instance[:key] == 'unset'
      instance.delete(:key_format)
    end
    # single_connection
    instance[:single_connection] = 'unset' if !instance[:single_connection].nil? && instance[:single_connection] == false
    # the address type needs to be inserted
    instance[:hostname] = PuppetX::CiscoIOS::Utility.detect_ipv4_or_ipv6(instance[:hostname]) unless instance[:hostname].nil?
    # timeout 0 = unset = no timeout
    instance[:timeout] = 'unset' if !instance[:timeout].nil? && instance[:timeout].to_i.zero?
    array_of_commands = PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(instance, commands_hash)
  end
  array_of_commands
end

.commands_hashObject



10
11
12
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 10

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

.instances_from_cli(output) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 42

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 = tidy_up_instance_hash(new_instance)
    new_instance_fields << new_instance
  end
  new_instance_fields
end

.instances_from_old_cli(output) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 29

def self.instances_from_old_cli(output)
  new_instance_fields = []
  output.scan(%r{#{PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'get_instances_old_cli')}}).each do |raw_instance_fields|
    new_instance = raw_instance_fields.first.match(PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'get_value_old_cli'))
    next if new_instance.nil?
    new_instance_hash = Hash[new_instance.names.map(&:to_sym).zip(new_instance.captures)]
    new_instance_hash[:hostname] = new_instance_hash[:name]
    new_instance_hash = tidy_up_instance_hash(new_instance_hash)
    new_instance_fields << new_instance_hash
  end
  new_instance_fields
end

.old_cli_commands_from_instance(instance) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 79

def self.old_cli_commands_from_instance(instance)
  raise 'tacacs_server requires key to be set if setting key_format' if instance[:key].nil? && !instance[:key_format].nil?
  if instance[:hostname].nil?
    instance[:hostname] = instance[:name]
  end
  instance[:single_connection] = if instance[:single_connection] == true && instance[:ensure].to_s != 'absent'
                                   ' single-connection'
                                 else
                                   ''
                                 end
  if instance[:port]
    instance[:port] = " port #{instance[:port]}"
  end
  if instance[:timeout]
    instance[:timeout] = " timeout #{instance[:timeout]}"
  end
  if instance[:key]
    instance[:key] = if instance[:key_format]
                       " key #{instance[:key_format]} #{instance[:key]}"
                     else
                       " key #{instance[:key]}"
                     end
  end
  instance[:ensure] = if instance[:ensure].to_s == 'absent'
                        'no '
                      else
                        ''
                      end
  command_line = PuppetX::CiscoIOS::Utility.set_values(instance, commands_hash.dig('set_values_old_cli'))
  command_line
end

.tidy_up_instance_hash(instance) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 14

def self.tidy_up_instance_hash(instance)
  instance[:single_connection] = !(instance[:single_connection].nil? || instance[:single_connection] == '')
  instance[:ensure] = 'present'
  instance[:hostname] = if instance[:hostname]
                          (instance[:hostname] =~ Resolv::IPv6::Regex) ? IPAddr.new(instance[:hostname]).to_s.upcase : instance[:hostname]
                        else
                          'unset'
                        end
  instance[:timeout] = 0 unless instance[:timeout]
  instance[:key] = 'unset' unless instance[:key]
  instance[:key_format] = 0 if instance[:key] && instance[:key_format].nil?
  instance.delete_if { |_k, v| (v.nil? || v == '') }
  instance
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 189

def canonicalize(_context, resources)
  resources.each do |resource|
    next if resource[:hostname].nil?
    if resource[:hostname] != 'unset'
      resource[:hostname] = IPAddr.new(resource[:hostname]).to_s.upcase if resource[:hostname] =~ Resolv::IPv6::Regex
    end
  end
  resources
end

#commands_hashObject



111
112
113
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 111

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

#create(context, name, should) ⇒ Object



178
179
180
181
182
183
184
185
186
187
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 178

def create(context, name, should)
  if test_for_new_cli(context)
    array_of_commands_to_run = Puppet::Provider::TacacsServer::CiscoIos.commands_from_instance(should)
    array_of_commands_to_run.each do |command|
      context.transport.run_command_tacacs_mode(name, command)
    end
  else
    context.transport.run_command_conf_t_mode(Puppet::Provider::TacacsServer::CiscoIos.old_cli_commands_from_instance(should))
  end
end

#delete(context, name) ⇒ Object



169
170
171
172
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 169

def delete(context, name)
  delete_hash = { name: name, ensure: 'absent' }
  run_update(context, name, delete_hash)
end

#get(context, _names = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 143

def get(context, _names = nil)
  return_values = []
  new_instances = test_and_get_new_instances(context)
  new_instances.each do |new_instance|
    PuppetX::CiscoIOS::Utility.enforce_simple_types(context, new_instance)
  end
  return_values << new_instances
  old_instances = test_and_get_old_instances(context)
  old_instances.each do |old_instance|
    PuppetX::CiscoIOS::Utility.enforce_simple_types(context, old_instance)
  end
  return_values << old_instances
  return_values.flatten
end

#run_update(context, name, should) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 158

def run_update(context, name, should)
  if PuppetX::CiscoIOS::Utility.instances_contains_name(test_and_get_new_instances(context).flatten, name)
    array_of_commands_to_run = Puppet::Provider::TacacsServer::CiscoIos.commands_from_instance(should)
    array_of_commands_to_run.each do |command|
      context.transport.run_command_tacacs_mode(name, command)
    end
  end
  return unless PuppetX::CiscoIOS::Utility.instances_contains_name(test_and_get_old_instances(context).flatten, name)
  context.transport.run_command_conf_t_mode(Puppet::Provider::TacacsServer::CiscoIos.old_cli_commands_from_instance(should))
end

#test_and_get_new_instances(context) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 123

def test_and_get_new_instances(context)
  return_values = []
  if test_for_new_cli(context)
    output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
    unless output.nil?
      return_values << Puppet::Provider::TacacsServer::CiscoIos.instances_from_cli(output)
    end
  end
  return_values
end

#test_and_get_old_instances(context) ⇒ Object



134
135
136
137
138
139
140
141
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 134

def test_and_get_old_instances(context)
  return_values = []
  output_oldcli = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.value_foraged_from_command_hash(commands_hash, 'get_values_old_cli'))
  unless output_oldcli.nil?
    return_values << Puppet::Provider::TacacsServer::CiscoIos.instances_from_old_cli(output_oldcli)
  end
  return_values
end

#test_for_new_cli(context) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 115

def test_for_new_cli(context)
  test_for_new_cli_output = context.transport.run_command_conf_t_mode("tacacs ?\b\b\b\b\b\b\b\b")
  if test_for_new_cli_output =~ %r{(\n\s{2}server)}
    return true
  end
  false
end

#update(context, name, should) ⇒ Object



174
175
176
# File 'lib/puppet/provider/tacacs_server/cisco_ios.rb', line 174

def update(context, name, should)
  run_update(context, name, should)
end