class OpsviewRest
Attributes
password[RW]
rest[RW]
url[RW]
username[RW]
Public Class Methods
new(url, options = {})
click to toggle source
# File lib/opsview_rest.rb, line 7 def initialize(url, options = {}) options = { username: 'api', password: 'changeme', connect: true }.update options @url = url @username = options[:username] @password = options[:password] @rest = RestClient::Resource.new("#{@url}/rest/", headers: { content_type: 'application/json' }) login if options[:connect] end
Public Instance Methods
api_request() { || ... }
click to toggle source
# File lib/opsview_rest.rb, line 142 def api_request response_body = begin response = yield response.body rescue RestClient::Exception => e raise "I have #{e.inspect} with #{e.http_code}" get(e.response) if e.http_code == 307 e.response end parse_response(JSON.parse(response_body)) end
create(options = {})
click to toggle source
# File lib/opsview_rest.rb, line 33 def create(options = {}) case options[:type] when :attribute require 'opsview_rest/attribute' OpsviewRest::Attribute.new(self, options) when :contact require 'opsview_rest/contact' OpsviewRest::Contact.new(self, options) when :host require 'opsview_rest/host' OpsviewRest::Host.new(self, options) when :hostcheckcommand require 'opsview_rest/hostcheckcommand' OpsviewRest::Hostcheckcommand.new(self, options) when :hostgroup require 'opsview_rest/hostgroup' OpsviewRest::Hostgroup.new(self, options) when :hosttemplate require 'opsview_rest/hosttemplate' OpsviewRest::Hosttemplate.new(self, options) when :keyword require 'opsview_rest/keyword' OpsviewRest::Keyword.new(self, options) when :monitoring_server require 'opsview_rest/monitoring_server' OpsviewRest::MonitoringServer.new(self, options) when :notificationmethod require 'opsview_rest/notificationmethod' OpsviewRest::NotificationMethod.new(self, options) when :role require 'opsview_rest/role' OpsviewRest::Role.new(self, options) when :servicecheck require 'opsview_rest/servicecheck' OpsviewRest::Servicecheck.new(self, options) when :servicegroup require 'opsview_rest/servicegroup' OpsviewRest::Servicegroup.new(self, options) when :timeperiod require 'opsview_rest/timeperiod' OpsviewRest::Timeperiod.new(self, options) else raise 'Type not implemented yet.' end end
delete(path_part, additional_headers = {}, &block)
click to toggle source
# File lib/opsview_rest.rb, line 130 def delete(path_part, additional_headers = {}, &block) api_request { @rest[path_part].delete(additional_headers, &block) } end
find(options = {})
click to toggle source
# File lib/opsview_rest.rb, line 96 def find(options = {}) options = { type: nil, rows: 'all', searchattribute: nil }.update options options[:searchattribute] = 'name' if options[:searchattribute].nil? if options[:name].nil? raise ArgumentError, 'Need to specify the name of the object.' else get("config/#{options[:type]}?s.#{options[:searchattribute]}=#{options[:name]}&rows=#{options[:rows]}") end end
get(path_part, additional_headers = {}, &block)
click to toggle source
# File lib/opsview_rest.rb, line 126 def get(path_part, additional_headers = {}, &block) api_request { @rest[path_part].get(additional_headers, &block) } end
initiate_reload()
click to toggle source
# File lib/opsview_rest.rb, line 92 def initiate_reload post('reload', {}) end
list(options = {})
click to toggle source
# File lib/opsview_rest.rb, line 79 def list(options = {}) options = { type: 'host', rows: 'all' }.update options get("config/#{options[:type]}?rows=#{options[:rows]}") end
login()
click to toggle source
# File lib/opsview_rest.rb, line 22 def login response = post('login', 'username' => @username, 'password' => @password) @rest.headers[:x_opsview_token] = response['token'] @rest.headers[:x_opsview_username] = @username response end
logout()
click to toggle source
# File lib/opsview_rest.rb, line 29 def logout delete('login') end
parse_response(response)
click to toggle source
# File lib/opsview_rest.rb, line 154 def parse_response(response) # We've got an error if there's "message" and "detail" fields # in the response if response['message'] && response['detail'] raise Opsview::Exceptions::RequestFailed, "Request failed: #{response['message']}, detail: #{response['detail']}" # If we have a token, return that: elsif response['token'] response # If we have a list of objects, return the list: elsif response['list'] response['list'] else response['object'] end end
post(path_part, payload, additional_headers = {}, &block)
click to toggle source
# File lib/opsview_rest.rb, line 134 def post(path_part, payload, additional_headers = {}, &block) api_request { @rest[path_part].post(payload.to_json, additional_headers, &block) } end
purge(options = {})
click to toggle source
# File lib/opsview_rest.rb, line 112 def purge(options = {}) options = { type: 'host', name: nil }.update options if options[:name].nil? raise ArgumentError, 'Need to specify the name of the object.' else id = find(type: options[:type], name: options[:name])[0]['id'] delete("config/#{options[:type]}/#{id}") end end
put(path_part, payload, additional_headers = {}, &block)
click to toggle source
# File lib/opsview_rest.rb, line 138 def put(path_part, payload, additional_headers = {}, &block) api_request { @rest[path_part].put(payload.to_json, additional_headers, &block) } end
reload()
click to toggle source
# File lib/opsview_rest.rb, line 88 def reload get('reload') end