Class: Papertrail::Connection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/papertrail/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/papertrail/connection.rb', line 15

def initialize(options)
  ssl_options = {
    :verify => options.fetch(:verify_ssl) { OpenSSL::SSL::VERIFY_PEER }
  }

  unless (options[:username] && options[:password]) || options[:token]
    raise ArgumentError, "Must provide a username and password or a token"
  end

  # Make Ubuntu OpenSSL work
  #
  # From: https://bugs.launchpad.net/ubuntu/+source/openssl/+bug/396818
  # "[OpenSSL] does not presume to select a set of CAs by default."
  if File.file?('/etc/ssl/certs/ca-certificates.crt')
    ssl_options[:ca_file] = '/etc/ssl/certs/ca-certificates.crt'
  end

  @connection = Papertrail::HttpClient.new(ssl_options).tap do |conn|
    if options[:username] && options[:password]
      conn.basic_auth(options[:username], options[:password])
    else
      conn.token_auth(options[:token])
    end
  end
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

Instance Method Details

#create_group(name, system_wildcard = nil) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/papertrail/connection.rb', line 63

def create_group(name, system_wildcard = nil)
  group = { :group => { :name => name } }
  if system_wildcard
    group[:group][:system_wildcard] = system_wildcard
  end
  @connection.post("groups.json", group)
end

#find_id_for_group(name) ⇒ Object



47
48
49
50
# File 'lib/papertrail/connection.rb', line 47

def find_id_for_group(name)
  response = @connection.get('groups.json')
  find_id_for_item(response.body, name)
end

#find_id_for_item(items, name_wanted) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/papertrail/connection.rb', line 52

def find_id_for_item(items, name_wanted)
  items.each do |item|
    return item['id'] if item['name'] == name_wanted
  end

  items.each do |item|
    return item['id'] if item['name'] =~ /#{Regexp.escape(name_wanted)}/i
  end
  return nil
end

#find_id_for_source(name) ⇒ Object



41
42
43
44
45
# File 'lib/papertrail/connection.rb', line 41

def find_id_for_source(name)
  response = @connection.get('systems.json')

  find_id_for_item(response.body, name)
end

#join_group(source_name, group_name) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/papertrail/connection.rb', line 83

def join_group(source_name, group_name)
  source_id = find_id_for_source(source_name)
  group_id = find_id_for_group(group_name)
  if source_id && group_id
    @connection.post("systems/#{source_id}/join.json", :group_id => group_id)
  end
end

#query(query = nil, options = {}) ⇒ Object



129
130
131
# File 'lib/papertrail/connection.rb', line 129

def query(query = nil, options = {})
  Papertrail::SearchQuery.new(self, query, options)
end

#register_source(name, *args) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/papertrail/connection.rb', line 91

def register_source(name, *args)
  options = args.last.is_a?(Hash) ? args.pop.dup : {}

  if ip_address = args.shift
    options[:ip_address] = ip_address
  end

  if hostname = args.shift
    options[:hostname] = hostname
  end

  request = {
    :system => {
      :name => name
    }
  }

  if options[:ip_address]
    request[:system][:ip_address] = options[:ip_address]
  end

  if options[:hostname]
    request[:system][:hostname] = options[:hostname]
  end

  if options[:destination_port]
    request[:destination_port] = options[:destination_port]
  end

  @connection.post("systems.json", request)
end

#show_group(name) ⇒ Object



71
72
73
74
75
# File 'lib/papertrail/connection.rb', line 71

def show_group(name)
  if id = find_id_for_group(name)
    @connection.get("groups/#{id}.json").body
  end
end

#show_source(name) ⇒ Object



77
78
79
80
81
# File 'lib/papertrail/connection.rb', line 77

def show_source(name)
  if id = find_id_for_source(name)
    @connection.get("systems/#{id}.json").body
  end
end

#unregister_source(name) ⇒ Object



123
124
125
126
127
# File 'lib/papertrail/connection.rb', line 123

def unregister_source(name)
  if source_id = find_id_for_source(name)
    @connection.delete("systems/#{source_id}.json")
  end
end