From 2ba116be93e424b864ff8b48537afed0c15b2fa0 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 9 May 2018 14:55:45 +0100 Subject: [PATCH 1/3] (maint) add puppet6-nightlies hint to announcement template --- misc/ANNOUNCEMENT_TEMPLATE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/ANNOUNCEMENT_TEMPLATE.md b/misc/ANNOUNCEMENT_TEMPLATE.md index 2c7ef1f0..104e6fc4 100644 --- a/misc/ANNOUNCEMENT_TEMPLATE.md +++ b/misc/ANNOUNCEMENT_TEMPLATE.md @@ -16,7 +16,7 @@ Hi all, We're pleased to announce that version X.Y.Z of the Resource API is being released today. -The Resource API provides a simple way to create new native resources in the form of types and providers for Puppet. It is provided as a Ruby gem to be referenced within modules. Support for it has been included as an experimental feature in version 1.4 of the Puppet Development Kit (`pdk new provider`). Use the [resource_api module](https://forge.puppet.com/puppetlabs/resource_api) to deploy it in your infrastructure. +The Resource API provides a simple way to create new native resources in the form of types and providers for Puppet. It is provided as a Ruby gem to be referenced within modules. Support for it has been included as an experimental feature in version 1.4 of the Puppet Development Kit (`pdk new provider`). Use the [resource_api module](https://forge.puppet.com/puppetlabs/resource_api) or the [puppet 6 nightly packages](https://groups.google.com/d/msg/puppet-users/N3LJGhsrqkU/TUEsq7VfDQAJ) to deploy it in your infrastructure. The new release of the Resource API provides the following enhancements: From 6a7fb315dac5bd21be830661d814e22118dae9d6 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 9 May 2018 15:08:47 +0100 Subject: [PATCH 2/3] (maint) correctly quote and escape the title in to_manifest using format_value_for_display makes the output uniform with all other attributes, and what puppet would produce by default. --- lib/puppet/resource_api/glue.rb | 2 +- spec/acceptance/device_spec.rb | 2 +- spec/acceptance/simple_get_filter_spec.rb | 4 ++-- spec/puppet/resource_api/glue_spec.rb | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/puppet/resource_api/glue.rb b/lib/puppet/resource_api/glue.rb index e94a04cd..266778d3 100644 --- a/lib/puppet/resource_api/glue.rb +++ b/lib/puppet/resource_api/glue.rb @@ -46,7 +46,7 @@ def prune_parameters(*_args) end def to_manifest - (["#{@typename} { #{values[@namevar].inspect}: "] + values.keys.reject { |k| k == @namevar }.map do |k| + (["#{@typename} { #{Puppet::Parameter.format_value_for_display(values[@namevar])}: "] + values.keys.reject { |k| k == @namevar }.map do |k| cs = ' ' ce = '' if attr_def[k][:behaviour] && attr_def[k][:behaviour] == :read_only diff --git a/spec/acceptance/device_spec.rb b/spec/acceptance/device_spec.rb index 9271c1f7..0e5463ed 100644 --- a/spec/acceptance/device_spec.rb +++ b/spec/acceptance/device_spec.rb @@ -14,7 +14,7 @@ describe 'using `puppet resource`' do it 'reads resources from the target system' do stdout_str, status = Open3.capture2e("puppet resource #{common_args} device_provider") - expected_values = 'device_provider { \"wibble\": \n\s+ensure => \'present\',\n\s+string => \'sample\',\n\#\s+string_ro => \'fixed\', # Read Only\n}' + expected_values = 'device_provider { \'wibble\': \n\s+ensure => \'present\',\n\s+string => \'sample\',\n\#\s+string_ro => \'fixed\', # Read Only\n}' expect(stdout_str.strip).to match %r{\A(DL is deprecated, please use Fiddle\n)?#{expected_values}\Z} expect(status).to eq 0 end diff --git a/spec/acceptance/simple_get_filter_spec.rb b/spec/acceptance/simple_get_filter_spec.rb index 6ebe95be..2c39c7b4 100644 --- a/spec/acceptance/simple_get_filter_spec.rb +++ b/spec/acceptance/simple_get_filter_spec.rb @@ -11,8 +11,8 @@ it 'does not receive names array' do stdout_str, status = Open3.capture2e("puppet resource #{common_args} test_simple_get_filter") - expect(stdout_str.strip).to match %r{^test_simple_get_filter \{ \"bar\"} - expect(stdout_str.strip).to match %r{^test_simple_get_filter \{ \"foo\"} + expect(stdout_str.strip).to match %r{^test_simple_get_filter \{ 'bar'} + expect(stdout_str.strip).to match %r{^test_simple_get_filter \{ 'foo'} expect(status).to eq 0 end end diff --git a/spec/puppet/resource_api/glue_spec.rb b/spec/puppet/resource_api/glue_spec.rb index 770a6b31..ba6920e5 100644 --- a/spec/puppet/resource_api/glue_spec.rb +++ b/spec/puppet/resource_api/glue_spec.rb @@ -71,7 +71,7 @@ end describe '.to_manifest' do - it { expect(instance.to_manifest).to eq "typename { \"title\": \n attr => 'value',\n# attr_ro => 'fixed', # Read Only\n}" } + it { expect(instance.to_manifest).to eq "typename { 'title': \n attr => 'value',\n# attr_ro => 'fixed', # Read Only\n}" } end describe '.to_hierayaml' do From 478c9254238749d9af8a1633b53f95ed1a01dcde Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Wed, 9 May 2018 15:27:56 +0100 Subject: [PATCH 3/3] (maint) properly escape YAML-specials in to_hierayaml --- lib/puppet/resource_api/glue.rb | 5 ++++- spec/puppet/resource_api/glue_spec.rb | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/puppet/resource_api/glue.rb b/lib/puppet/resource_api/glue.rb index 266778d3..c431ed58 100644 --- a/lib/puppet/resource_api/glue.rb +++ b/lib/puppet/resource_api/glue.rb @@ -1,3 +1,5 @@ +require 'yaml' + module Puppet; end # rubocop:disable Style/Documentation module Puppet::ResourceApi # A trivial class to provide the functionality required to push data through the existing type/provider parts of puppet @@ -59,7 +61,8 @@ def to_manifest # Convert our resource to yaml for Hiera purposes. def to_hierayaml - ([" #{values[@namevar]}: "] + values.keys.reject { |k| k == @namevar }.map { |k| " #{k}: #{Puppet::Parameter.format_value_for_display(values[k])}" }).join("\n") + "\n" + attributes = Hash[values.keys.reject { |k| k == @namevar }.map { |k| [k.to_s, values[k]] }] + YAML.dump('type' => { values[@namevar] => attributes }).split("\n").drop(2).join("\n") + "\n" end end end diff --git a/spec/puppet/resource_api/glue_spec.rb b/spec/puppet/resource_api/glue_spec.rb index ba6920e5..01023f92 100644 --- a/spec/puppet/resource_api/glue_spec.rb +++ b/spec/puppet/resource_api/glue_spec.rb @@ -48,12 +48,14 @@ describe Puppet::ResourceApi::ResourceShim do subject(:instance) do - described_class.new({ namevarname: 'title', attr: 'value', attr_ro: 'fixed' }, 'typename', :namevarname, + described_class.new({ namevarname: title, attr: 'value', attr_ro: 'fixed' }, 'typename', :namevarname, namevarname: { type: 'String', behaviour: :namevar, desc: 'the title' }, attr: { type: 'String', desc: 'a string parameter' }, attr_ro: { type: 'String', desc: 'a string readonly', behaviour: :read_only }) end + let(:title) { 'title' } + describe '.values' do it { expect(instance.values).to eq(namevarname: 'title', attr: 'value', attr_ro: 'fixed') } end @@ -75,7 +77,13 @@ end describe '.to_hierayaml' do - it { expect(instance.to_hierayaml).to eq " title: \n attr: 'value'\n attr_ro: 'fixed'\n" } + it { expect(instance.to_hierayaml).to eq " title:\n attr: value\n attr_ro: fixed\n" } + + context 'when the title contains YAML special characters' do + let(:title) { "foo:\nbar" } + + it { expect(instance.to_hierayaml).to eq " ? |-\n foo:\n bar\n : attr: value\n attr_ro: fixed\n" } + end end end end