# Generated by Puppet
LoadPlugin "curl_json"

<Plugin "curl_json">
<%- if @url.start_with? '/' -%>
  <Sock "<%= @url %>">
<%- else -%>
  <URL "<%= @url %>">
<%- end -%>
<% if scope.lookupvar('collectd::collectd_version_real') and (scope.function_versioncmp([scope.lookupvar('collectd::collectd_version_real'), '5.6']) >= 0) -%>
<%- if @host -%>
    Host "<%= @host %>"
<%- end -%>
<%- end -%>
    Instance "<%= @instance %>"
<% if scope.lookupvar('collectd::collectd_version_real') and (scope.function_versioncmp([scope.lookupvar('collectd::collectd_version_real'), '5.5']) >= 0) -%>
<% if @interval -%>
    Interval <%= @interval %>
<% end -%>
<% end -%>
<% if @user -%>
    User "<%= @user %>"
    Password "<%= @password %>"
<% end -%>
<% if scope.lookupvar('collectd::collectd_version_real') and (scope.function_versioncmp([scope.lookupvar('collectd::collectd_version_real'), '5.5']) >= 0) -%>
<% unless @digest.nil? -%>
    Digest <%= @digest %>
<% end -%>
<% end -%>
<% unless @verifypeer.nil? -%>
    VerifyPeer <%= @verifypeer %>
<% end -%>
<% unless @verifyhost.nil? -%>
    VerifyHost <%= @verifyhost %>
<% end -%>
<% if @cacert -%>
    CACert "<%= @cacert %>"
<% end -%>
<% if scope.lookupvar('collectd::collectd_version_real') and (scope.function_versioncmp([scope.lookupvar('collectd::collectd_version_real'), '5.3']) >= 0) -%>
<% if @header -%>
    Header "<%= @header %>"
<% end -%>
<%-
  def valid_json?(json)
    JSON.parse(json)
    return true
  rescue JSON::ParserError
    return false
  end
-%>
<% unless @post.nil? -%>
<% if valid_json?(@post) -%>
    Post "<%= @post.gsub('\\"','\\\\\\\\\"').gsub!(%r{(?<!\\)"},'\"') %>"
<% else -%>
    Post "<%= @post %>"
<% end -%>
<% end -%>
<% end -%>
<% if scope.lookupvar('collectd::collectd_version_real') and (scope.function_versioncmp([scope.lookupvar('collectd::collectd_version_real'), '5.5']) >= 0) -%>
<% unless @timeout.nil? -%>
    Timeout <%= @timeout %>
<% end -%>
<% end -%>
<% @keys.sort.each do |key,keydata| -%>
    <Key "<%= key %>">
      Type "<%= keydata['type'] %>"
<% if keydata['instance'] -%>
      Instance "<%= keydata['instance'] %>"
<% end -%>
    </Key>
<% end -%>
<%- if @url.start_with? '/' -%>
  </Sock>
<%- else -%>
  </URL>
<%- end -%>
</Plugin>

