Skip to content

Commit

Permalink
Always convert the content param to string, change the default to 'true'
Browse files Browse the repository at this point in the history
- Change the default value of the content parameter to 'true'.
- Always convert its value to string

The above changes make it now possible to use:
- `puppet resource fact` (on CLI) to show all external facts
- `resources { 'fact': purge => true } on a puppet manifest, to remove all
  unmanaged external facts

We are now able to omit the content parameter, since it has a default
value.

Accepting boolean values for the content parameter is also now possible.

Signed-off-by: Vikraman Choudhury <vikraman.choudhury@gmail.com>
Signed-off-by: Theo Chatzimichos <tampakrap@gmail.com>

fixes #9
  • Loading branch information
tampakrap committed May 30, 2015
1 parent b765b93 commit 8b66216
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 10 additions & 3 deletions lib/puppet/type/fact.rb
Expand Up @@ -19,15 +19,22 @@

newproperty(:content) do
desc "The content of the fact"
defaultto ''
defaultto 'true'
validate do |value|
fail("Content cannot be empty or whitespace") if value.match(/^\s*$/)
fail("Content cannot be empty or whitespace") if munge(value).match(/^\s*$/)
end

munge do |value|
value.to_s
end

def insync?(is)
is == should
end
end

newproperty(:target) do
desc "Target txt file to write under /etc/facter/facts.d"
defaultto { @resource[:name] }
end

end
13 changes: 11 additions & 2 deletions spec/integration/type/fact_spec.rb
Expand Up @@ -3,8 +3,12 @@
describe Puppet::Type.type(:fact) do

context "when validating content" do
it "should be mandatory" do
expect { described_class.new(:name => "environment") }.to raise_error
it "can be optional" do
expect { described_class.new(:name => "environment") }.to_not raise_error
end

it "should return the default value" do
described_class.new(:name => "environment").should(:content).should == "true"
end

it "should reject empty string" do
Expand All @@ -19,6 +23,11 @@
expect { described_class.new(:name => "environment", :content => "production") }.to_not raise_error
expect { described_class.new(:name => "environment", :content => " production") }.to_not raise_error
end

it "should convert boolean to string" do
described_class.new(:name => "is_hypervisor", :content => true).should(:content).should == "true"
described_class.new(:name => "is_hypervisor", :content => false).should(:content).should == "false"
end
end

context "when validating target" do
Expand Down

0 comments on commit 8b66216

Please sign in to comment.