Class: Tablomat::IPSet::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/tablomat/ipset/set.rb

Overview

Interface to manage ipsets

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#activeObject (readonly)

Returns the value of attribute active.



9
10
11
# File 'lib/tablomat/ipset/set.rb', line 9

def active
  @active
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/tablomat/ipset/set.rb', line 9

def name
  @name
end

#systemObject (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(add_options, 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: '')
  create_options = "range #{rangefrom}-#{rangeto} #{create_options}" if type.include?('bitmap')
  command = "#{@system.ipset_bin} create #{@name} #{type} #{create_options}"
  @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

#destroyObject



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

Returns:

  • (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.message.include?('The set with the given name does not exist')

  false
end

#typeObject



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