-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpuppetdb_command.rb
More file actions
97 lines (88 loc) · 3.26 KB
/
puppetdb_command.rb
File metadata and controls
97 lines (88 loc) · 3.26 KB
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
# frozen_string_literal: true
require 'bolt/error'
# Send a command with a payload to PuppetDB.
#
# The `pdb_command` function only supports version 5 of the `replace_facts`
# command. Other commands might also work, but are not tested or supported
# by Bolt.
#
# See the [commands endpoint](https://puppet.com/docs/puppetdb/latest/api/command/v1/commands.html)
# documentation for more information about available commands and payload
# format.
#
# _This function is experimental and subject to change._
#
# > **Note:** Not available in apply block
#
Puppet::Functions.create_function(:puppetdb_command) do
# Send a command with a payload to PuppetDB.
#
# @param command The command to invoke.
# @param version The version of the command to invoke.
# @param payload The payload to the command.
# @return The UUID identifying the response sent by PuppetDB.
# @example Replace facts for a target
# $payload = {
# 'certname' => 'localhost',
# 'environment' => 'dev',
# 'producer' => 'bolt',
# 'producer_timestamp' => '1970-01-01',
# 'values' => { 'orchestrator' => 'bolt' }
# }
#
# puppetdb_command('replace_facts', 5, $payload)
dispatch :puppetdb_command do
param 'String[1]', :command
param 'Integer', :version
param 'Hash[Data, Data]', :payload
return_type 'String'
end
# Send a command with a payload to a named PuppetDB instance.
#
# @param command The command to invoke.
# @param version The version of the command to invoke.
# @param payload The payload to the command.
# @param instance The PuppetDB instance to send the command to.
# @return The UUID identifying the response sent by PuppetDB.
# @example Replace facts for a target using a named PuppetDB instance
# $payload = {
# 'certname' => 'localhost',
# 'environment' => 'dev',
# 'producer' => 'bolt',
# 'producer_timestamp' => '1970-01-01',
# 'values' => { 'orchestrator' => 'bolt' }
# }
#
# puppetdb_command('replace_facts', 5, $payload, 'instance-1')
dispatch :puppetdb_command_with_instance do
param 'String[1]', :command
param 'Integer', :version
param 'Hash[Data, Data]', :payload
param 'String', :instance
return_type 'String'
end
def puppetdb_command(command, version, payload)
puppetdb_command_with_instance(command, version, payload, nil)
end
def puppetdb_command_with_instance(command, version, payload, instance)
# Disallow in apply blocks.
unless Puppet[:tasks]
raise Puppet::ParseErrorWithIssue.from_issue_and_stack(
Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING,
action: 'puppetdb_command'
)
end
# Send analytics report.
Puppet.lookup(:bolt_executor).report_function_call(self.class.name)
puppetdb_client = Puppet.lookup(:bolt_pdb_client)
# Error if the PDB client does not implement :send_command
unless puppetdb_client.respond_to?(:send_command)
raise Bolt::Error.new(
"PuppetDB client #{puppetdb_client.class} does not implement :send_command, " \
"unable to invoke command.",
'bolt/pdb-command'
)
end
puppetdb_client.send_command(command, version, payload, instance)
end
end