Puppet Function: influxdb::retrieve_token

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

Overview

influxdb::retrieve_token(String $uri, String $token_name, String $admin_token_file)Any

Parameters:

  • uri (String)
  • token_name (String)
  • admin_token_file (String)

Returns:

  • (Any)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet/functions/influxdb/retrieve_token.rb', line 4

Puppet::Functions.create_function(:'influxdb::retrieve_token') do
  dispatch :retrieve_token do
    param 'String', :uri
    param 'String', :token_name
    param 'String', :admin_token_file
  end

  def retrieve_token(uri, token_name, admin_token_file)
    return unless File.file?(admin_token_file)
    begin
      admin_token = File.read(admin_token_file)

      client = Puppet.runtime[:http]
      response = client.get(URI(uri + '/api/v2/authorizations'),
                             headers: { 'Authorization' => "Token #{admin_token}" })

      if response.success?
        body = JSON.parse(response.body)
        token = body['authorizations'].find { |auth| auth['description'] == token_name }
        token ? token['token'] : nil
      else
        Puppet.err("Unable to retrieve #{token_name}": response.body)
        nil
      end
    rescue StandardError => e
      Puppet.err("Unable to retrieve #{token_name}": e.message)
      nil
    end
  end
end