From dd5647f48111cfe965a78da710b67a1b3b9cac68 Mon Sep 17 00:00:00 2001 From: Trevor Vaughan Date: Mon, 6 Jan 2020 22:29:07 -0500 Subject: [PATCH] Fix HC-99 * Added a workaround for a bug in Puppet as demonstrated in HC-99 * Add key deletion example * Update README.md with example of deleting a top-level key * Add test as a verification example HC-99 #close --- README.md | 55 +++++++++++++------ lib/puppet/type/hocon_setting.rb | 6 +- .../puppet/provider/conf_setting/ruby_spec.rb | 10 ++++ 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 186863f..743fdf1 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,26 @@ #### Table of Contents -1. [Overview](#overview) -2. [Module Description - What the module does and why it is useful](#module-description) -3. [Setup - The basics of getting started with the hocon module](#setup) - * [Setup requirements](#setup-requirements) - * [Beginning with hocon](#beginning-with-hocon) -4. [Usage - Configuration options and additional functionality](#usage) -5. [Reference - An under-the-hood peek at what the module is doing and how](#reference) -6. [Development - Guide for contributing to the module](#development) + + + +* [Overview](#overview) +* [Module Description](#module-description) +* [Setup](#setup) +* [Beginning with hocon](#beginning-with-hocon) +* [Usage](#usage) +* [Reference](#reference) + * [Type: hocon_setting](#type-hocon_setting) + * [Parameters](#parameters) + * [`ensure`](#ensure) + * [`path`](#path) + * [`setting`](#setting) + * [`value`](#value) + * [`type`](#type) +* [Development](#development) +* [Contributors](#contributors) + + ## Overview @@ -31,7 +43,7 @@ To manage a HOCON file, add the resource type `hocon_setting` to a class. Manage individual settings in HOCON files by adding the `hocon_setting` resource type to a class. For example: -``` +```puppet hocon_setting { "sample setting": ensure => present, path => '/tmp/foo.conf', @@ -43,7 +55,7 @@ hocon_setting { "sample setting": To control a setting nested within a map contained at another setting, provide the path to that setting under the "setting" parameter, with each level separated by a ".". So to manage `barsetting` in the following map -``` +```puppet foo : { bar : { barsetting : "FOO!" @@ -53,10 +65,10 @@ foo : { You would put the following in your manifest: -``` +```puppet hocon_setting {'sample nested setting': ensure => present, - path => '/tmp/foo.conf', + path => '/tmp/foo.conf', setting => 'foo.bar.barsetting', value => 'BAR!', } @@ -64,15 +76,26 @@ hocon_setting {'sample nested setting': You can also set maps like so: -``` +```puppet hocon_setting { 'sample map setting': - ensure => present, - path => '/tmp/foo.conf', + ensure => present, + path => '/tmp/foo.conf', setting => 'hash_setting', - value => { 'a' => 'b' }, + value => { 'a' => 'b' }, } ``` +To delete a top level key, you will need to specify both the key name and the +type of key. + +```puppet +hocon_setting { 'delete top key': + ensure => absent, + path => '/tmp/foo.conf', + setting => 'array_key', + type => 'array', +``` + ## Reference ### Type: hocon_setting diff --git a/lib/puppet/type/hocon_setting.rb b/lib/puppet/type/hocon_setting.rb index a0b82e7..56b2677 100644 --- a/lib/puppet/type/hocon_setting.rb +++ b/lib/puppet/type/hocon_setting.rb @@ -84,8 +84,6 @@ end def insync?(is) - # TODO: this doesn't appear to get called, and according to Puppet's source - # it may be deprecated. if @resource[:type] == 'array_element' # make sure all passed values are in the file Array(@resource[:value]).each do |v| @@ -94,6 +92,10 @@ def insync?(is) end end true + elsif @resource[:type] == 'array' + # Works around a bug in Puppet + # See: https://tickets.puppetlabs.com/browse/HC-99 + is == @should else super end diff --git a/spec/unit/puppet/provider/conf_setting/ruby_spec.rb b/spec/unit/puppet/provider/conf_setting/ruby_spec.rb index ecf48d2..d5ad6f8 100644 --- a/spec/unit/puppet/provider/conf_setting/ruby_spec.rb +++ b/spec/unit/puppet/provider/conf_setting/ruby_spec.rb @@ -113,6 +113,16 @@ def validate_file(expected_content, tmp = tmpfile) ) end + it 'deletes an empty array when requested' do + resource = Puppet::Type::Hocon_setting.new(common_params.merge( + setting: 'test_key_1', ensure: 'absent', type: 'array', + )) + provider = described_class.new(resource) + expect(provider.exists?).to be true + provider.destroy + validate_file("\n") + end + it 'adds an array element even if the target setting does not yet exist' do resource = Puppet::Type::Hocon_setting.new( common_params.merge(setting: 'test_key_2',