Puppet Function: sensuclassic_sorted_json

Defined in:
lib/puppet/functions/sensuclassic_sorted_json.rb
Function type:
Ruby 4.x API

Overview

sensuclassic_sorted_json(Hash $hash, Optional[Boolean] $pretty)String

This function takes unsorted hash and outputs JSON object making sure the keys are sorted. Optionally you can pass a boolean as the second parameter, which controls if the output is pretty formatted.

Examples:

Calling function without pretty

unsorted_hash = {
  'client_addr' => '127.0.0.1',
  'bind_addr'   => '192.168.34.56',
  'start_join'  => [
    '192.168.34.60',
    '192.168.34.61',
    '192.168.34.62',
  ],
  'ports'       => {
    'rpc'   => 8567,
    'https' => 8500,
    'http'  => -1,
  },
}
sorted_json(unsorted_hash)

{"bind_addr":"192.168.34.56","client_addr":"127.0.0.1",
"ports":{"http":-1,"https":8500,"rpc":8567},
"start_join":["192.168.34.60","192.168.34.61","192.168.34.62"]}

Calling function with pretty output

sorted_json(unsorted_hash, true)

{
    "bind_addr": "192.168.34.56",
    "client_addr": "127.0.0.1",
    "ports": {
        "http": -1,
        "https": 8500,
        "rpc": 8567
    },
    "start_join": [
        "192.168.34.60",
        "192.168.34.61",
        "192.168.34.62"
    ]
}

Parameters:

  • hash (Hash)

    Hash to be sorted

  • pretty (Optional[Boolean])

    Boolean that determines if output should be pretty format

Returns:

  • (String)

    Returns a JSON string



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/puppet/functions/sensuclassic_sorted_json.rb', line 125

Puppet::Functions.create_function(:sensuclassic_sorted_json) do
  # @param hash Hash to be sorted
  # @param pretty Boolean that determines if output should be pretty format
  # @return [String] Returns a JSON string
  # @example Calling function without pretty
  #   unsorted_hash = {
  #     'client_addr' => '127.0.0.1',
  #     'bind_addr'   => '192.168.34.56',
  #     'start_join'  => [
  #       '192.168.34.60',
  #       '192.168.34.61',
  #       '192.168.34.62',
  #     ],
  #     'ports'       => {
  #       'rpc'   => 8567,
  #       'https' => 8500,
  #       'http'  => -1,
  #     },
  #   }
  #   sorted_json(unsorted_hash)
  #
  #   {"bind_addr":"192.168.34.56","client_addr":"127.0.0.1",
  #   "ports":{"http":-1,"https":8500,"rpc":8567},
  #   "start_join":["192.168.34.60","192.168.34.61","192.168.34.62"]}
  # @example Calling function with pretty output
  #   sorted_json(unsorted_hash, true)
  #
  #   {
  #       "bind_addr": "192.168.34.56",
  #       "client_addr": "127.0.0.1",
  #       "ports": {
  #           "http": -1,
  #           "https": 8500,
  #           "rpc": 8567
  #       },
  #       "start_join": [
  #           "192.168.34.60",
  #           "192.168.34.61",
  #           "192.168.34.62"
  #       ]
  #   }
  dispatch :sort_json do
    param 'Hash', :hash
    optional_param 'Boolean', :pretty
  end

  def sort_json(hash, pretty = false)
    if pretty
      return SensuClassicJSON.sorted_pretty_generate(hash, 4) << "\n"
    else
      return SensuClassicJSON.sorted_generate(hash)
    end
  end
end