opsview_rest 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/opsview_rest.rb DELETED
@@ -1,164 +0,0 @@
1
- require 'rest-client'
2
- require 'json'
3
-
4
- class OpsviewRest
5
-
6
- attr_accessor :url, :username, :password, :rest
7
-
8
- def initialize(url, options = {})
9
- options = {
10
- :username => "api",
11
- :password => "changeme",
12
- :connect => true
13
- }.update options
14
-
15
- @url = url
16
- @username = options[:username]
17
- @password = options[:password]
18
- @rest = RestClient::Resource.new("#{@url}/rest/", :headers => { :content_type => 'application/json' })
19
-
20
- login if options[:connect]
21
- end
22
-
23
- def login
24
- response = post('login', { 'username' => @username, 'password' => @password })
25
- @rest.headers[:x_opsview_token] = response['token']
26
- @rest.headers[:x_opsview_username] = @username
27
- response
28
- end
29
-
30
- def logout
31
- delete('login')
32
- end
33
-
34
- def create(options = {})
35
- case options[:type]
36
- when :attribute
37
- require 'opsview_rest/attribute'
38
- OpsviewRest::Attribute.new(self, options)
39
- when :contact
40
- require 'opsview_rest/contact'
41
- OpsviewRest::Contact.new(self, options)
42
- when :host
43
- require 'opsview_rest/host'
44
- OpsviewRest::Host.new(self, options)
45
- when :hostcheckcommand
46
- require 'opsview_rest/hostcheckcommand'
47
- OpsviewRest::Hostcheckcommand.new(self, options)
48
- when :hostgroup
49
- require 'opsview_rest/hostgroup'
50
- OpsviewRest::Hostgroup.new(self, options)
51
- when :hosttemplate
52
- require 'opsview_rest/hosttemplate'
53
- OpsviewRest::Hosttemplate.new(self, options)
54
- when :keyword
55
- require 'opsview_rest/keyword'
56
- OpsviewRest::Keyword.new(self, options)
57
- when :monitoring_server
58
- require 'opsview_rest/monitoring_server'
59
- OpsviewRest::MonitoringServer.new(self, options)
60
- when :notification_method
61
- require 'opsview_rest/notification_method'
62
- OpsviewRest::NotificationMethod.new(self, options)
63
- when :role
64
- require 'opsview_rest/role'
65
- OpsviewRest::Role.new(self, options)
66
- when :servicecheck
67
- require 'opsview_rest/servicecheck'
68
- OpsviewRest::Servicecheck.new(self, options)
69
- when :servicegroup
70
- require 'opsview_rest/servicegroup'
71
- OpsviewRest::Servicegroup.new(self, options)
72
- when :timeperiod
73
- require 'opsview_rest/timeperiod'
74
- OpsviewRest::Timeperiod.new(self, options)
75
- else
76
- raise "Type not implemented yet."
77
- end
78
- end
79
-
80
- def list(options = {})
81
- options = {
82
- :type => "host"
83
- }.update options
84
-
85
- get("config/#{options[:type]}")
86
- end
87
-
88
- def reload
89
- get("reload")
90
- end
91
-
92
- def find(options = {})
93
- options = {
94
- :type => "host",
95
- :name => nil
96
- }.update options
97
-
98
- unless name
99
- raise "Need to specify the name of the object."
100
- else
101
- get("config/#{options[:type]}?s.name=#{options[:name]}", :rows => :all)
102
- end
103
- end
104
-
105
- def purge(options = {})
106
- options = {
107
- :type => "host",
108
- :name => nil
109
- }.update options
110
-
111
- unless name
112
- raise "Need to specify the name of the object."
113
- else
114
- id = find(:type => options[:type], :name => options[:name])[0]["id"]
115
- delete("config/#{options[:type]}/#{id}")
116
- end
117
- end
118
-
119
- def get(path_part, additional_headers = {}, &block)
120
- api_request { @rest[path_part].get(additional_headers, &block) }
121
- end
122
-
123
- def delete(path_part, additional_headers = {}, &block)
124
- api_request { @rest[path_part].delete(additional_headers, &block) }
125
- end
126
-
127
- def post(path_part, payload, additional_headers = {}, &block)
128
- api_request { @rest[path_part].post(payload.to_json, additional_headers, &block) }
129
- end
130
-
131
- def put(path_part, payload, additional_headers = {}, &block)
132
- api_request { @rest[path_part].put(payload.to_json, additional_headers, &block) }
133
- end
134
-
135
- def api_request(&block)
136
- response_body = begin
137
- response = block.call
138
- response.body
139
- rescue RestClient::Exception => e
140
- puts "I have #{e.inspect} with #{e.http_code}"
141
- if e.http_code == 307
142
- get(e.response)
143
- end
144
- e.response
145
- end
146
- parse_response(JSON.parse(response_body))
147
- end
148
-
149
- def parse_response(response)
150
- # We've got an error if there's "message" and "detail" fields
151
- # in the response
152
- if response["message"] and response["detail"]
153
- raise Opsview::Exceptions::RequestFailed, "Request failed: #{response["message"]}, detail: #{response["detail"]}"
154
- # If we have a token, return that:
155
- elsif response["token"]
156
- response
157
- # If we have a list of objects, return the list:
158
- elsif response["list"]
159
- response["list"]
160
- else
161
- response["object"]
162
- end
163
- end
164
- end