Class: Y2Firewall::Firewalld::ZoneParser

Inherits:
Object
  • Object
show all
Includes:
Yast::Logger
Defined in:
library/network/src/lib/y2firewall/firewalld/zone_parser.rb

Overview

Class to help parsing firewall-cmd –list_all_zones output

Constant Summary collapse

BOOLEAN_ATTRIBUTES =
["icmp-block-inversion", "masquerade"].freeze

Instance Method Summary collapse

Constructor Details

#initialize(zone_names, zones_definition) ⇒ ZoneParser

Constructor

existing zones.



38
39
40
41
# File 'library/network/src/lib/y2firewall/firewalld/zone_parser.rb', line 38

def initialize(zone_names, zones_definition)
  @zone_names = zone_names
  @zones_definition = zones_definition
end

Instance Method Details

#parseArray<Y2Firewall::Firewalld::Zone>

It parses the zone definition instantiating the defined zones and settings their attributes.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'library/network/src/lib/y2firewall/firewalld/zone_parser.rb', line 47

def parse
  return [] if !@zone_names || @zone_names.empty?
  zone = nil
  zones = []
  @zones_definition.reject(&:empty?).each do |line|
    attribute, _value = line.split("\s")
    next if !attribute

    if @zone_names.include?(attribute)
      zone = Zone.new(name: attribute)
      zones << zone
      next
    end

    next unless zone

    attribute, value = line.lstrip.split(":\s")

    next unless zone.respond_to?("#{attribute}=")
    if BOOLEAN_ATTRIBUTES.include?(attribute)
      zone.public_send("#{attribute}=", value == "yes" ? true : false)
    else
      zone.public_send("#{attribute}=", value.to_s.split)
    end
  end

  zones.map(&:untouched!)

  zones
end