Class: Tablomat::IPSet::Set
- Inherits:
-
Object
- Object
- Tablomat::IPSet::Set
- Defined in:
- lib/tablomat/ipset/set.rb
Overview
Interface to manage ipsets
Instance Attribute Summary collapse
-
#active ⇒ Object
readonly
Returns the value of attribute active.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#system ⇒ Object
readonly
Returns the value of attribute system.
Instance Method Summary collapse
- #add(entry_data:, add_options: '', exist: false) ⇒ Object
- #create(type:, create_options: '', rangefrom: '', rangeto: '') ⇒ Object
- #del(entry_data:, exist: false) ⇒ Object
- #destroy ⇒ Object
- #entry(data, &block) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(system, name) ⇒ Set
constructor
A new instance of Set.
- #type ⇒ Object
Constructor Details
#initialize(system, name) ⇒ Set
Returns a new instance of Set.
11 12 13 14 15 16 |
# File 'lib/tablomat/ipset/set.rb', line 11 def initialize(system, name) @system = system @name = name @entrys = {} @active = false end |
Instance Attribute Details
#active ⇒ Object (readonly)
Returns the value of attribute active.
9 10 11 |
# File 'lib/tablomat/ipset/set.rb', line 9 def active @active end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/tablomat/ipset/set.rb', line 9 def name @name end |
#system ⇒ Object (readonly)
Returns the value of attribute system.
9 10 11 |
# File 'lib/tablomat/ipset/set.rb', line 9 def system @system end |
Instance Method Details
#add(entry_data:, add_options: '', exist: false) ⇒ Object
49 50 51 |
# File 'lib/tablomat/ipset/set.rb', line 49 def add(entry_data:, add_options: '', exist: false) entry(entry_data).add(, exist) end |
#create(type:, create_options: '', rangefrom: '', rangeto: '') ⇒ Object
57 58 59 60 61 62 |
# File 'lib/tablomat/ipset/set.rb', line 57 def create(type:, create_options: '', rangefrom: '', rangeto: '') = "range #{rangefrom}-#{rangeto} #{}" if type.include?('bitmap') command = "#{@system.ipset_bin} create #{@name} #{type} #{}" @system.exec command @active = true end |
#del(entry_data:, exist: false) ⇒ Object
53 54 55 |
# File 'lib/tablomat/ipset/set.rb', line 53 def del(entry_data:, exist: false) entry(entry_data).del(exist) end |
#destroy ⇒ Object
64 65 66 67 68 |
# File 'lib/tablomat/ipset/set.rb', line 64 def destroy command = "#{@system.ipset_bin} destroy #{@name}" @system.exec command @active = false end |
#entry(data, &block) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/tablomat/ipset/set.rb', line 41 def entry(data, &block) data = data.to_s.downcase (@entrys[data] || Entry.new(self, data)).tap do |entry| @entrys[data] = entry block&.call(entry) end end |
#exists? ⇒ Boolean
18 19 20 21 22 23 24 25 26 |
# File 'lib/tablomat/ipset/set.rb', line 18 def exists? command = "#{@system.ipset_bin} list #{@name}" @system.exec command true rescue IPSetError => e raise unless e..include?('The set with the given name does not exist') false end |
#type ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/tablomat/ipset/set.rb', line 28 def type command = "#{system.ipset_bin} list #{@name}" stdout = `#{command} 2>&1`.strip << "\n" if $CHILD_STATUS != 0 # throw error puts "Invalid return value when calling #{command}" end stdout = stdout.split("\n").select { |s| s.include?('Type:') }[0] stdout.split(/Type: /)[1] rescue StandardError => e puts "[error] #{e}" end |