Skip to content

Commit 906f991

Browse files
committed
(BOLT-994) Task helper for remote resources
1 parent 51005f9 commit 906f991

File tree

7 files changed

+20
-32
lines changed

7 files changed

+20
-32
lines changed

tasks/panos_task.rb renamed to lib/puppet/util/task_helper.rb

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
require 'puppet'
22
require 'json'
33

4-
class PanosTask
5-
def initialize
4+
class Puppet::Util::TaskHelper
5+
def initialize(transport_name)
6+
@transport_name = transport_name
7+
68
# work around the fact that bolt (for now, see BOLT-132) is not able to transport additional code from the module
79
# this requires that the panos module is pluginsynced to the node executing the task
810
Puppet.settings.initialize_app_defaults(
@@ -13,18 +15,21 @@ def initialize
1315
$LOAD_PATH.unshift(Puppet[:plugindest])
1416

1517
unless target
16-
puts "Panos task must be run on a proxy"
18+
puts "Task must be run on a proxy"
1719
exit 1
1820
end
1921

22+
Puppet::Util::Log.newdestination(:console)
23+
Puppet[:log_level] = 'debug'
24+
2025
add_plugin_paths(params['_installdir'])
2126
end
2227

2328
def transport
2429
require 'puppet/resource_api/transport'
25-
require 'puppet/transport/panos'
30+
require "puppet/transport/#{@transport_name}"
2631

27-
Puppet::ResourceApi::Transport.connect('panos', credentials)
32+
Puppet::ResourceApi::Transport.connect(@transport_name, credentials)
2833
end
2934

3035
def params
@@ -36,24 +41,7 @@ def target
3641
end
3742

3843
def credentials
39-
@credentials ||= if target.key? 'apikey'
40-
{
41-
host: target['host'],
42-
apikey: target['apikey']
43-
}
44-
else
45-
{
46-
host: target['host'],
47-
user: target['user'],
48-
password: target['password']
49-
}
50-
end
51-
52-
if target.key? 'port'
53-
@credentials[:port] = target['port']
54-
end
55-
56-
@credentials
44+
@credentials ||= target.each_with_object({}){|(k,v), h| h[k.to_sym] = v}
5745
end
5846

5947
private

tasks/apikey.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"parameters": {
77
},
88
"files": [
9-
"panos/tasks/panos_task.rb",
9+
"panos/lib/puppet/util/task_helper.rb",
1010
"panos/lib/puppet/transport/panos.rb",
1111
"panos/lib/puppet/transport/schema/panos.rb"
1212
]

tasks/apikey.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/opt/puppetlabs/puppet/bin/ruby
22

3-
require_relative 'panos_task'
4-
task = PanosTask.new
3+
require_relative '../lib/puppet/util/task_helper'
4+
task = Puppet::Util::TaskHelper.new('panos')
55

66
puts JSON.generate(apikey: task.transport.apikey)

tasks/commit.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"parameters": {
77
},
88
"files": [
9-
"panos/tasks/panos_task.rb",
9+
"panos/lib/puppet/util/task_helper.rb",
1010
"panos/lib/puppet/transport/panos.rb",
1111
"panos/lib/puppet/transport/schema/panos.rb"
1212
]

tasks/commit.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/opt/puppetlabs/puppet/bin/ruby
22

3-
require_relative 'panos_task'
4-
task = PanosTask.new
3+
require_relative '../lib/puppet/util/task_helper'
4+
task = Puppet::Util::TaskHelper.new('panos')
55

66
if task.transport.outstanding_changes?
77
task.transport.commit

tasks/set_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
},
1616
"files": [
17-
"panos/tasks/panos_task.rb",
17+
"panos/lib/puppet/util/task_helper.rb",
1818
"panos/lib/puppet/transport/panos.rb",
1919
"panos/lib/puppet/transport/schema/panos.rb"
2020
]

tasks/set_config.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/opt/puppetlabs/puppet/bin/ruby
22

3-
require_relative 'panos_task'
4-
task = PanosTask.new
3+
require_relative '../lib/puppet/util/task_helper'
4+
task = Puppet::Util::TaskHelper.new('panos')
55

66
file = task.params['config_file']
77
transport.import(file, 'configuration')

0 commit comments

Comments
 (0)