Class: Puppet::Provider::TacacsServerGroup::CiscoIos

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

Overview

Tacacs Server Group Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.commands_from_instance(instance) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/puppet/provider/tacacs_server_group/cisco_ios.rb', line 26

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

.commands_from_is_should(is, should) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/provider/tacacs_server_group/cisco_ios.rb', line 35

def self.commands_from_is_should(is, should)
  array_of_commands = []

  if should[:source_interface] == 'unset' && is[:source_interface] && is[:source_interface] != 'unset'
    array_of_commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ source_interface: is[:source_interface] }, commands_hash)[0]}"
  elsif should[:source_interface] && should[:source_interface] != 'unset' && should[:source_interface] != is[:source_interface]
    array_of_commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ source_interface: should[:source_interface] }, commands_hash)
  end

  if should[:vrf] == 'unset' && is[:vrf] && is[:vrf] != 'unset'
    array_of_commands << "no #{PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ vrf: is[:vrf] }, commands_hash)[0]}"
  elsif should[:vrf] && should[:vrf] != 'unset' && should[:vrf] != is[:vrf]
    array_of_commands += PuppetX::CiscoIOS::Utility.build_commmands_from_attribute_set_values({ vrf: should[:vrf] }, commands_hash)
  end

  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/tacacs_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
23
24
# File 'lib/puppet/provider/tacacs_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[:servers] = [].push(new_instance[:servers]) if new_instance[:servers].is_a?(String)
    new_instance[:ensure] = 'present'
    new_instance[:source_interface] = 'unset' unless new_instance[:source_interface]
    new_instance[:vrf] = 'unset' unless new_instance[:vrf]
    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



117
118
119
120
121
122
# File 'lib/puppet/provider/tacacs_server_group/cisco_ios.rb', line 117

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

#commands_hashObject



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

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

#create(context, name, should) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/puppet/provider/tacacs_server_group/cisco_ios.rb', line 106

def create(context, name, should)
  # make a clone of should, because commands_from_instance modifys the hash
  instance = should.clone
  array_of_commands_to_run = Puppet::Provider::TacacsServerGroup::CiscoIos.commands_from_instance(instance)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_conf_t_mode(command)
  end
  is = { name: name, ensure: 'present', vrf: 'unset', source_interface: 'unset' }
  update(context, name, is, should)
end

#delete(context, name) ⇒ Object



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

def delete(context, name)
  delete_hash = { name: name, ensure: 'absent' }
  array_of_commands_to_run = Puppet::Provider::TacacsServerGroup::CiscoIos.commands_from_instance(delete_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/tacacs_server_group/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::TacacsServerGroup::CiscoIos.instances_from_cli(output)
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, return_value)
end

#set(context, changes) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/puppet/provider/tacacs_server_group/cisco_ios.rb', line 65

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



91
92
93
94
95
96
# File 'lib/puppet/provider/tacacs_server_group/cisco_ios.rb', line 91

def update(context, _name, is, should)
  array_of_commands_to_run = Puppet::Provider::TacacsServerGroup::CiscoIos.commands_from_is_should(is, should)
  array_of_commands_to_run.each do |command|
    context.transport.run_command_tacacs_server_group_mode(should[:name], command)
  end
end