Module: PuppetX::Puppetlabs::PuppetlabsInfluxdb

Included in:
Puppet::Provider::InfluxdbSetup::InfluxdbSetup
Defined in:
lib/puppet_x/puppetlabs/influxdb/influxdb.rb

Overview

Mixin module to provide constants and instance methods for the providers

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cert_storeObject

Returns the value of attribute cert_store.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def cert_store
  @cert_store
end

.client_optionsObject

Returns the value of attribute client_options.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def client_options
  @client_options
end

.hostObject

Returns the value of attribute host.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def host
  @host
end

.portObject

Returns the value of attribute port.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def port
  @port
end

.ssl_contextObject

Returns the value of attribute ssl_context.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def ssl_context
  @ssl_context
end

.token_fileObject

Returns the value of attribute token_file.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def token_file
  @token_file
end

.use_sslObject

Returns the value of attribute use_ssl.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def use_ssl
  @use_ssl
end

.use_system_storeObject

Returns the value of attribute use_system_store.



11
12
13
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 11

def use_system_store
  @use_system_store
end

Instance Attribute Details

#authObject

Returns the value of attribute auth.



24
25
26
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 24

def auth
  @auth
end

#bucket_hashObject

Returns the value of attribute bucket_hash.



24
25
26
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 24

def bucket_hash
  @bucket_hash
end

#dbrp_hashObject

Returns the value of attribute dbrp_hash.



24
25
26
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 24

def dbrp_hash
  @dbrp_hash
end

#label_hashObject

Returns the value of attribute label_hash.



24
25
26
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 24

def label_hash
  @label_hash
end

#telegraf_hashObject

Returns the value of attribute telegraf_hash.



24
25
26
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 24

def telegraf_hash
  @telegraf_hash
end

#user_mapObject

Returns the value of attribute user_map.



24
25
26
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 24

def user_map
  @user_map
end

Instance Method Details

#get_bucket_infoObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 169

def get_bucket_info
  response = influx_get('/api/v2/buckets')
  response.each do |r|
    next unless r['buckets']
    r['buckets'].each do |bucket|
      process_links(bucket, bucket['links'])
      @bucket_hash << bucket
    end
  end
rescue StandardError => e
  Puppet.err("Error getting bucket state: #{e.message}")
  Puppet.err(e.backtrace)
  nil
end

#get_dbrp_infoObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 184

def get_dbrp_info
  # org is a mandatory parameter, so we have to look over each org to get all dbrps
  # get_org_info must be called before this
  orgs = @org_hash.map { |org| org['id'] }
  orgs.each do |org|
    dbrp_response = influx_get("/api/v2/dbrps?orgID=#{org}")
    dbrp_response.each do |r|
      next unless r['content']
      r['content'].each do |dbrp|
        @dbrp_hash << dbrp.merge('name' => dbrp['database'])
      end
    end
  end
rescue StandardError => e
  Puppet.err("Error getting dbrp state: #{e.message}")
  Puppet.err(e.backtrace)
  nil
end

#get_label_infoObject

No links entries for labels other than self



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 219

def get_label_info
  response = influx_get('/api/v2/labels')
  response.each do |r|
    next unless r['labels']
    r['labels'].each do |label|
      @label_hash << label
    end
  end
rescue StandardError => e
  Puppet.err("Error getting label state: #{e.message}")
  Puppet.err(e.backtrace)
  nil
end

#get_org_infoObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 153

def get_org_info
  response = influx_get('/api/v2/orgs')

  response.each do |r|
    next unless r['orgs']
    r['orgs'].each do |org|
      process_links(org, org['links'])
      @org_hash << org
    end
  end
rescue StandardError => e
  Puppet.err("Error getting org state: #{e.message}")
  Puppet.err(e.backtrace)
  nil
end

#get_user_infoObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 203

def 
  response = influx_get('/api/v2/users')
  response.each do |r|
    next unless r['users']
    r['users'].each do |user|
      process_links(user, user['links'])
      @user_map << user
    end
  end
rescue StandardError => e
  Puppet.err("Error getting user state: #{e.message}")
  Puppet.err(e.backtrace)
  nil
end

#id_from_name(hashes, name) ⇒ Object

Helper methods to map names to internal IDs



74
75
76
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 74

def id_from_name(hashes, name)
  hashes.select { |user| user['name'] == name }.map { |user| user['id'] }.first
end

#influx_delete(name) ⇒ Object

Raises:

  • (Puppet::DevError)


139
140
141
142
143
144
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 139

def influx_delete(name)
  response = @client.delete(URI(@influxdb_uri + name), headers: @auth, options: @client_options)
  raise Puppet::DevError, "Received HTTP code #{response.code} for delete #{name} with message #{response.reason}" unless response.success?

  JSON.parse(response.body ? response.body : '{}')
end

#influx_get(name, results = []) ⇒ Object

Helper function to process paginated responses. Returns an array of responses



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 83

