Class: Oxidized::Http

Inherits:
Output
  • Object
show all
Defined in:
lib/oxidized/output/http.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Output

#cfg_to_str

Constructor Details

#initializeHttp

Returns a new instance of Http.



5
6
7
8
# File 'lib/oxidized/output/http.rb', line 5

def initialize
  super
  @cfg = Oxidized.config.output.http
end

Instance Attribute Details

#commitrefObject (readonly)

Returns the value of attribute commitref.



3
4
5
# File 'lib/oxidized/output/http.rb', line 3

def commitref
  @commitref
end

Instance Method Details

#setupObject

Raises:



10
11
12
13
14
15
16
17
18
# File 'lib/oxidized/output/http.rb', line 10

def setup
  return unless @cfg.empty?

  CFGS.user.output.http.user = 'Oxidized'
  CFGS.user.output.http.pasword = 'secret'
  CFGS.user.output.http.url = 'http://localhost/web-api/oxidized'
  CFGS.save :user
  raise NoConfig, 'no output http config, edit ~/.config/oxidized/config'
end

#store(node, outputs, opt = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/oxidized/output/http.rb', line 24

def store(node, outputs, opt = {})
  @commitref = nil
  uri = URI.parse @cfg.url
  http = Net::HTTP.new uri.host, uri.port
  # http.use_ssl = true if uri.scheme = 'https'
  req = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/json')
  req.basic_auth @cfg.user, @cfg.password
  req.body = generate_json(node, outputs, opt)
  response = http.request req

  case response.code.to_i
  when 200 || 201
    Oxidized.logger.info "Configuration http backup complete for #{node}"
    p [:success]
  when (400..499)
    Oxidized.logger.info "Configuration http backup for #{node} failed status: #{response.body}"
    p [:bad_request]
  when (500..599)
    p [:server_problems]
    Oxidized.logger.info "Configuration http backup for #{node} failed status: #{response.body}"
  end
end