Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Changelog
* RADIUS
* radius_global (@jonnytpuppet)
* radius_server (@jonnytpuppet)
* SNMP
* snmp_notification_receiver (@jonnytpuppet)
* SYSLOG
* syslog_server (@jonnytpuppet)
* syslog_setting (@jonnytpuppet)
Expand Down
8 changes: 4 additions & 4 deletions lib/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ inherit_from: ../.rubocop.yml
# Baseline code complexity metrics for the lib/ subdirectory:

Metrics/AbcSize:
Max: 40
Max: 45

Metrics/CyclomaticComplexity:
Max: 17
Max: 23

Metrics/MethodLength:
Max: 45
Max: 48

Metrics/ParameterLists:
Max: 9

Metrics/PerceivedComplexity:
Max: 19
Max: 24
50 changes: 50 additions & 0 deletions lib/cisco_node_utils/cmd_ref/snmp_notification_receiver.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# snmp_notification_receiver
---
port:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.*version.* udp-port (\d+).*$/'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future work it would be preferable to use named keys in the config_get_token as well ('/^snmp-server host <ip>.*version.* udp-port (\d+).*$/') but this isn't critical at this time.

default_value: null

receivers:
config_get: "show running snmp all"
config_get_token: '/^snmp-server host (\S+) (traps|informs)/'
config_set: "<state> snmp-server host <ip> <type> version <version> <security> <username> <udp_port>"
multiple:

security:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.* version 3 (auth|noauth|priv).*$/'
default_value: null

source_interface:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.* source-interface (\S+).*$/'
config_set: "snmp-server host <ip> source-interface <source_interface> <port>"
default_value: null

type:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s (traps|informs).*$/'
default_value: null

username:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.*version.* (\S+)$/'
default_value: null

username_with_port:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.*version.* (\S+) udp-port/'
default_value: null

version:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.* version (\S+).*$/'
default_value: null

vrf:
config_get: "show running-config snmp all"
config_get_token: '/^snmp-server host %s.* use-vrf (\S+).*$/'
config_set: "snmp-server host <ip> use-vrf <vrf> <port>"
default_value: null

158 changes: 158 additions & 0 deletions lib/cisco_node_utils/snmp_notification_receiver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# November 2015, Jonathan Tripathy
#
# Copyright (c) 2014-2015 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative 'node_util'

module Cisco
# SnmpNotificationReceiver - node utility class for SNMP server management
class SnmpNotificationReceiver < NodeUtil
attr_reader :name

def initialize(name,
instantiate: true,
type: '',
version: '',
security: '',
username: '',
port: '',
vrf: '',
source_interface: '')

fail TypeError unless name.is_a?(String)
@name = name

fail TypeError unless type.is_a?(String)

fail TypeError unless version.is_a?(String) || version.is_a?(Integer)

fail TypeError unless security.is_a?(String)

fail TypeError unless username.is_a?(String)

fail TypeError unless port.is_a?(String) || port.is_a?(Integer)

fail TypeError unless vrf.is_a?(String)

fail TypeError unless source_interface.is_a?(String)

return unless instantiate

# Mandatory Properties
fail TypeError unless name.length > 0
fail TypeError unless type.length > 0

if version.is_a?(Integer)
fail TypeError if version <= 0
else
fail TypeError if version.length <= 0
end

fail TypeError unless username.length > 0

config_set('snmp_notification_receiver',
'receivers',
state: '',
ip: name,
type: type,
version: version,
security: security,
username: username,
udp_port: port.empty? ? '' : "udp-port #{port}")

unless source_interface.empty?
config_set('snmp_notification_receiver',
'source_interface',
ip: name,
source_interface: source_interface,
port: port.empty? ? '' : "udp-port #{port}")
end

return if vrf.empty?
config_set('snmp_notification_receiver',
'vrf',
ip: name,
vrf: vrf,
port: port.empty? ? '' : "udp-port #{port}")
end

def self.receivers
hash = {}

receivers_list = config_get('snmp_notification_receiver', 'receivers')
return hash if receivers_list.nil?

receivers_list.each do |arr|
next if !arr.is_a?(Array) || arr.empty?
id = arr[0]
hash[id] = SnmpNotificationReceiver.new(id, instantiate: false)
end

hash
end

def ==(other)
name == other.name
end

def destroy
config_set('snmp_notification_receiver',
'receivers',
state: 'no',
ip: name,
type: type,
version: version,
security: security.nil? ? '' : "#{security}",
username: username,
udp_port: port.nil? ? '' : "udp-port #{port}")
end

def port
config_get('snmp_notification_receiver', 'port', @name)
end

