Module: PuppetX::Puppetlabs::PuppetlabsInfluxdb

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

.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

.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

Instance Attribute Details

#authObject

Returns the value of attribute auth.



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

def auth
  @auth
end

#bucket_hashObject

Returns the value of attribute bucket_hash.



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

def bucket_hash
  @bucket_hash
end

#dbrp_hashObject

Returns the value of attribute dbrp_hash.



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

def dbrp_hash
  @dbrp_hash
end

#label_hashObject

Returns the value of attribute label_hash.



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

def label_hash
  @label_hash
end

#telegraf_hashObject

Returns the value of attribute telegraf_hash.



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

def telegraf_hash
  @telegraf_hash
end

#user_mapObject

Returns the value of attribute user_map.



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

def user_map
  @user_map
end

Instance Method Details

#get_bucket_infoObject



140
141
142
143
144
145
146
147
148
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 140

def get_bucket_info
  response = influx_get('/api/v2/buckets', params: {})
  return unless response['buckets']

  response['buckets'].each do |bucket|
    process_links(bucket, bucket['links'])
    @bucket_hash << bucket
  end
end

#get_dbrp_infoObject



150
151
152
153
154
155
156
157
158
159
160
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 150

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}", params: {})
    dbrp_response['content'].each do |dbrp|
      @dbrp_hash << dbrp.merge('name' => dbrp['database'])
    end
  end
end

#get_label_infoObject

No links entries for labels other than self



183
184
185
186
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 183

def get_label_info
  response = influx_get('/api/v2/labels', params: {})
  @label_hash = response['labels'] ? response['labels'] : []
end

#get_org_infoObject



130
131
132
133
134
135
136
137
138
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 130

def get_org_info
  response = influx_get('/api/v2/orgs', params: {})
  return unless response['orgs']

  response['orgs'].each do |org|
    process_links(org, org['links'])
    @org_hash << org
  end
end

#get_telegraf_infoObject



162
163
164
165
166
167
168
169
170
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 162

def get_telegraf_info
  response = influx_get('/api/v2/telegrafs', params: {})
  return unless response['configurations']

  response['configurations'].each do |telegraf|
    process_links(telegraf, telegraf['links'])
    @telegraf_hash << telegraf
  end
end

#get_user_infoObject



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

def 
  response = influx_get('/api/v2/users', params: {})
  return unless response['users']

  response['users'].each do |user|
    process_links(user, user['links'])
    @user_map << user
  end
end

#id_from_name(hashes, name) ⇒ Object

Helper methods to map names to internal IDs



65
66
67
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 65

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)


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

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

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

#influx_get(name, _params = {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 73

def influx_get(name, _params = {})
  response = @client.get(URI(@influxdb_uri + name), headers: @auth)
  if response.success?
    JSON.parse(response.body ? response.body : '{}')
    # 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
    {}
  else
    raise Puppet::DevError, "Received HTTP code #{response.code} with message #{response.reason}"
  end
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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 101

def influx_patch(name, body)
  @client.connect(URI(@influxdb_uri)) 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} with message #{response.reason}" unless response.is_a?(Net::HTTPSuccess)

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

#influx_post(name, body) ⇒ Object

Raises:

  • (Puppet::DevError)


86
87
88
89
90
91
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 86

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

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

#influx_put(name, body) ⇒ Object

Raises:

  • (Puppet::DevError)


93
94
95
96
97
98
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 93

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

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

#influx_setupObject



123
124
125
126
127
128
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 123

def influx_setup
  response = influx_get('/api/v2/setup', params: {})
  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



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 39

def init_attrs(resources)
  # TODO: Only one uri per resource type
  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
    @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}"
end

#init_authObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 53

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



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

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

#name_from_id(hashes, id) ⇒ Object



69
70
71
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 69

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


188
189
190
191
192
193
194
195
196
# File 'lib/puppet_x/puppetlabs/influxdb/influxdb.rb', line 188

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, params: {})
  end
end