Skip to content

Commit c6d1b3a

Browse files
committed
(FM-7839) Implement 'to_json' method for ResourceShim for bolt
bolt's `get_resources` uses the `libexec/query_resources.rb` to retrieve resources as json from the target nodes. Especially https://github.com/puppetlabs/bolt/blob/7dec6e49d66c7f9ae06088d46ee3330de0ed2fed/libexec/query_resources.rb#L71 causes `ResourceShims` to fail as they did not implement `to_json`. This integration test models what `libexec/query_resources.rb` is doing. The added `to_json(*)` method now passes this test, and - shown in manual testing - works for bolt.
1 parent 8ee1220 commit c6d1b3a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

lib/puppet/resource_api/glue.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def to_hash
4545
values
4646
end
4747

48+
def to_json(*)
49+
attrs = filtered_keys.map { |k| [k.to_s, values[k]] unless values[k].nil? }
50+
attributes = Hash[*attrs.compact.flatten]
51+
resource = { title => attributes }
52+
resource.to_json
53+
end
54+
4855
# attribute names that are not title or namevars
4956
def filtered_keys
5057
values.keys.reject { |k| k == :title || !attr_def[k] || (attr_def[k][:behaviour] == :namevar && @namevars.size == 1) }

spec/integration/resource_api_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ def get(_context)
223223
describe 'its title' do
224224
it { expect(instance.to_resource.title).to eq 'somename' }
225225
end
226+
227+
it 'can serialize to json' do
228+
expect({ 'resources' => [instance.to_resource] }.to_json).to eq '{"resources":[{"somename":{"ensure":"absent","boolean_param":false,"integer_param":99,"float_param":3.21,"ensure_param":"present","variant_pattern_param":"1234ABCD","url_param":"http://www.puppet.com","string_array_param":"d","e":"f","string_param":"default value"}}]}' # rubocop:disable Metrics/LineLength
229+
end
226230
end
227231

228232
it('ensure is reported as a symbol') { expect(instance[:ensure]).to be_a Symbol }

spec/puppet/resource_api/glue_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,23 @@
4747
end
4848
end
4949

50+
describe '.to_json' do
51+
it { expect(instance.to_json).to eq '{"title":{"attr":"value","attr_ro":"fixed"}}' }
52+
53+
context 'with nil values' do
54+
subject(:instance) do
55+
described_class.new({ namevarname: title, attr: nil, attr_ro: 'fixed' }, 'typename', [:namevarname],
56+
namevarname: { type: 'String', behaviour: :namevar, desc: 'the title' },
57+
attr: { type: 'String', desc: 'a string parameter' },
58+
attr_ro: { type: 'String', desc: 'a string readonly', behaviour: :read_only })
59+
end
60+
61+
it 'doesn\'t output them' do
62+
expect(instance.to_json).to eq '{"title":{"attr_ro":"fixed"}}'
63+
end
64+
end
65+
end
66+
5067
describe '.to_hierayaml' do
5168
it { expect(instance.to_hierayaml).to eq " title:\n attr: value\n attr_ro: fixed\n" }
5269

0 commit comments

Comments
 (0)