Class: PuppetX::Patching::OrionClient

Inherits:
HTTPHelper
  • Object
show all
Defined in:
lib/puppet_x/encore/patching/orion_client.rb

Overview

Abstraction of the SolarWinds Orion API

Instance Method Summary collapse

Methods inherited from HTTPHelper

#delete, #execute, #get, #ip?, #net_http_request_class, #post

Constructor Details

#initialize(server, port: 17_778, username: nil, password: nil, ssl: true, ssl_verify: OpenSSL::SSL::VERIFY_NONE, redirect_limit: 10, headers: { 'Content-Type' => 'application/json', }) ⇒ OrionClient

Returns a new instance of OrionClient.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 7

def initialize(server,
               port: 17_778,
               username: nil,
               password: nil,
               ssl: true,
               ssl_verify: OpenSSL::SSL::VERIFY_NONE,
               redirect_limit: 10,
               headers: {
                 'Content-Type' => 'application/json',
               })
  super(username: username,
        password: password,
        ssl: ssl,
        ssl_verify: ssl_verify,
        redirect_limit: redirect_limit,
        headers: headers)
  @server = server
  @port = port
  @scheme = ssl ? 'https' : 'http'
end

Instance Method Details

#get_node(hostname_or_ip, name_property: 'DNS') ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 51

def get_node(hostname_or_ip, name_property: 'DNS')
  field = ip?(hostname_or_ip) ? 'IPAddress' : name_property
  field_list = ['NodeID', 'Uri', 'IPAddress', name_property].uniq
  q = "SELECT #{field_list.join(',')} FROM Orion.Nodes WHERE #{field}=@query_on"
  params = {
    'query_on' => hostname_or_ip,
  }
  query(q, params)
end

#invoke(entity, verb, body: nil) ⇒ Object



46
47
48
49
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 46

def invoke(entity, verb, body: nil)
  resp = post(make_url("Invoke/#{entity}/#{verb}"), body: body)
  JSON.parse(resp.body)
end

#make_url(endpoint) ⇒ Object



28
29
30
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 28

def make_url(endpoint)
  "#{@scheme}://#{@server}:#{@port}/SolarWinds/InformationService/v3/Json/#{endpoint}"
end

#query(query, params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 32

def query(query, params)
  body = {
    'query' => query,
    'parameters' => params,
  }
  resp = post(make_url('Query'), body: body.to_json)
  data = JSON.parse(resp.body)
  if data['results']
    data['results']
  else
    []
  end
end

#resume_alerts(uri_array) ⇒ Object



66
67
68
69
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 66

def resume_alerts(uri_array)
  body = [uri_array].to_json
  invoke('Orion.AlertSuppression', 'ResumeAlerts', body: body)
end

#suppress_alerts(uri_array) ⇒ Object



61
62
63
64
# File 'lib/puppet_x/encore/patching/orion_client.rb', line 61

def suppress_alerts(uri_array)
  body = [uri_array].to_json
  invoke('Orion.AlertSuppression', 'SuppressAlerts', body: body)
end