Class: Puppet::Provider::SnmpUser::CiscoIos

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

Overview

SNMP user Puppet Provider for Cisco IOS devices

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.command_from_instance(property_hash) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 58

def self.command_from_instance(property_hash)
  property_hash[:name] = property_hash[:name].split.first
  property_hash[:privacy] = 'priv des' if property_hash[:privacy] == 'des'
  property_hash[:privacy] = 'priv aes 128' if property_hash[:privacy] == 'aes128'
  property_hash[:privacy] = 'priv aes 192' if property_hash[:privacy] == 'aes192'
  property_hash[:privacy] = 'priv aes 256' if property_hash[:privacy] == 'aes256'
  property_hash[:enforce_privacy] = 'encrypted' if property_hash[:enforce_privacy] == true
  property_hash[:roles] = property_hash[:roles].first if property_hash.key? :roles
  command = PuppetX::CiscoIOS::Utility.set_values(property_hash, commands_hash)
  command
end

.commands_hashObject



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

def self.instances_from_cli(output)
  new_instance_fields = []
  return new_instance_fields if output.nil? || output.empty?
  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'
    # making a composite key
    name_field = ''
    name_field += new_instance[:name] + ' '
    name_field += new_instance[:version]
    name_field.strip!
    new_instance[:name] = name_field
    new_instance[:roles] = [].push(new_instance[:roles].strip) if new_instance[:roles].is_a?(String)
    new_instance.delete_if { |_k, v| v.nil? }

    new_instance_fields << new_instance
  end
  new_instance_fields
end

.instances_from_cli_v3(output) ⇒ 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
56
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 32

def self.instances_from_cli_v3(output)
  new_instance_fields = []
  return new_instance_fields if output.nil? || output.empty?
  output.split("\n\n").each do |raw_instance_fields|
    new_instance = PuppetX::CiscoIOS::Utility.parse_resource(raw_instance_fields, commands_hash)
    new_instance[:ensure] = 'present'
    new_instance[:version] = 'v3'

    next if new_instance[:v3_user].nil?
    new_instance[:name] = new_instance[:v3_user] + ' v3'
    new_instance[:roles] = new_instance[:v3_roles] unless new_instance[:v3_roles].nil?
    new_instance[:roles] = [].push(new_instance[:roles]) if new_instance[:roles].is_a?(String)
    new_instance[:auth] = new_instance[:v3_auth].downcase unless new_instance[:v3_auth].nil?
    unless new_instance[:v3_privacy].nil? || new_instance[:v3_privacy].casecmp('none').zero?
      new_instance[:privacy] = new_instance[:v3_privacy]
      new_instance[:privacy] = new_instance[:privacy].downcase
    end
    new_instance[:engine_id] = new_instance[:v3_engine_id] unless new_instance[:v3_engine_id].nil?
    # remove the v3_ keys
    new_instance.delete_if { |k, _v| k.to_s =~ %r{^v3_} }
    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



120
121
122
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 120

def canonicalize(_context, resources)
  resources
end

#commands_hashObject



70
71
72
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 70

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

#create(context, _name, should) ⇒ Object



105
106
107
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 105

def create(context, _name, should)
  context.transport.run_command_conf_t_mode(Puppet::Provider::SnmpUser::CiscoIos.command_from_instance(should))
end

#delete(context, _name, should) ⇒ Object



116
117
118
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 116

def delete(context, _name, should)
  context.transport.run_command_conf_t_mode(Puppet::Provider::SnmpUser::CiscoIos.command_from_instance(should))
end

#get(context, _names = nil) ⇒ Object



74
75
76
77
78
79
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 74

def get(context, _names = nil)
  output = context.transport.run_command_enable_mode(PuppetX::CiscoIOS::Utility.get_values(commands_hash))
  output_v3 = context.transport.run_command_enable_mode(commands_hash['get_v3_values']['default'])
  PuppetX::CiscoIOS::Utility.enforce_simple_types(context, (Puppet::Provider::SnmpUser::CiscoIos.instances_from_cli(output) <<
      Puppet::Provider::SnmpUser::CiscoIos.instances_from_cli_v3(output_v3)).flatten!)
end

#set(context, changes) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 81

def set(context, changes)
  changes.each do |name, change|
    is = change.key?(:is) ? change[:is] : (get(context) || []).find { |key| key[:name] == name }
    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, should)
      end
    end
  end
end

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



109
110
111
112
113
114
# File 'lib/puppet/provider/snmp_user/cisco_ios.rb', line 109

def update(context, _name, is, should)
  # perform a delete on current, then add
  is[:ensure] = 'absent'
  context.transport.run_command_conf_t_mode(Puppet::Provider::SnmpUser::CiscoIos.command_from_instance(is))
  context.transport.run_command_conf_t_mode(Puppet::Provider::SnmpUser::CiscoIos.command_from_instance(should))
end