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

(HC-33) Support adding element when array does not exist yet #25

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
40 changes: 27 additions & 13 deletions lib/puppet/provider/hocon_setting/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ def self.namevar(section_name, setting)
end

def exists?
if resource[:type] == 'array_element'
Array(@resource[:value]).each do |v|
if value.flatten.include?(v)
return true
ret_value = false

if conf_file.has_value?(setting)
if resource[:type] == 'array_element'
Array(@resource[:value]).each do |v|
if value.flatten.include?(v)
return true
end
end
else
ret_value = true
end
return false
else
conf_file.has_value?(setting)
end

return ret_value
end

def create
Expand All @@ -44,13 +49,22 @@ def type=(value)
end

def value
val = conf_object.get_value(setting).unwrapped

# This is required because of :array_matching => :all.
# Without this, Puppet will almost always register changes
# to a hocon_setting even when it shouldn't.
val = conf_file.has_value?(setting) ?
conf_object.get_value(setting).unwrapped : []
unless val.is_a?(Array)
val = [val]
if resource[:type] == 'array_element'
# If the current value of the target setting is not an array,
# present the current value as an empty array so that an
# element is added to an empty array (as opposed to converting
# the current value into the first element in an array and
# adding the value to set as a second element in the array).
val = []
else
# This is required because of :array_matching => :all.
# Without this, Puppet will almost always register changes
# to a hocon_setting even when it shouldn't.
val = [val]
end
end
val
end
Expand Down
84 changes: 84 additions & 0 deletions spec/unit/puppet/provider/conf_setting/ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,90 @@ def validate_file(expected_content,tmpfile = tmpfile)
EOS
)
end

it "should add 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',
:ensure => 'present',
:value => { 'foo' => 'foovalue3' },
:type => 'array_element'))
provider = described_class.new(resource)
expect(provider.exists?).to be false
provider.create
validate_file(
<<-EOS
test_key_1: [
{
foo: foovalue
bar: barvalue
master: true
}
,
{
foo: foovalue2
baz: bazvalue
url: "http://192.168.1.1:8080"
}
,
{
foo: foovalue3
}
]
test_key_2 : [

{

"foo" : "foovalue3"

}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the whitespace behavior here seems really bizarre to me, but I assume that's controlled by logic in the hocon gem - nothing that the Puppet module can do to avoid it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@camlow325 That's correct, the whitespace behavior is controlled entirely by the hocon gem. It definitely seems weird to me that there seem to be some extra newlines being generated, so maybe there are some improvements that need to be made there.



]
EOS
)
end

it "should add an array element even if the target setting is not an array" do
File.open(tmpfile, 'a') do |fh|
fh.write('test_key_2: 3')
end
resource = Puppet::Type::Hocon_setting.new(
common_params.merge(:setting => 'test_key_2',
:ensure => 'present',
:value => { 'foo' => 'foovalue3' },
:type => 'array_element'))
provider = described_class.new(resource)
expect(provider.exists?).to be false
provider.create
validate_file(
<<-EOS
test_key_1: [
{
foo: foovalue
bar: barvalue
master: true
}
,
{
foo: foovalue2
baz: bazvalue
url: "http://192.168.1.1:8080"
}
,
{
foo: foovalue3
}
]
test_key_2: [
{
"foo" : "foovalue3"
}

]
EOS
)
end
end

context "when ensuring that a setting is present" do
Expand Down