Class: Puppet::Provider::OpnsenseProvider
- Inherits:
-
ResourceApi::SimpleProvider
- Object
- ResourceApi::SimpleProvider
- Puppet::Provider::OpnsenseProvider
show all
- Defined in:
- lib/puppet/provider/opnsense_provider.rb
Overview
A base provider for all opnsense providers
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#_edit_single_object(device_name, should) ⇒ Puppet::Util::Execution::ProcessOutput
-
#_fetch_resource_list(device_names) ⇒ void
-
#_find_uuid_by_namevars(namevars, by_column) ⇒ String
-
#_get_resource_from_devices(devices) ⇒ Hash<Symbol>
-
#_get_resources_from_devices(devices) ⇒ Array<Hash<Symbol>>
-
#array_from_value(value) ⇒ Array<String>
-
#bool_from_value(value) ⇒ FalseClass, TrueClass
-
#create(_context, name, should) ⇒ Puppet::Util::Execution::ProcessOutput
-
#delete(_context, name) ⇒ Puppet::Util::Execution::ProcessOutput
-
#get(_context, filter) ⇒ Array<Hash<Symbol>>
-
#get_config_path(device_name) ⇒ String
-
#get_configured_devices ⇒ Array
-
#get_device_names_by_filter(filter) ⇒ Array
-
#get_opn_cli_json_list(device_name, group, command) ⇒ Object
-
#get_opn_cli_json_show(device_name, group, command) ⇒ Object
-
#initialize ⇒ void
constructor
-
#opn_cli_base_cmd(device_name, *args) ⇒ Puppet::Util::Execution::ProcessOutput
-
#update(_context, name, should) ⇒ Puppet::Util::Execution::ProcessOutput
Constructor Details
#initialize ⇒ void
12
13
14
15
16
17
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 12
def initialize
super
@config_basedir = '~/.puppet-opnsense'
@config_suffix = '-config.yaml'
@resource_list = []
end
|
Instance Attribute Details
#resource_list=(value) ⇒ Object
8
9
10
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 8
def resource_list=(value)
@resource_list = value
end
|
#resource_type=(value) ⇒ Object
Sets the attribute resource_type
9
10
11
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 9
def resource_type=(value)
@resource_type = value
end
|
Instance Method Details
#_edit_single_object(device_name, should) ⇒ Puppet::Util::Execution::ProcessOutput
154
155
156
157
158
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 154
def _edit_single_object(device_name, should)
args = _translate_puppet_resource_to_command_args('edit', should)
device_name = should[:device].to_s
opn_cli_base_cmd(device_name, args)
end
|
#_fetch_resource_list(device_names) ⇒ void
This method returns an undefined value.
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 70
def _fetch_resource_list(device_names)
case @resource_type
when 'single'
@resource_list = _get_resource_from_devices(device_names)
when 'list'
@resource_list = _get_resources_from_devices(device_names)
else
raise Puppet::ResourceError, "Unknown resource type '#{@resource_type}'"
end
@resource_list
end
|
#_find_uuid_by_namevars(namevars, by_column) ⇒ String
163
164
165
166
167
168
169
170
171
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 163
def _find_uuid_by_namevars(namevars, by_column)
resource_found = @resource_list.find do |resource|
resource[:device] == namevars[:device] && resource[by_column] == namevars[by_column]
end
unless resource_found
raise Puppet::ResourceError, "Could not find uuid for #{namevars}"
end
resource_found[:uuid]
end
|
#_get_resource_from_devices(devices) ⇒ Hash<Symbol>
85
86
87
88
89
90
91
92
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 85
def _get_resource_from_devices(devices)
result = []
devices.each do |device|
json_object = get_opn_cli_json_show(device, @group, @command)
result.push(_translate_json_object_to_puppet_resource(device, json_object))
end
result
end
|
#_get_resources_from_devices(devices) ⇒ Array<Hash<Symbol>>
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 96
def _get_resources_from_devices(devices)
result = []
devices.each do |device|
json_list = get_opn_cli_json_list(device, @group, @command)
json_list.each do |json_list_item|
result.push(_translate_json_object_to_puppet_resource(device, json_list_item))
end
end
result
end
|
#array_from_value(value) ⇒ Array<String>
214
215
216
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 214
def array_from_value(value)
value == [] ? value : value.split(',')
end
|
#bool_from_value(value) ⇒ FalseClass, TrueClass
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 201
def bool_from_value(value)
case value
when true, 'true', 1, '1' then
true
when false, 'false', nil, '', 0, '0' then
false
else
raise ArgumentError, 'invalid value for Boolean()'
end
end
|
#create(_context, name, should) ⇒ Puppet::Util::Execution::ProcessOutput
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 111
def create(_context, name, should)
device_name = should[:device].to_s
if @resource_type == 'single'
return _edit_single_object(name, should)
end
args = _translate_puppet_resource_to_command_args('create', should[@create_key], should)
opn_cli_base_cmd(device_name, args, '-o', 'json')
end
|
#delete(_context, name) ⇒ Puppet::Util::Execution::ProcessOutput
142
143
144
145
146
147
148
149
150
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 142
def delete(_context, name)
if @resource_type == 'single'
raise NotImplementedError, "Single resource type #{self.class} has not implemented `delete`"
end
uuid = _find_uuid_by_namevars(name, @find_uuid_by_column)
device_name = name.fetch(:device).to_s
opn_cli_base_cmd(device_name, [@group, @command, 'delete', uuid, '-o', 'json'])
end
|
#get(_context, filter) ⇒ Array<Hash<Symbol>>
22
23
24
25
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 22
def get(_context, filter)
device_names = get_device_names_by_filter(filter)
_fetch_resource_list(device_names)
end
|
#get_config_path(device_name) ⇒ String
195
196
197
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 195
def get_config_path(device_name)
File.join(get_config_basedir.to_s, "#{File.basename(device_name)}#{_get_suffix}")
end
|
44
45
46
47
48
49
50
51
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 44
def get_configured_devices
devices = []
Dir.glob(_get_config_glob_pattern).each do |path|
device_name = File.basename(path).gsub(%r{#{_get_suffix}$}, '')
devices.push(device_name)
end
devices
end
|
#get_device_names_by_filter(filter) ⇒ Array
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 29
def get_device_names_by_filter(filter)
if filter.is_a?(Array)
device_names = filter.map { |item|
item[:device] if item.is_a?(Hash) && item.include?(:device)
}.compact.uniq
end
if device_names.empty?
device_names = get_configured_devices
end
device_names
end
|
#get_opn_cli_json_list(device_name, group, command) ⇒ Object
222
223
224
225
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 222
def get_opn_cli_json_list(device_name, group, command)
json_output = opn_cli_base_cmd(device_name, [group, command, 'list', '-o', 'json'])
JSON.parse(json_output)
end
|
#get_opn_cli_json_show(device_name, group, command) ⇒ Object
231
232
233
234
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 231
def get_opn_cli_json_show(device_name, group, command)
json_output = opn_cli_base_cmd(device_name, [group, command, 'show', '-o', 'json'])
JSON.parse(json_output)
end
|
#opn_cli_base_cmd(device_name, *args) ⇒ Puppet::Util::Execution::ProcessOutput
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 176
def opn_cli_base_cmd(device_name, *args)
config_path = get_config_path(device_name)
args.unshift(
'opn-cli',
'-c',
config_path,
)
begin
output = Puppet::Util::Execution.execute(
args, failonfail: true, combine: true, custom_environment: { 'LC_ALL' => 'en_US.utf8' }
)
output
rescue Puppet::ExecutionFailure => e
raise Puppet::Error, e.message.to_s
end
end
|
#update(_context, name, should) ⇒ Puppet::Util::Execution::ProcessOutput
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/puppet/provider/opnsense_provider.rb', line 126
def update(_context, name, should)
device_name = should[:device].to_s
if @resource_type == 'single'
return _edit_single_object(device_name, should)
end
uuid = _find_uuid_by_namevars(name, @find_uuid_by_column)
args = _translate_puppet_resource_to_command_args('update', uuid, should)
device_name = should[:device].to_s
opn_cli_base_cmd(device_name, args)
end
|