def username
if !port.nil?
endpoint = 'username_with_port'
else
endpoint = 'username'
end

config_get('snmp_notification_receiver', endpoint, @name)
end

def version
config_get('snmp_notification_receiver', 'version', @name)
end

def type
config_get('snmp_notification_receiver', 'type', @name)
end

def security
config_get('snmp_notification_receiver', 'security', @name)
end

def vrf
config_get('snmp_notification_receiver', 'vrf', @name)
end

def source_interface
val = config_get('snmp_notification_receiver', 'source_interface', @name)
val = val.downcase unless val.nil?
val
end
end
end
137 changes: 137 additions & 0 deletions tests/test_snmp_notification_receiver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#
# Minitest for SnmpNotificationReceiver class
#
# Copyright (c) 2014-2015 Cisco and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require_relative 'ciscotest'
require_relative '../lib/cisco_node_utils/snmp_notification_receiver'

# TestSnmpNotificationReceiver - Minitest for SnmpNotificationReceiver
# node utility.
class TestSnmpNotificationReceiver < CiscoTestCase
def setup
# setup runs at the beginning of each test
super
no_snmpnotificationreceiver
config('vrf context red')
end

def teardown
# teardown runs at the end of each test
no_snmpnotificationreceiver
config('no vrf context red')
super
end

def no_snmpnotificationreceiver
# Turn the feature off for a clean test.
config('no snmp-server host 4.5.6.7 informs version 3 priv ab udp-port 45',
'no snmp-server host 8.9.10.11 traps version 3 auth cd udp-port 46')
end

# TESTS

def test_create_destroy_single
id = '4.5.6.7'
refute_includes(Cisco::SnmpNotificationReceiver.receivers, id)

receiver = \
Cisco::SnmpNotificationReceiver.new(id,
instantiate: true,
type: 'informs',
version: '3',
security: 'priv',
username: 'ab',
port: '45',
vrf: 'red',
source_interface: 'ethernet1/3')

assert_includes(Cisco::SnmpNotificationReceiver.receivers, id)
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id], receiver)

assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].source_interface,
'ethernet1/3')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].port, '45')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].type, 'informs')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].username, 'ab')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].version, '3')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].vrf, 'red')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].security, 'priv')

receiver.destroy
refute_includes(Cisco::SnmpNotificationReceiver.receivers, id)
end

def test_create_destroy_multiple
id = '4.5.6.7'
id2 = '8.9.10.11'

refute_includes(Cisco::SnmpNotificationReceiver.receivers, id)
refute_includes(Cisco::SnmpNotificationReceiver.receivers, id2)

receiver = \
Cisco::SnmpNotificationReceiver.new(id,
instantiate: true,
type: 'informs',
version: '3',
security: 'priv',
username: 'ab',
port: '45',
vrf: 'red',
source_interface: 'ethernet1/3')

receiver2 = \
Cisco::SnmpNotificationReceiver.new(id2,
instantiate: true,
type: 'traps',
version: '3',
security: 'auth',
username: 'cd',
port: '46',
vrf: 'red',
source_interface: 'ethernet1/4')

assert_includes(Cisco::SnmpNotificationReceiver.receivers, id)
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id], receiver)

assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].source_interface,
'ethernet1/3')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].port, '45')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].type, 'informs')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].username, 'ab')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].version, '3')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].vrf, 'red')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id].security, 'priv')

assert_includes(Cisco::SnmpNotificationReceiver.receivers, id2)
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2], receiver2)

assert_equal(
Cisco::SnmpNotificationReceiver.receivers[id2].source_interface,
'ethernet1/4')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].port, '46')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].type, 'traps')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].username, 'cd')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].version, '3')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].vrf, 'red')
assert_equal(Cisco::SnmpNotificationReceiver.receivers[id2].security,
'auth')

receiver.destroy
receiver2.destroy
refute_includes(Cisco::SnmpNotificationReceiver.receivers, id)
refute_includes(Cisco::SnmpNotificationReceiver.receivers, id2)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there no way to change any of the properties without doing a destroy and recreate? If I want to change the username for a receiver from 'ab' to 'cd', I have to call instance.destroy() and instance.new() rather than something like instance.username = 'cd'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a limitation in the Cisco CLI that does not allow you to change properties independently of each other. This was discussed in the an email thread with the Cisco SNMP guys for the snmp_user provider.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough - snmpuser does the same approach but I was mis-remembering that it had setters for individual properties too. This is fine.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mikewiebe - Is there anything you're needing to merge this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ajjohnsonpdx @glennmatthews provided quite a few review comments which I believe are not yet resolved?

end