Skip to content
Open
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
95 changes: 95 additions & 0 deletions lib/puppet/functions/augeas/augeas.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# This is an autogenerated function, ported from the original legacy version.
# It /should work/ as is, but will not have all the benefits of the modern
# function API. You should see the function docs to learn how to add function
# signatures for type safety and to document this function using puppet-strings.
#
# https://puppet.com/docs/puppet/latest/custom_functions_ruby.html
#
# ---- original file header ----
#
# augeas.rb
#

# ---- original file header ----
#
# @summary
# Modifies a string using Augeas.
#
#*Example:*
#
# augeas("proc /proc proc nodev,noexec,nosuid 0 0
#", 'Fstab.lns', ['rm ./1/opt[3]'])
#
#Would result in:
#
# "proc /proc proc nodev,noexec 0 0
#"
#
#
Puppet::Functions.create_function(:'augeas::augeas') do
# @param arguments
# The original array of arguments. Port this to individually managed params
# to get the full benefit of the modern function API.
#
# @return [Data type]
# Describe what the function returns here
#
dispatch :default_impl do
# Call the method named 'default_impl' when this is matched
# Port this to match individual params for better type safety
repeated_param 'Any', :arguments
end


def default_impl(*arguments)

unless Puppet.features.augeas?
raise Puppet::ParseError, ('augeas(): this function requires the augeas feature. See http://projects.puppetlabs.com/projects/puppet/wiki/Puppet_Augeas#Pre-requisites for how to activate it.')
end

# Check that 2 arguments have been given ...
raise(Puppet::ParseError, 'augeas(): Wrong number of arguments ' +
"given (#{arguments.size} for 3)") if arguments.size != 3

content = arguments[0]
lens = arguments[1]
changes = arguments[2]

# Check arguments
raise(Puppet::ParseError, 'augeas(): content must be a string') unless content.is_a?(String)
raise(Puppet::ParseError, 'augeas(): lens must be a string') unless lens.is_a?(String)
raise(Puppet::ParseError, 'augeas(): changes must be an array') unless changes.is_a?(Array)

require 'augeas'
aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
augeas_version = aug.get('/augeas/version')
raise(Puppet::ParseError, 'augeas(): requires Augeas 1.0.0 or greater') unless Puppet::Util::Package.versioncmp(augeas_version, '1.0.0') >= 0
raise(Puppet::ParseError, 'augeas(): requires ruby-augeas 0.5.0 or greater') unless aug.methods.include?(:text_store)

result = nil
begin
aug.set('/input', content)
aug.text_store(lens, '/input', '/store')
unless aug.match("/augeas/text/store//error").empty?
error = aug.get("/augeas/text/store//error/message")
raise Puppet::ParseError, "augeas(): Failed to parse string with lens #{lens}: #{error}"
end

# Apply changes
aug.context = '/store'
changes.each do |c|
r = aug.srun(c)
raise Puppet::ParseError, "augeas(): Failed to apply change to tree" unless r and r[0] >= 0
end
unless aug.text_retrieve(lens, '/input', '/store', '/output')
error = aug.get("/augeas/text/store//error/message")
raise Puppet::ParseError, "augeas(): Failed to apply changes with lens #{lens}: #{error}"
end
result = aug.get("/output")
ensure
aug.close
end
return result

end
end
41 changes: 41 additions & 0 deletions spec/functions/augeas_augeas_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe 'augeas::augeas' do
# without knowing details about the implementation, this is the only test
# case that we can autogenerate. You should add more examples below!
it { is_expected.not_to eq(nil) }

#################################
# Below are some example test cases. You may uncomment and modify them to match
# your needs. Notice that they all expect the base error class of `StandardError`.
# This is because the autogenerated function uses an untyped array for parameters
# and relies on your implementation to do the validation. As you convert your
# function to proper dispatches and typed signatures, you should change the
# expected error of the argument validation examples to `ArgumentError`.
#
# Other error types you might encounter include
#
# * StandardError
# * ArgumentError
# * Puppet::ParseError
#
# Read more about writing function unit tests at https://rspec-puppet.com/documentation/functions/
#
# it 'raises an error if called with no argument' do
# is_expected.to run.with_params.and_raise_error(StandardError)
# end
#
# it 'raises an error if there is more than 1 arguments' do
# is_expected.to run.with_params({ 'foo' => 1 }, 'bar' => 2).and_raise_error(StandardError)
# end
#
# it 'raises an error if argument is not the proper type' do
# is_expected.to run.with_params('foo').and_raise_error(StandardError)
# end
#
# it 'returns the proper output' do
# is_expected.to run.with_params(123).and_return('the expected output')
# end
#################################

end