Class: Puppet::Provider::SnmpNotificationReceiver::CiscoNexus

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

Overview

Implementation for the snmp_notification_receiver type using the Resource API.

Instance Method Summary collapse

Instance Method Details

#canonicalize(_context, resources) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 18

def canonicalize(_context, resources)
  resources.each do |resource|
    resource[:port] = 'unset' if resource[:port].nil? || resource[:port] == (nil || -1)
    resource[:source_interface] = 'unset' if resource[:source_interface].nil?
    resource[:vrf] = 'unset' if resource[:vrf].nil?
  end
  resources
end

#delete(context, name) ⇒ Object



100
101
102
103
104
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 100

def delete(context, name)
  context.notice("Destroying '#{name}'")
  @snmp_notification_receivers = Cisco::SnmpNotificationReceiver.receivers
  @snmp_notification_receivers[name].destroy if @snmp_notification_receivers[name]
end

#get(_context, receivers = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 27

def get(_context, receivers=nil)
  require 'cisco_node_utils'
  current_states = []
  @snmp_notification_receivers = Cisco::SnmpNotificationReceiver.receivers
  if receivers.nil? || receivers.empty?
    @snmp_notification_receivers.each do |receiver, instance|
      current_states << get_current_state(receiver, instance)
    end
  else
    receivers.each do |receiver|
      individual_receiver = @snmp_notification_receivers[receiver]
      next if individual_receiver.nil?
      current_states << get_current_state(receiver, individual_receiver)
    end
  end
  current_states
end

#get_current_state(receiver, instance) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 45

def get_current_state(receiver, instance)
  {
    name:             receiver,
    ensure:           'present',
    port:             instance.port ? instance.port.to_i : 'unset',
    username:         instance.username,
    version:          instance.version.prepend('v').delete('c'),
    type:             instance.type,
    security:         instance.security,
    vrf:              instance.vrf ? instance.vrf : 'unset',
    source_interface: instance.source_interface ? instance.source_interface : 'unset',
  }
end

#munge(val) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 78

def munge(val)
  if val.is_a?(String) && val.eql?('unset')
    nil
  elsif val.is_a?(Integer) && val.eql?(-1)
    nil
  elsif val.is_a?(Integer)
    val.to_s
  else
    val
  end
end

#update(context, name, should) ⇒ Object Also known as: create



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 59

def update(context, name, should)
  validate_should(should)
  # existing receiver needs to be deleted before updating
  @snmp_notification_receivers = Cisco::SnmpNotificationReceiver.receivers
  @snmp_notification_receivers[name].destroy if @snmp_notification_receivers[name]
  context.notice("Setting '#{name}' with #{should.inspect}")
  Cisco::SnmpNotificationReceiver.new(name,
                                      instantiate:      true,
                                      type:             munge(should[:type]) || '',
                                      security:         should[:version].eql?('v3') ? munge(should[:security]) || '' : '',
                                      version:          should[:version].eql?('v2') ? should[:version].delete('v') << 'c' : should[:version].delete('v'),
                                      username:         munge(should[:username]) || '',
                                      port:             munge(should[:port]) || '',
                                      vrf:              munge(should[:vrf]) || '',
                                      source_interface: munge(should[:source_interface]) || '')
end

#validate_should(should) ⇒ Object

Raises:

  • (Puppet::ResourceError)


90
91
92
93
94
95
96
97
98
# File 'lib/puppet/provider/snmp_notification_receiver/cisco_nexus.rb', line 90

def validate_should(should)
  required = []
  [:type, :version, :username].each do |property|
    required << property unless should[property]
  end
  raise Puppet::ResourceError, "You must specify the following properties: #{required}" unless required.empty?
  raise Puppet::ResourceError, "The 'type' property only supports a setting of 'traps' when 'version' is set to 'v1'" if !should[:type].eql?('traps') && should[:version].eql?('v1')
  raise Puppet::ResourceError, "The 'security' property is only supported when 'version' is set to 'v3'" if should[:security] && !should[:version].eql?('v3')
end