# Custom function to extract the current number of replicas for a pacemaker
# resource, as defined in the pacemaker cluster.
# Input is the name of a pacemaker bundle resource
# Output is the number of replicas for that resource or 0 if not found
Puppet::Functions.create_function(:'pacemaker_bundle_replicas') do
  dispatch :pacemaker_bundle_replicas do
    param 'String', :bundle
    return_type 'Integer'
  end

  def pacemaker_bundle_replicas(bundle)
    # the name of the node holding the replicas attribute varies based on the
    # container engine used (podman, docker...), so match via attributes instead
    replicas = `cibadmin -Q | xmllint --xpath "string(//bundle[@id='#{bundle}']/*[boolean(@image) and boolean(@run-command)]/@replicas)" -`

    # post-condition: 0 in case the bundle does not exist or an error occurred
    if $?.success? && !replicas.empty?
      return Integer(replicas)
    else
      return 0
    end
  end
end

# Custom function to extract the current number of replicas for a pacemaker
# resource, as defined in the pacemaker cluster.
# Input is the name of a pacemaker bundle resource
# Output is the number of replicas for that resource or 0 if not found
Puppet::Functions.create_function(:'pacemaker_bundle_replicas') do
  dispatch :pacemaker_bundle_replicas do
    param 'String', :bundle
    return_type 'Integer'
  end

  def pacemaker_bundle_replicas(bundle)
    # the name of the node holding the replicas attribute varies based on the
    # container engine used (podman, docker...), so match via attributes instead
    replicas = `cibadmin -Q | xmllint --xpath "string(//bundle[@id='#{bundle}']/*[boolean(@image) and boolean(@run-command)]/@replicas)" -`
    # strip line break
    replicas.strip!
    # post-condition: 0 in case the bundle does not exist or an error occurred
    if $?.success? && !replicas.empty?
      return Integer(replicas)
    else
      return 0
    end
  end
end

Trim line breaks from pacemaker bundle function
Trim line breaks from pacemaker bundle function

On CentOS9, the cibadmin query is returning '\n' if the bundle is not
defined which results in:

Error: Evaluation Error: Error while evaluating a Function Call, invalid
value for Integer(): "\n" (line: 1, column: 1) on node
standalone-0.localdomain

This change strips whitespace from the check to ensure it has a value
before we attempt to convert to an integer.

Change-Id: I369644d491357a8cdeae3eff51f4f595b1033b66
