Skip to content

Commit

Permalink
Fix HC-99
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
trevor-vaughan committed Jan 7, 2020
1 parent 95c8bd7 commit 3305c30
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
55 changes: 39 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!-- vim-markdown-toc GFM -->

* [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)

<!-- vim-markdown-toc -->

## Overview

Expand All @@ -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',
Expand All @@ -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!"
Expand All @@ -53,26 +65,37 @@ 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!',
}
```

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
Expand Down
6 changes: 4 additions & 2 deletions lib/puppet/type/hocon_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/puppet/provider/conf_setting/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 3305c30

Please sign in to comment.