Skip to content

Commit 0830311

Browse files
committed
MODULES-11309 : convert a string to a resource
1 parent 934328e commit 0830311

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
# @summary
4+
# This converts a string to a puppet resource.
5+
#
6+
# This attempts to convert a string like 'File[/foo]' into the
7+
# puppet resource `File['/foo']` as detected by the catalog.
8+
#
9+
# Things like 'File[/foo, /bar]' are not supported as a
10+
# title might contain things like ',' or ' '. There is
11+
# no clear value seperator to use.
12+
#
13+
# This function can depend on the parse order of your
14+
# manifests/modules as it inspects the catalog thus far.
15+
Puppet::Functions.create_function(:'stdlib::str2resource') do
16+
# @param res_string The string to lookup as a resource
17+
# @example
18+
# stdlib::str2resource('File[/foo]') => File[/foo]
19+
# @return Puppet::Resource
20+
dispatch :str2resource do
21+
param 'String', :res_string
22+
#return_type 'Puppet::Resource'
23+
return_type 'Any'
24+
end
25+
26+
def str2resource(res_string)
27+
type_name, title = Puppet::Resource.type_and_title(res_string, nil)
28+
29+
resource = closure_scope.findresource(type_name, title)
30+
31+
if resource.nil?
32+
raise(Puppet::ParseError, "stdlib::str2resource(): could not find #{type_name}[#{title}], this is parse order dependant and values should not be quoted")
33+
end
34+
35+
return resource
36+
end
37+
end

spec/functions/str2resource_spec.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe 'stdlib::str2resource' do
6+
context 'when default' do
7+
it { is_expected.not_to eq(nil) }
8+
it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{stdlib::str2resource}) }
9+
end
10+
11+
context 'when testing simple resource definitions exist' do
12+
let :pre_condition do
13+
<<-PRECOND
14+
file { 'foo': }
15+
file { '/foo': }
16+
file { 'foot': }
17+
user { 'foo': }
18+
PRECOND
19+
end
20+
21+
file_foo = Puppet::Resource.new(:file, 'foo')
22+
user_foo = Puppet::Resource.new(:user, 'foo')
23+
24+
it { is_expected.to run.with_params('File[foo]').and_return(file_foo) }
25+
it { is_expected.not_to run.with_params('File[\'foo\']') }
26+
it { is_expected.not_to run.with_params('File["foo"]') }
27+
28+
it { is_expected.to run.with_params('User[foo]').and_return(user_foo) }
29+
end
30+
31+
context 'when someone tries a compound definition' do
32+
let :pre_condition do
33+
'user { "foo, bar": }'
34+
end
35+
36+
user_foo_bar = Puppet::Resource.new(:user, 'foo, bar')
37+
38+
it { is_expected.to run.with_params('User[foo, bar]').and_return(user_foo_bar) }
39+
end
40+
41+
context 'when testing simple resource definitions no exist' do
42+
it { is_expected.not_to run.with_params('File[foo]') }
43+
end
44+
end

0 commit comments

Comments
 (0)