Class: Puppet::Provider::RadiusServerGroup::CiscoIos

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

Overview

Configure rad of the device

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(property_hash) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/puppet/provider/radius_server_group/cisco_ios.rb', line 24

def self.commands_from_instance(property_hash)
  commands_array = []
  # servers are special
  property_hash[:servers] = nil unless property_hash[:servers].nil?
  command = PuppetX::CiscoIOS::Utility.set_values(property_hash, commands_hash)
  commands_array.push(command)
  commands_array
end

.commands_from_is_should(is, should) ⇒ Object



33
34
35
36
37
# File 'lib/puppet/provider/radius_server_group/cisco_ios.rb', line 33

def self.commands_from_is_should(is, should)
  array_of_commands = []
  array_of_commands += PuppetX::CiscoIOS::Utility.commands_from_diff_of_two_arrays(commands_hash, is[:servers], should[:servers], 'servers')
  array_of_commands
end

.commands_hashObject



8
9
10
# File 'lib/puppet/provider/radius_server_group/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
# File 'lib/puppet/provider/radius_server_group/cisco_ios.rb', line 12

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[:servers] = [].push(new_instance[:servers]) if new_instance[:servers].is_a?(String)
    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



101
102
103
# File 'lib/puppet/provider/radius_server_group/cisco_ios.rb', line 101

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



39
40
41
# File 'lib/puppet/provider/radius_server_group/cisco_ios.rb', line 39

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

#create(context, name, should) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/puppet/provider/radius_server_group/cisco_ios.rb', line 92

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

#delete(context, name) ⇒ Object



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

def delete(context, name)
  clear_hash = { name: name, ensure: 'absent' }
  array_of_commands_to_run = Puppet::Provider::RadiusServerGroup::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



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

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

#set(context, changes) ⇒ Object



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

def set(context, changes)
  changes.each do |name, change|
    is = if context.type.feature?('simple_get_filter')
           change.key?(:is) ? change[:is] : (get(context, [name]) || []).find { |r| r[:name] == name }
         else
           change.key?(:is) ? change[:is] : (get(context) || []).find { |r| r[:name] == name }
         end
    should = change[:should]
    is = { name: name, ensure: 'absent' } if is.nil?
    should = { name: name, ensure: 'absent' } if should.nil?
    if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
      context.creating(name) do
        create(context, name, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
      context.updating(name) do
        update(context, name, is, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(context, name)
      end
    end
  end
end

#update(context, name, is, should) ⇒ Object



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

def update(context, name, is, should)
  # only called if adding/removing servers
  array_of_commands_to_run = Puppet::Provider::RadiusServerGroup::CiscoIos.commands_from_is_should(is, should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_radius_mode(name, command)
  end
end