forked from puppetlabs/bolt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request puppetlabs#1374 from beechtom/puppetlabsGH-1365
(puppetlabsGH-1365) Add resolve_references plan function
- Loading branch information
Showing
7 changed files
with
227 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
bolt-modules/boltlib/lib/puppet/functions/resolve_references.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bolt/error' | ||
|
||
# Evaulates all _plugin references in a hash and returns the resolved reference data. | ||
Puppet::Functions.create_function(:resolve_references) do | ||
# Resolve references. | ||
# @param references A hash of reference data to resolve. | ||
# @return A hash of resolved reference data. | ||
# @example Resolve a hash of reference data | ||
# $references = { | ||
# "targets" => [ | ||
# "_plugin" => "terraform", | ||
# "dir" => "path/to/terraform/project", | ||
# "resource_type" => "aws_instance.web", | ||
# "uri" => "public_ip" | ||
# ] | ||
# } | ||
# | ||
# resolve_references($references) | ||
dispatch :resolve_references do | ||
param 'Data', :references | ||
return_type 'Data' | ||
end | ||
|
||
def resolve_references(references) | ||
unless Puppet[:tasks] | ||
raise Puppet::ParseErrorWithIssue | ||
.from_issue_and_stack( | ||
Bolt::PAL::Issues::PLAN_OPERATION_NOT_SUPPORTED_WHEN_COMPILING, | ||
action: 'resolve_references' | ||
) | ||
end | ||
|
||
plugins = Puppet.lookup(:bolt_inventory).plugins | ||
plugins.resolve_references(references) | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
bolt-modules/boltlib/spec/fixtures/modules/test/tasks/references.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
|
||
values = { | ||
"value" => { | ||
"name" => "127.0.0.1" | ||
} | ||
} | ||
|
||
puts values.to_json |
61 changes: 61 additions & 0 deletions
61
bolt-modules/boltlib/spec/functions/resolve_references_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
require 'bolt/plugin' | ||
|
||
describe 'resolve_references' do | ||
include PuppetlabsSpec::Fixtures | ||
let(:boltdir) { Bolt::Boltdir.new('./spec/fixtures') } | ||
let(:config) { Bolt::Config.new(boltdir, {}) } | ||
let(:pal) { Bolt::PAL.new(config.modulepath, config.hiera_config, config.boltdir.resource_types) } | ||
let(:pdb_client) { Bolt::PuppetDB::Client.new({}) } | ||
let(:analytics) { Bolt::Analytics::NoopClient.new } | ||
let(:plugins) { Bolt::Plugin.setup(config, pal, pdb_client, analytics) } | ||
let(:inventory) { Bolt::Inventory.new({}, plugins: plugins) } | ||
let(:tasks_enabled) { true } | ||
|
||
let(:references) do | ||
{ | ||
"targets" => { | ||
"_plugin" => "task", | ||
"task" => "test::references" | ||
} | ||
} | ||
end | ||
|
||
let(:resolved) do | ||
{ | ||
"targets" => { | ||
"name" => "127.0.0.1" | ||
} | ||
} | ||
end | ||
|
||
around(:each) do |example| | ||
Puppet[:tasks] = tasks_enabled | ||
Puppet.override(bolt_inventory: inventory) do | ||
example.run | ||
end | ||
end | ||
|
||
context 'calls resolve_references' do | ||
it 'resolves all plugin references' do | ||
is_expected.to run.with_params(references).and_return(resolved) | ||
end | ||
|
||
it 'errors when called with incorrect plugin data' do | ||
references['targets']['_plugin'] = 'fake_plugin' | ||
is_expected.to run.with_params(references).and_raise_error(/Unknown plugin/) | ||
end | ||
end | ||
|
||
context 'without tasks enabled' do | ||
let(:tasks_enabled) { false } | ||
|
||
it 'fails and reports that resolve_references is not available' do | ||
is_expected.to run | ||
.with_params(references) | ||
.and_raise_error(/Plan language function 'resolve_references' cannot be used/) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.