def influx_get(name, results = [])
  # Return the current data if there is no 'next' object
  return results if name.nil?

  response = @client.get(URI(@influxdb_uri + name), headers: @auth, options: @client_options)
  if response.success?
    # Recursively append the results of calling the URL in the 'next' object to our array
    body = JSON.parse(response.body)
    results << body
    influx_get(body.dig('links', 'next'), results)
  # We may receive a 404 if the api path doesn't exists, such as a /links request for an org with no labels
  # We won't consider this a fatal error
  elsif response.code == 404
    results
  else
    raise Puppet::DevError, "Received HTTP code #{response.code} for get #{name} with message #{response.reason}"
  end
rescue StandardError => e
  Puppet.err("Error in get call: #{e.message}")
  Puppet.err(e.backtrace)
  []
end

#influx_patch(name, body) ⇒ Object

Our HTTP class doesn’t have a patch method, so we create the connection and use Net::HTTP manually



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 125

def influx_patch(name, body)
  @client.connect(URI(@influxdb_uri), options: @client_options) do |conn|
    request = Net::HTTP::Patch.new(@influxdb_uri + name)
    request['Content-Type'] = 'application/json'
    request['Authorization'] = @auth[:Authorization]
    request.body = body

    response = conn.request(request)
    raise Puppet::DevError, "Received HTTP code #{response.code} for patch #{name} with message #{response.reason}" unless response.is_a?(Net::HTTPSuccess)

    JSON.parse(response.body ? response.body : '{}')
  end
end

#influx_post(name, body) ⇒ Object

def influx_get(name)

_influx_get(name)

end

Raises:

  • (Puppet::DevError)


110
111
112
113
114
115
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 110

def influx_post(name, body)
  response = @client.post(URI(@influxdb_uri + name), body, headers: @auth.merge({ 'Content-Type' => 'application/json' }), options: @client_options)
  raise Puppet::DevError, "Received HTTP code '#{response.code}' for post #{name} with message '#{response.reason}' '#{body}" unless response.success?

  JSON.parse(response.body ? response.body : '{}')
end

#influx_put(name, body) ⇒ Object

Raises:

  • (Puppet::DevError)


117
118
119
120
121
122
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 117

def influx_put(name, body)
  response = @client.put(URI(@influxdb_uri + name), body, headers: @auth.merge({ 'Content-Type' => 'application/json' }), options: @client_options)
  raise Puppet::DevError, "Received HTTP code #{response.code} for put #{name} with message #{response.reason}" unless response.success?

  JSON.parse(response.body ? response.body : '{}')
end

#influx_setupObject



146
147
148
149
150
151
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 146

def influx_setup
  response = influx_get('/api/v2/setup')
  response['allowed'] == false
rescue StandardException
  false
end

#init_attrs(resources) ⇒ Object

Make class instance variables available as instance variables to whichever object calls this method For subclasses which call super, the instance variables will be part of their scope



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 41

def init_attrs(resources)
  # TODO: this can probably be refactored into a proper cache of resources
  resources.each do |resource|
    @host ||= resource[:host] ? resource[:host] : PuppetlabsInfluxdb.host
    @port ||= resource[:port] ? resource[:port] : PuppetlabsInfluxdb.port
    @use_ssl ||= (!resource[:use_ssl].nil?) ? resource[:use_ssl] : PuppetlabsInfluxdb.use_ssl
    @use_system_store ||= resource[:use_system_store] ? resource[:use_system_store] : PuppetlabsInfluxdb.use_system_store
    @token ||= resource[:token]
    @token_file ||= resource[:token_file] ? resource[:token_file] : PuppetlabsInfluxdb.token_file
  end

  protocol = @use_ssl ? 'https' : 'http'
  @influxdb_uri = "#{protocol}://#{@host}:#{@port}"

  @client_options = if @use_system_store
                      { include_system_store: true }
                    else
                      {}
                    end
end

#init_authObject



62
63
64
65
66
67
68
69
70
71
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 62

def init_auth
  @auth = if @token
            { Authorization: "Token #{@token.unwrap}" }
          elsif @token_file && File.file?(@token_file)
            token = File.read(@token_file)
            { Authorization: "Token #{token}" }
          else
            {}
          end
end

#initializeObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 26

def initialize
  @client ||= Puppet.runtime[:http]
  @cert_store ||= OpenSSL::X509::Store.new
  @org_hash = []
  @telegraf_hash = []
  @label_hash = []
  @user_map = []
  @bucket_hash = []
  @dbrp_hash = []
  @auth = {}
  @self_hash = []
end

#name_from_id(hashes, id) ⇒ Object



78
79
80
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 78

def name_from_id(hashes, id)
  hashes.select { |user| user['id'] == id }.map { |user| user['name'] }.first
end


233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 233

def process_links(hash, links)
  # For each org hash returned by the api, traverse the 'links' entries and add an element to the hash
  # For example, given an org 'puppetlabs' with {"links" => ["buckets": "/api/v2/buckets?org=puppetlabs"]}
  #   add the results of the "buckets" api call to a "buckets" key
  links.each do |k, v|
    next if (k == 'self') || (k == 'write')
    hash[k] = influx_get(v)
  end
rescue StandardError => e
  Puppet.err("Error processing links: #{e.message}")
  Puppet.err(e.backtrace)
  nil
end