Class: Cisco::ObjectGroupEntry

Inherits:
NodeUtil
  • Object
show all
Defined in:
lib/cisco_node_utils/object_group_entry.rb

Overview

node_utils class for object_group_entry

Instance Attribute Summary collapse

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(afi, type, name, seqno) ⇒ ObjectGroupEntry


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cisco_node_utils/object_group_entry.rb', line 27

def initialize(afi, type, name, seqno)
  fail TypeError unless name.is_a?(String)
  fail ArgumentError unless type[/address|port/]
  @afi = Acl.afi_cli(afi)
  @type = type
  @grp_name = name
  @seqno = seqno
  og = ObjectGroup.object_groups[afi.to_s][type.to_s][name.to_s]
  fail "ObjectGroup #{afi} #{type} #{name} does not exist" if
    og.nil?
  set_args_keys_default
end

Instance Attribute Details

#afiObject (readonly)

Returns the value of attribute afi


25
26
27
# File 'lib/cisco_node_utils/object_group_entry.rb', line 25

def afi
  @afi
end

#grp_nameObject (readonly)

Returns the value of attribute grp_name


25
26
27
# File 'lib/cisco_node_utils/object_group_entry.rb', line 25

def grp_name
  @grp_name
end

#typeObject (readonly)

Returns the value of attribute type


25
26
27
# File 'lib/cisco_node_utils/object_group_entry.rb', line 25

def type
  @type
end

Class Method Details

.object_group_entriesObject


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cisco_node_utils/object_group_entry.rb', line 56

def self.object_group_entries
  hash = {}
  grps = config_get('object_group', 'all_object_groups')
  return hash if grps.nil?
  grps.each do |afi, type, name|
    lafi = afi
    lafi = 'ipv4' if afi == 'ip'
    hash[lafi] ||= {}
    hash[lafi][type] ||= {}
    hash[lafi][type][name] ||= {}
    entries = config_get('object_group', 'all_entries',
                         afi:      Acl.afi_cli(lafi),
                         type:     type,
                         grp_name: name)
    next if entries.nil?
    entries.each do |seqno|
      hash[lafi][type][name][seqno] =
        ObjectGroupEntry.new(lafi, type, name, seqno)
    end
  end
  hash
end

Instance Method Details

#addressObject


119
120
121
122
123
124
125
126
127
# File 'lib/cisco_node_utils/object_group_entry.rb', line 119

def address
  match = entry_get
  return nil if match.nil?
  addr = match[:address]
  return nil if addr.nil?
  # Normalize addr. Some platforms zero_pad ipv6 addrs.
  addr.gsub!(/^0*/, '').gsub!(/:0*/, ':')
  addr
end

#address=(address) ⇒ Object


129
130
131
# File 'lib/cisco_node_utils/object_group_entry.rb', line 129

def address=(address)
  @set_args[:address] = address
end

#destroyObject


52
53
54
# File 'lib/cisco_node_utils/object_group_entry.rb', line 52

def destroy
  config_set('object_group', 'entry_destroy', @set_args)
end

#entry_getObject


79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/cisco_node_utils/object_group_entry.rb', line 79

def entry_get
  str = config_get('object_group', 'entry', @get_args)
  return nil if str.nil?
  str = str.strip

  # rubocop:disable Metrics/LineLength
  regexp = Regexp.new('(?<seqno>\d+)'\
                      ' *(?<address>host \S+|[:\.0-9a-fA-F]+ [:\.0-9a-fA-F]+|[:\.0-9a-fA-F]+\/\d+)?'\
                      ' *(?<port>range \S+ \S+|(lt|eq|gt|neq) \S+)?')
  # rubocop:enable Metrics/LineLength
  regexp.match(str)
end

#entry_set(attrs) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cisco_node_utils/object_group_entry.rb', line 92

def entry_set(attrs)
  if attrs.empty?
    attrs[:state] = 'no'
  else
    destroy if seqno
    attrs[:state] = ''
  end
  set_args_keys_default
  set_args_keys(attrs)
  [:address,
   :port,
  ].each do |p|
    attrs[p] = '' if attrs[p].nil?
    send(p.to_s + '=', attrs[p])
  end
  @get_args = @set_args
  config_set('object_group', 'entry', @set_args)
end

#portObject


133
134
135
136
137
# File 'lib/cisco_node_utils/object_group_entry.rb', line 133

def port
  match = entry_get
  return nil if match.nil?
  match.names.include?('port') ? match[:port] : nil
end

#port=(port) ⇒ Object


139
140
141
# File 'lib/cisco_node_utils/object_group_entry.rb', line 139

def port=(port)
  @set_args[:port] = port
end

#seqnoObject

PROPERTIES



113
114
115
116
117
# File 'lib/cisco_node_utils/object_group_entry.rb', line 113

def seqno
  match = entry_get
  return nil if match.nil?
  match.names.include?('seqno') ? match[:seqno] : nil
end

#set_args_keys(hash = {}) ⇒ Object

rubocop:disable Style/AccessorMethodName


47
48
49
50
# File 'lib/cisco_node_utils/object_group_entry.rb', line 47

def set_args_keys(hash={})
  set_args_keys_default
  @set_args = @get_args.merge!(hash) unless hash.empty?
end

#set_args_keys_defaultObject

Helper method to delete @set_args hash keys


41
42
43
44
# File 'lib/cisco_node_utils/object_group_entry.rb', line 41

def set_args_keys_default
  @set_args = { afi: @afi, type: @type, grp_name: @grp_name, seqno: @seqno }
  @get_args = @set_args
end