Class: Puppet::Provider::RadiusServer::CiscoIos

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

Overview

Configure a radius_server on the device

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(instance) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 24

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 'radius_server requires key_format to be set if setting key' if !instance[:key].nil? && instance[:key_format].nil?
    raise 'radius_server requires hostname to be set if setting auth_port and/or acct_port' if (!instance[:auth_port].nil? || !instance[:acct_port].nil?) && instance[:hostname].nil?
    instance[:timeout] = 'unset' if instance[:timeout].to_s == '0'
    instance[:retransmit_count] = 'unset' if instance[:retransmit_count].to_s == '0'
    unless instance[:hostname].nil?
      # address command is special we need to craft it ( hostname, auth_port and acct_port )
      address_ports_string = ''
      address_ports_string += " auth-port #{instance[:auth_port]}" if instance[:auth_port] && PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, 'auth_port')
      address_ports_string += " acct-port #{instance[:acct_port]}" if instance[:acct_port] && PuppetX::CiscoIOS::Utility.attribute_safe_to_run(commands_hash, 'acct_port')
      instance[:hostname] = "#{PuppetX::CiscoIOS::Utility.detect_ipv4_or_ipv6(instance[:hostname])}#{address_ports_string}"
    end
    unless instance[:key].to_s == 'unset'
      # key is made up of key and key_format
      instance[:key] = "#{instance[:key_format]} #{instance[:key]}" unless instance[:key_format].nil?
    end
    array_of_commands = PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values(instance, commands_hash)
  end
  array_of_commands
end

.commands_hashObject



9
10
11
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 9

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

.instances_from_cli(output) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 13

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[:ensure] = 'present'
    new_instance.delete_if { |_k, v| v.nil? }
    new_instance_fields << new_instance
  end
  new_instance_fields
end

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



89
90
91
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 89

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



54
55
56
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 54

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

#create(context, name, should) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 80

def create(context, name, should)
  create_hash = { name: name, ensure: 'create' }
  array_of_commands_to_run = Puppet::Provider::RadiusServer::CiscoIos.commands_from_instance(create_hash)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
  update(context, name, should)
end

#delete(context, name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 72

def delete(context, name)
  clear_hash = { name: name, ensure: 'absent' }
  array_of_commands_to_run = Puppet::Provider::RadiusServer::CiscoIos.commands_from_instance(clear_hash)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
end

#get(context, _names = nil) ⇒ Object



58
59
60
61
62
63
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 58

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

#update(context, _name, should) ⇒ Object



65
66
67
68
69
70
# File 'lib/puppet/provider/radius_server/cisco_ios.rb', line 65

def update(context, _name, should)
  array_of_commands_to_run = Puppet::Provider::RadiusServer::CiscoIos.commands_from_instance(should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_radius_server_mode(should[:name], command)
  end
end