Skip to content

Commit

Permalink
Merge pull request #25 from camlow325/bug/master/HC-33-add-array-elem…
Browse files Browse the repository at this point in the history
…ent-when-not-already-in-target

(HC-33) Support adding element when array does not exist yet
  • Loading branch information
nwolfe committed Oct 31, 2015
2 parents c5b08c4 + eaf3947 commit d06c364
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 13 deletions.
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"
}
]
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

0 comments on commit d06c364

Please sign in to comment.