Class: SwhdApi::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/swhd_api.rb

Overview

main api helper class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil, options = {}) ⇒ Manager

Returns a new instance of Manager.



13
14
15
16
17
18
# File 'lib/swhd_api.rb', line 13

def initialize(url = nil, options = {})
  raise SwhdApi::MissingUrl if url.nil? || url.empty?
  # TODO: strip trailing slash if any
  @url = url
  @options = options
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



11
12
13
# File 'lib/swhd_api.rb', line 11

def logger
  @logger
end

#session_idObject (readonly)

Returns the value of attribute session_id.



9
10
11
# File 'lib/swhd_api.rb', line 9

def session_id
  @session_id
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/swhd_api.rb', line 10

def url
  @url
end

Instance Method Details

#connect(credentials = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/swhd_api.rb', line 20

def connect(credentials = {})
  if credentials.key?(:apikey)
    # HACK: remove once SolarWinds get the sessionKey working as documented
    @session_id = credentials[:apikey]
    return

  elsif credentials.key?(:username) && credentials.key?(:password)
    # use username and password
    # @session_id = "2"
  elsif credentials.key?(:techkey)
    # use tech's key
    # @session_id = "3"
  else
    raise MissingCredentials, ':apikey or :techkey or :username and :password'
  end

  results = request('Session', :get, credentials) # { "apiKey" => credentials[:apikey] })
  @session_id = results['sessionKey']
end

#fetch(resource, params = {}) ⇒ Object

fetch all pages of results



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/swhd_api.rb', line 84

def fetch(resource, params = {})
  method = :get
  page = 1

  params[:page] = page
  partial = request(resource, method, params)
  while partial.is_a?(Array) && !partial.empty?
    results ||= []
    results += partial
    page += 1
    params[:page] = page
    partial = request(resource, method, params)
  end

  results = partial unless partial.nil? || partial.empty?
  results
end

#request(resource, method, params = {}, body = {}) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/swhd_api.rb', line 40

def request(resource, method, params = {}, body = {})
  raise NoSession if @session_id.nil? && resource != 'Session'
  raise MissingResource if resource.nil? || resource.empty?

  endpoint = "#{@url}/#{resource}"
  # TODO: log a call to solarwinds and find out why no one can do this (other users on their forums)
  # HACK: for now, use apikey instead of session key
  params['apiKey'] = @session_id
  payload = @options.dup
  payload[:params] = params.reject { |_k, v| v.nil? }

  response =
    case method
    when :post
      payload[:body] = body.to_json
      Typhoeus::Request.post(endpoint, payload)
    when :get
      Typhoeus::Request.get(endpoint, payload)
    when :put
      payload[:body] = body.to_json
      Typhoeus::Request.put(endpoint, payload)
    when :delete
      Typhoeus::Request.delete(endpoint, payload)
    end

  if response.timed_out?
    raise TimedOut, response.body
  elsif response.code == 0
    raise NoResponse, response.body
  elsif response.return_code == :partial_file
    raise PartialFile, response.body
  elsif !response.success?
    raise AuthenticationFailure, response.return_message if response.code == 401
    raise RequestFailed, "Response Code: #{response.code}\n
      Return Code: #{response.return_code} - #{response.return_message}\n
      #{response.body}"
  end

  @logger.debug response.body unless @logger.nil?

  JSON.parse(response.body)
end