From 83300e556e8a2514534fd63db4b773d996814caf Mon Sep 17 00:00:00 2001 From: Arthur Wang Date: Fri, 22 May 2015 17:15:28 +0000 Subject: [PATCH] Add methods + tests for host muting --- lib/dogapi/facade.rb | 12 ++++++++++++ lib/dogapi/v1/monitor.rb | 27 +++++++++++++++++++++++++++ tests/test_monitors.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/lib/dogapi/facade.rb b/lib/dogapi/facade.rb index c822761d..12169de0 100644 --- a/lib/dogapi/facade.rb +++ b/lib/dogapi/facade.rb @@ -386,6 +386,18 @@ def get_all_downtimes(options = {}) @monitor_svc.get_all_downtimes(options) end + # + # HOST MUTING + # + + def mute_host(hostname, options = {}) + @monitor_svc.mute_host(hostname, options) + end + + def unmute_host(hostname) + @monitor_svc.unmute_host(hostname) + end + # # SERVICE CHECKS # diff --git a/lib/dogapi/v1/monitor.rb b/lib/dogapi/v1/monitor.rb index d1014705..46e47acd 100644 --- a/lib/dogapi/v1/monitor.rb +++ b/lib/dogapi/v1/monitor.rb @@ -231,6 +231,33 @@ def get_all_downtimes(options = {}) end end + # + # HOST MUTING + + def mute_host(hostname, options = {}) + begin + params = { + :api_key => @api_key, + :application_key => @application_key + } + + request(Net::HTTP::Post, "/api/#{API_VERSION}/host/#{hostname}/mute", params, options, true) + rescue Exception => e + suppress_error_if_silent e + end + end + def unmute_host(hostname) + begin + params = { + :api_key => @api_key, + :application_key => @application_key + } + + request(Net::HTTP::Post, "/api/#{API_VERSION}/host/#{hostname}/unmute", params, nil, true) + rescue Exception => e + suppress_error_if_silent e + end + end end end diff --git a/tests/test_monitors.rb b/tests/test_monitors.rb index c6ca6b65..7da810dd 100644 --- a/tests/test_monitors.rb +++ b/tests/test_monitors.rb @@ -148,6 +148,40 @@ def test_downtime dog.cancel_downtime(downtime_id) end + def test_host_muting + dog = Dogapi::Client.new(@api_key, @app_key) + hostname = 'test.host' + + # Reset test + dog.unmute_host(hostname) + + message = "Muting this host for a test." + end_ts = Time.now.to_i + 60 * 60 + + res_code, res = dog.mute_host(hostname, :end => end_ts, :message => message) + assert_equal res_code, "200", res_code + assert_equal res["action"], "Muted", res["action"] + assert_equal res["hostname"], hostname, res["hostname"] + assert_equal res["message"], message, res["message"] + assert_equal res["end"], end_ts, res["end"] + + # muting the same host multiple times should fail unless override is true + end_ts2 = end_ts + 60 * 15 + res_code, res = dog.mute_host(hostname, :end => end_ts2) + assert_equal res_code, "400", res_code + + res_code, res = dog.mute_host(hostname, :end => end_ts2, :override => true) + assert_equal res_code, "200", res_code + assert_equal res["action"], "Muted", res["action"] + assert_equal res["hostname"], hostname, res["hostname"] + assert_equal res["end"], end_ts2, res["end"] + + res_code, res = dog.unmute_host(hostname) + assert_equal res_code, "200", res_code + assert_equal res["action"], "Unmuted", res["action"] + assert_equal res["hostname"], hostname, res["hostname"] + end + def test_service_checks dog = Dogapi::Client.new(@api_key, @app_key) status, response = dog.service_check('app.ok', 'app1', 1)