-
Notifications
You must be signed in to change notification settings - Fork 8
/
modify_zone_interfaces.rb
121 lines (104 loc) · 3.22 KB
/
modify_zone_interfaces.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# Copyright (c) [2018] SUSE LLC
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact SUSE LLC.
#
# To contact SUSE LLC about this file by physical or electronic mail, you may
# find current contact information at www.suse.com.
require "cwm"
require "y2firewall/firewalld"
module Y2Firewall
module Widgets
# Combo box widget for selecting the zone which interfaces will be modified
class ZoneInterfacesSelector < ::CWM::ComboBox
extend Yast::I18n
# @!attribute [rw] interfaces_input
attr_accessor :interfaces_input
# Constructor
#
# @param interfaces_input [CWM::InputField] input field for modifying the
# selected zone interfaces
def initialize(interfaces_input)
super()
textdomain "firewall"
@interfaces_input = interfaces_input
end
# @macro seeAbstractWidget
def init
self.value = Y2Firewall::Firewalld.instance.default_zone
interfaces_input.value = selected_zone ? selected_zone.interfaces.join(" ") : ""
end
# @macro seeAbstractWidget
def opt
[:notify]
end
# @macro seeAbstractWidget
def label
_("Zone")
end
# @see CWM::ComboBox#items
def items
zones.map { |z| [z.name, z.name] }
end
# @macro seeAbstractWidget
def handle
interfaces_input.value = selected_zone ? selected_zone.interfaces.join(" ") : ""
nil
end
# @macro seeAbstractWidget
def store
return unless selected_zone
selected_zone.interfaces = interfaces_input.items_from_ui
end
private
# Returns the list of known zones
#
# @note Just a convenience method which value is 'memoized'.
#
# @return [Array<Y2Firewall::Firewalld::Zone>] List of zones.
def zones
@zones ||= Y2Firewall::Firewalld.instance.zones
end
# Returns the selected zone
#
# @return [Y2Firewall::Firewalld::Zone,nil] selected zone
def selected_zone
return nil if !value || value.empty?
Y2Firewall::Firewalld.instance.find_zone(value)
end
end
# An input field widget.
# The {#label} method is mandatory.
#
# @example input field widget child
class ZoneInterfaces < CWM::InputField
def initialize
super()
textdomain "firewall"
end
# @macro seeAbstractWidget
def label
_("Interfaces:")
end
# Split the current input field value by any combination of commas
# and/or spaces.
#
# @return [Array<String>] the current value splitted by any combination
# of commas and/or spaces
def items_from_ui
value.split(/ *[, ] */)
end
end
end
end