Skip to content

Commit b4f1264

Browse files
authored
Add __PARAMETER__, __CLASS__ and __MODULE__ tags (crayfishx#62)
* Added __PARAMETER__, __CLASS__ and __MODULE__ tags
1 parent 4abab44 commit b4f1264

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Gemfile.lock

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ The following mandatory Hiera 5 options must be set for each level of the hierar
5959
6060
`name`: A human readable name for the lookup
6161
`lookup_key`: This option must be set to `hiera_http`
62-
`uris` or `uri`: An array of URI's passed to `uris` _or_ a single URI passed to `uri`
62+
`uris` or `uri`: An array of URI's passed to `uris` _or_ a single URI passed to `uri`. This option supports interpolating special tags, see below.
6363

6464

6565
The following are optional configuration parameters supported in the `options` hash of the Hiera 5 config
@@ -98,12 +98,16 @@ The following are optional configuration parameters supported in the `options` h
9898

9999
`:headers:`: Hash of headers to send in the request
100100

101-
### Using the key name as part of the URI
101+
### Interpolating special tags
102102

103-
Previous versions of this backed allowed the use of `%{key}` to include the key
104-
name as part of the URL. Due to API changes in Hiera v5, this interpolation is
105-
no longer possible. This backend now supports an alternative method to include
106-
the key name using the `__KEY__` tag.
103+
Previous versions of this backed allowed the use of variables such as `%{key}` and `%{calling_module}` to be used in the URL, this has changed with Hiera 5. To allow for similar behaviour you can use a number of tags surrounded by `__` to interpolate special variables derived from the key into the `uri` or `uris` option in hiera.yaml. Currently you can interpolate `__KEY__`, `__MODULE__`, `__CLASS__` and `__PARAMETER__`, these tags are derived from parsing the original lookup key.
104+
105+
In the case of a lookup key matching `foo::bar::tango` the following tags are available;
106+
107+
* `__KEY__` : The original lookup key unchanched; `foo::bar::tango`
108+
* `__MODULE__` : The first part of the lookup key; `foo`
109+
* `__CLASS__` : All but the last parts of the lookup key; `foo::bar`
110+
* `__PARAMETER__` : The last part of they key representing the class parameter; `tango`
107111

108112
Example using this backend to interact with the [Puppet Enterprise Jenkins Pipeline plugin](https://wiki.jenkins.io/display/JENKINS/Puppet+Enterprise+Pipeline+Plugin)
109113

@@ -124,10 +128,6 @@ hierarchy:
124128
options:
125129
output: json
126130
failure: graceful
127-
# use_auth: true
128-
# auth_user: ''
129-
# auth_pass: ''
130-
131131
```
132132

133133
### Author

lib/puppet/functions/hiera_http.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,33 @@ def lookup_key(key, options, context)
2727
end
2828
end
2929

30-
options['uri'].gsub! '__KEY__', key
30+
options['uri'] = parse_tags(key, options['uri'])
3131
result = http_get(context, options)
3232

3333
answer = result.is_a?(Hash) ? result[key] : result
3434
context.not_found if answer.nil?
3535
return answer
3636
end
3737

38+
def parse_tags(key,uri)
39+
key_parts = key.split(/::/)
40+
parsed_uri = uri.gsub(/__(\w+)__/i) { |tag|
41+
case tag
42+
when '__KEY__'
43+
key
44+
when '__MODULE__'
45+
key_parts.first if key_parts.length > 1
46+
when '__CLASS__'
47+
key_parts[0..-2].join('::') if key_parts.length > 1
48+
when '__PARAMETER__'
49+
key_parts.last
50+
end
51+
}
52+
return parsed_uri
53+
end
54+
55+
56+
3857
def http_get(context, options)
3958
uri = URI.parse(options['uri'])
4059
host, port, path = uri.host, uri.port, URI.escape(context.interpolate(uri.request_uri))

spec/functions/hiera_http_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,36 @@
4040
expect(@lookuphttp).to receive(:get_parsed).with('/path/foo::bar::tango')
4141
function.lookup_key('foo::bar::tango', options, @context)
4242
end
43+
44+
it "should interpolate __MODULE__ correctly" do
45+
options = { 'uri' => 'http://localhost/path/__MODULE__' }
46+
expect(@context).to receive(:interpolate).with('/path/foo').and_return('/path/foo')
47+
expect(@lookuphttp).to receive(:get_parsed).with('/path/foo')
48+
function.lookup_key('foo::bar::tango', options, @context)
49+
end
50+
51+
it "should interpolate __CLASS__ correctly" do
52+
options = { 'uri' => 'http://localhost/path/__CLASS__' }
53+
expect(@context).to receive(:interpolate).with('/path/foo::bar').and_return('/path/foo::bar')
54+
expect(@lookuphttp).to receive(:get_parsed).with('/path/foo::bar')
55+
function.lookup_key('foo::bar::tango', options, @context)
56+
end
57+
58+
it "should interpolate __PARAMETER__ correctly" do
59+
options = { 'uri' => 'http://localhost/path/__PARAMETER__' }
60+
expect(@context).to receive(:interpolate).with('/path/tango').and_return('/path/tango')
61+
expect(@lookuphttp).to receive(:get_parsed).with('/path/tango')
62+
function.lookup_key('foo::bar::tango', options, @context)
63+
end
64+
65+
it "should interpolate more than one field" do
66+
options = { 'uri' => 'http://localhost/path/__MODULE__/__PARAMETER__/__PARAMETER__' }
67+
expect(@context).to receive(:interpolate).with('/path/foo/tango/tango').and_return('/path/foo/tango/tango')
68+
expect(@lookuphttp).to receive(:get_parsed).with('/path/foo/tango/tango')
69+
function.lookup_key('foo::bar::tango', options, @context)
70+
end
71+
72+
4373
end
4474

4575
context "When confine_to_keys is set" do

0 commit comments

Comments
 (0)