Class: Y2Firewall::Firewalld::Service
- Inherits:
-
Object
- Object
- Y2Firewall::Firewalld::Service
- Extended by:
- Relations, Yast::I18n
- Includes:
- Yast::I18n
- Defined in:
- library/network/src/lib/y2firewall/firewalld/service.rb
Overview
Class to work with Firewalld services
@example
ha = firewalld.find_service("high-availability") ha.ports # => ["2224/tcp", "3121/tcp", "5403/tcp", "5404/udp", "5405/udp", "21064/tcp"]
ha.tcp_ports #=> ["2224", "3121", "5403", "21064"] ha.udp_ports #=> ["5404", "5405"]
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Service name.
Class Method Summary collapse
-
.modify_ports(name:, tcp_ports: [], udp_ports: []) ⇒ Boolean
Convenience method for setting the tcp and udp ports of a given service.
Instance Method Summary collapse
-
#apply_changes! ⇒ Boolean
Apply the changes done since read in firewalld.
-
#create! ⇒ Object
Create the service in firewalld.
-
#initialize(name:) ⇒ Service
constructor
Constructor.
-
#read ⇒ Boolean
Read the firewalld configuration initializing the object accordingly.
-
#supported? ⇒ Boolean
Return whether the service is available in firewalld or not.
-
#tcp_ports ⇒ Array<String>
Convenience method to select only the service tcp ports.
-
#udp_ports ⇒ Array<String>
Convenience method to select only the service udp ports.
Methods included from Relations
enable_modifications_cache, has_attributes, has_many
Constructor Details
#initialize(name:) ⇒ Service
Constructor
82 83 84 85 86 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 82 def initialize(name:) @name = name @ports = [] @protocols = [] end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns service name.
51 52 53 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 51 def name @name end |
Class Method Details
.modify_ports(name:, tcp_ports: [], udp_ports: []) ⇒ Boolean
Convenience method for setting the tcp and udp ports of a given service. If the service is found, it modify the ports according to the given parameters applying the changes at the end.
71 72 73 74 75 76 77 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 71 def self.modify_ports(name:, tcp_ports: [], udp_ports: []) return false unless Firewalld.instance.installed? service = Firewalld.instance.find_service(name) service.ports = tcp_ports.map { |p| "#{p}/tcp" } + udp_ports.map { |p| "#{p}/udp" } service.apply_changes! end |
Instance Method Details
#apply_changes! ⇒ Boolean
Apply the changes done since read in firewalld
115 116 117 118 119 120 121 122 123 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 115 def apply_changes! return true if !modified? return false if !supported? apply_attributes_changes! apply_relations_changes! untouched! true end |
#create! ⇒ Object
Create the service in firewalld
89 90 91 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 89 def create! api.create_service(name) end |
#read ⇒ Boolean
Read the firewalld configuration initializing the object accordingly
103 104 105 106 107 108 109 110 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 103 def read return false unless supported? read_attributes read_relations untouched! true end |
#supported? ⇒ Boolean
Return whether the service is available in firewalld or not
96 97 98 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 96 def supported? api.service_supported?(name) end |
#tcp_ports ⇒ Array<String>
Convenience method to select only the service tcp ports
128 129 130 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 128 def tcp_ports ports.select { |p| p.include?("tcp") }.map { |p| p.sub("/tcp", "") } end |
#udp_ports ⇒ Array<String>
Convenience method to select only the service udp ports
135 136 137 |
# File 'library/network/src/lib/y2firewall/firewalld/service.rb', line 135 def udp_ports ports.select { |p| p.include?("udp") }.map { |p| p.sub("/udp", "") } end |