Class: Cisco::SnmpCommunity

Inherits:
NodeUtil show all
Defined in:
lib/cisco_node_utils/snmpcommunity.rb

Overview

SnmpCommunity - node utility class for SNMP community config management

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NodeUtil

client, #client, config_get, #config_get, #config_get_default, config_get_default, config_set, #config_set, #get, #ios_xr?, #nexus?, #node, node, platform, #platform, supports?, #supports?

Constructor Details

#initialize(name, group, instantiate = true) ⇒ SnmpCommunity

Returns a new instance of SnmpCommunity.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 24

def initialize(name, group, instantiate=true)
  fail TypeError unless name.is_a?(String)
  fail TypeError unless group.is_a?(String)
  @name = name
  return unless instantiate
  if platform == :nexus
    config_set('snmp_community', 'community',
               state: '',
               name:  @name,
               group: group)
  else
    config_set('snmp_community', 'community',
               state: '',
               name:  @name)
    # create the mapping for group
    config_set('snmp_community', 'group_simple', state: '', group: group)
    config_set('snmp_community', 'group_community_mapping', name: @name, group: group) # rubocop:disable Metrics/LineLength
  end
end

Class Method Details

.communitiesObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 44

def self.communities
  @communities = {}
  comms = config_get('snmp_community', 'all_communities')
  unless comms.nil?
    comms.each do |comm|
      @communities[comm] = SnmpCommunity.new(comm, '', false)
    end
  end
  @communities
end

.default_aclObject



105
106
107
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 105

def self.default_acl
  config_get_default('snmp_community', 'acl')
end

.default_groupObject



87
88
89
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 87

def self.default_group
  config_get_default('snmp_community', 'group')
end

Instance Method Details

#aclObject



91
92
93
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 91

def acl
  config_get('snmp_community', 'acl', name: @name)
end

#acl=(acl) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 95

def acl=(acl)
  fail TypeError unless acl.is_a?(String)
  if acl.empty?
    acl = self.acl
    config_set('snmp_community', 'acl', state: 'no', name: @name, acl: acl) unless acl.empty? # rubocop:disable Metrics/LineLength
  else
    config_set('snmp_community', 'acl', state: '', name: @name, acl: acl)
  end
end

#destroyObject



55
56
57
58
59
60
61
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 55

def destroy
  # CLI requires specifying a group even for "no" commands
  config_set('snmp_community', 'community',
             state: 'no',
             name:  @name,
             group: 'null')
end

#groupObject

name is read only

def name
  @name
end


68
69
70
71
72
73
74
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 68

def group
  if platform == :nexus
    config_get('snmp_community', 'group', name: @name)
  else
    config_get('snmp_community', 'group_community_mapping', name: @name)
  end
end

#group=(group) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/cisco_node_utils/snmpcommunity.rb', line 76

def group=(group)
  fail TypeError unless group.is_a?(String)
  if platform == :nexus
    config_set('snmp_community', 'group', name: @name, group: group)
  else
    # create the mapping
    config_set('snmp_community', 'group_simple', group: group)
    config_set('snmp_community', 'group_community_mapping', name: @name, group: group) # rubocop:disable Metrics/LineLength
  end
end