Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some glue fixes: announcement, to_manifest, to_hierayaml #77

Merged
merged 3 commits into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/puppet/resource_api/glue.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -46,7 +48,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
Expand All @@ -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
2 changes: 1 addition & 1 deletion misc/ANNOUNCEMENT_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/simple_get_filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions spec/puppet/resource_api/glue_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -71,11 +73,17 @@
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
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