Puppet Function: influxdb::retrieve_token

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

Overview

Signatures:

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

    Parameters:

    • uri (String)
    • token_name (String)
    • admin_token_file (String)
    • use_system_store (Boolean)

    Returns:

    • (Any)
  • influxdb::retrieve_token(String $uri, String $token_name, Sensitive $admin_token, Boolean $use_system_store)Any

    Parameters:

    • uri (String)
    • token_name (String)
    • admin_token (Sensitive)
    • use_system_store (Boolean)

    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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/puppet/functions/influxdb/retrieve_token.rb', line 4

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

  dispatch :retrieve_token do
    param 'String', :uri
    param 'String', :token_name
    param 'Sensitive', :admin_token
    param 'Boolean', :use_system_store
  end

  def retrieve_token_file(uri, token_name, admin_token_file, use_system_store)
    admin_token = File.read(admin_token_file)
    retrieve_token(uri, token_name, admin_token, use_system_store)
  rescue Errno::EISDIR, Errno::EACCES, Errno::ENOENT => e
    Puppet.err("Unable to retrieve #{token_name}": e.message)
    nil
  end

  def retrieve_token(uri, token_name, admin_token, use_system_store)
    if admin_token.is_a?(Puppet::Pops::Types::PSensitiveType::Sensitive)
      admin_token = admin_token.unwrap
    end

    client = Puppet.runtime[:http]
    client_options = if use_system_store
                       { include_system_store: true }
                     else
                       {}
                     end

    response = client.get(URI(uri + '/api/v2/authorizations'),
                          headers: { 'Authorization' => "Token #{admin_token}", options: client_options })

    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