From c88b7daacfae0e83eca4fad5a6b0ce9a38a77e01 Mon Sep 17 00:00:00 2001 From: Jim Salinas Date: Tue, 10 Sep 2013 10:03:33 -0500 Subject: [PATCH] Adding notifications for Rackspace Monitoring --- .../models/monitoring/notification.rb | 42 ++++++++++++ .../models/monitoring/notifications.rb | 30 +++++++++ lib/fog/rackspace/monitoring.rb | 7 ++ .../monitoring/create_notification.rb | 18 +++++ .../monitoring/delete_notification.rb | 16 +++++ .../requests/monitoring/get_notification.rb | 50 ++++++++++++++ .../requests/monitoring/list_notifications.rb | 67 +++++++++++++++++++ .../monitoring/update_notification.rb | 17 +++++ .../models/monitoring/notification_tests.rb | 19 ++++++ .../models/monitoring/notifications_tests.rb | 10 +++ .../requests/monitoring/list_tests.rb | 9 ++- .../requests/monitoring/notification_tests.rb | 46 +++++++++++++ 12 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 lib/fog/rackspace/models/monitoring/notification.rb create mode 100644 lib/fog/rackspace/models/monitoring/notifications.rb create mode 100644 lib/fog/rackspace/requests/monitoring/create_notification.rb create mode 100644 lib/fog/rackspace/requests/monitoring/delete_notification.rb create mode 100644 lib/fog/rackspace/requests/monitoring/get_notification.rb create mode 100644 lib/fog/rackspace/requests/monitoring/list_notifications.rb create mode 100644 lib/fog/rackspace/requests/monitoring/update_notification.rb create mode 100644 tests/rackspace/models/monitoring/notification_tests.rb create mode 100644 tests/rackspace/models/monitoring/notifications_tests.rb create mode 100644 tests/rackspace/requests/monitoring/notification_tests.rb diff --git a/lib/fog/rackspace/models/monitoring/notification.rb b/lib/fog/rackspace/models/monitoring/notification.rb new file mode 100644 index 000000000..3ee89f113 --- /dev/null +++ b/lib/fog/rackspace/models/monitoring/notification.rb @@ -0,0 +1,42 @@ +require 'fog/core/model' +require 'fog/rackspace/models/monitoring/base' + +module Fog + module Rackspace + class Monitoring + class Notification < Fog::Rackspace::Monitoring::Base + + identity :id + + attribute :label + attribute :details + attribute :type + + def params + options = { + 'label' => label, + 'details' => details, + 'type' => type, + } + options.reject {|key, value| value.nil?} + end + + def save + if identity + data = service.update_notification(identity, params) + else + data = service.create_notification(params) + self.id = data.headers['X-Object-ID'] + end + true + end + + def destroy + requires :id + service.delete_notification(id) + end + + end + end + end +end diff --git a/lib/fog/rackspace/models/monitoring/notifications.rb b/lib/fog/rackspace/models/monitoring/notifications.rb new file mode 100644 index 000000000..7e2297e55 --- /dev/null +++ b/lib/fog/rackspace/models/monitoring/notifications.rb @@ -0,0 +1,30 @@ +require 'fog/core/collection' +require 'fog/rackspace/models/monitoring/notification' + +module Fog + module Rackspace + class Monitoring + class Notifications < Fog::Collection + + model Fog::Rackspace::Monitoring::Notification + + attribute :marker + + def all(options={}) + data = service.list_notifications(options).body + marker = data['metadata']['next_marker'] + + load(data['values']) + end + + def get(notification_id) + data = service.get_notification(notification_id).body + new(data) + rescue Fog::Rackspace::Monitoring::NotFound + nil + end + + end + end + end +end diff --git a/lib/fog/rackspace/monitoring.rb b/lib/fog/rackspace/monitoring.rb index 5baa8337c..b3053bc00 100644 --- a/lib/fog/rackspace/monitoring.rb +++ b/lib/fog/rackspace/monitoring.rb @@ -40,6 +40,8 @@ module Fog collection :data_points model :check_type collection :check_types + model :notification + collection :notifications request_path 'fog/rackspace/requests/monitoring' request :list_agent_tokens @@ -52,26 +54,31 @@ module Fog request :list_check_types request :list_overview request :list_notification_plans + request :list_notifications request :get_agent_token request :get_alarm request :get_alarm_example request :get_check request :get_entity + request :get_notification request :create_agent_token request :create_alarm request :create_check request :create_entity + request :create_notification request :update_check request :update_entity request :update_alarm + request :update_notification request :delete_agent_token request :delete_alarm request :delete_check request :delete_entity + request :delete_notification request :evaluate_alarm_example diff --git a/lib/fog/rackspace/requests/monitoring/create_notification.rb b/lib/fog/rackspace/requests/monitoring/create_notification.rb new file mode 100644 index 000000000..6a611b4cf --- /dev/null +++ b/lib/fog/rackspace/requests/monitoring/create_notification.rb @@ -0,0 +1,18 @@ +module Fog + module Rackspace + class Monitoring + class Real + def create_notification(options = {}) + data = options.dup + request( + :body => JSON.encode(data), + :expects => [201], + :method => 'POST', + :path => 'notifications' + ) + end + end + end + end +end + diff --git a/lib/fog/rackspace/requests/monitoring/delete_notification.rb b/lib/fog/rackspace/requests/monitoring/delete_notification.rb new file mode 100644 index 000000000..3746415db --- /dev/null +++ b/lib/fog/rackspace/requests/monitoring/delete_notification.rb @@ -0,0 +1,16 @@ +module Fog + module Rackspace + class Monitoring + class Real + def delete_notification(notification_id) + request( + :expects => [204], + :method => 'DELETE', + :path => "notifications/#{notification_id}" + ) + end + end + end + end +end + diff --git a/lib/fog/rackspace/requests/monitoring/get_notification.rb b/lib/fog/rackspace/requests/monitoring/get_notification.rb new file mode 100644 index 000000000..48c919880 --- /dev/null +++ b/lib/fog/rackspace/requests/monitoring/get_notification.rb @@ -0,0 +1,50 @@ +module Fog + module Rackspace + class Monitoring + class Real + + def get_notification(notification_id) + request( + :expects => [200], + :method => 'GET', + :path => "notifications/#{notification_id}" + ) + end + + end + + class Mock + def get_notification(notification_id) + response = Excon::Response.new + response.status = 200 + response.body = { + "created_at" => 1378783452067, + "details" => { + "address" => "test@test.com" + }, + "id" => "ntnJN3MQrA", + "label" => "my email update test", + "type" => "email", + "updated_at" => 1378784136307 + } + + response.headers = { + "Date"=> Time.now.utc.to_s, + "Content-Type"=>"application/json; charset=UTF-8", + "X-RateLimit-Limit"=>"50000", + "X-RateLimit-Remaining"=>"49627", + "X-RateLimit-Window"=>"24 hours", + "X-RateLimit-Type"=>"global", + "X-Response-Id"=>"j23jlk234jl2j34j", + "X-LB"=>"dfw1-maas-prod-api0", + "Vary"=>"Accept-Encoding", + "Transfer-Encoding"=>"chunked" + } + response + end + end + end + end +end + + diff --git a/lib/fog/rackspace/requests/monitoring/list_notifications.rb b/lib/fog/rackspace/requests/monitoring/list_notifications.rb new file mode 100644 index 000000000..109332aa8 --- /dev/null +++ b/lib/fog/rackspace/requests/monitoring/list_notifications.rb @@ -0,0 +1,67 @@ +module Fog + module Rackspace + class Monitoring + class Real + + def list_notifications(options={}) + request( + :expects => [200], + :method => 'GET', + :path => "notifications", + :query => options + ) + end + + end + + class Mock + def list_notifications(options={}) + account_id = Fog::Mock.random_numbers(6).to_s + server_id = Fog::Rackspace::MockData.uuid + entity_id = Fog::Mock.random_letters(10) + entity_label = Fog::Mock.random_letters(10) + + response = Excon::Response.new + response.status = 200 + response.body = { + "values"=> [ + { + "created_at"=>1378783452067, + "details"=>{ + "address"=>"test@test.com" + }, + "id"=>"ntnJN3MQrA", + "label"=>"my email update test", + "type"=>"email", + "updated_at"=>1378784136307 + } + ], + "metadata" => { + "count" => 1, + "limit" => 100, + "marker" => nil, + "next_marker" => nil, + "next_href" => nil + } + } + response.headers = { + "Date" => Time.now.utc.to_s, + "Content-Type" => "application/json; charset=UTF-8", + "X-RateLimit-Limit" => "50000", + "X-RateLimit-Remaining" => "49627", + "X-RateLimit-Window" => "24 hours", + "X-RateLimit-Type" => "global", + "X-Response-Id" => "j23jlk234jl2j34j", + "X-LB" => "dfw1-maas-prod-api0", + "Vary" => "Accept-Encoding", + "Transfer-Encoding" => "chunked" + } + response.remote_ip = Fog::Rackspace::MockData.ipv4_address + response + end + end + + end + end +end + diff --git a/lib/fog/rackspace/requests/monitoring/update_notification.rb b/lib/fog/rackspace/requests/monitoring/update_notification.rb new file mode 100644 index 000000000..e3fad4cf4 --- /dev/null +++ b/lib/fog/rackspace/requests/monitoring/update_notification.rb @@ -0,0 +1,17 @@ +module Fog + module Rackspace + class Monitoring + class Real + def update_notification(id, options) + request( + :body => JSON.encode(options), + :expects => [204], + :method => 'PUT', + :path => "notifications/#{id}" + ) + end + end + end + end +end + diff --git a/tests/rackspace/models/monitoring/notification_tests.rb b/tests/rackspace/models/monitoring/notification_tests.rb new file mode 100644 index 000000000..08ba1ea39 --- /dev/null +++ b/tests/rackspace/models/monitoring/notification_tests.rb @@ -0,0 +1,19 @@ +Shindo.tests('Fog::Rackspace::Monitoring | notification', ['rackspace','rackspace_monitoring']) do + pending if Fog.mocking? + service = Fog::Rackspace::Monitoring.new + + options = { :label => "fog_#{Time.now.to_i.to_s}", :type => "email", :details => {:address => "test@test.com"} } + + model_tests(service.notifications, options, false) do + + tests('#update').succeeds do + new_label = "new_label_#{Time.now.to_i.to_s}" + @instance.label = new_label + @instance.save + @instance.label = nil # blank out label just to make sure + @instance.reload + returns(new_label) { @instance.label } + end + end + +end diff --git a/tests/rackspace/models/monitoring/notifications_tests.rb b/tests/rackspace/models/monitoring/notifications_tests.rb new file mode 100644 index 000000000..3a7605fd5 --- /dev/null +++ b/tests/rackspace/models/monitoring/notifications_tests.rb @@ -0,0 +1,10 @@ +Shindo.tests('Fog::Rackspace::Monitoring | notifications', ['rackspace','rackspace_monitoring']) do + pending if Fog.mocking? + service = Fog::Rackspace::Monitoring.new + + options = { :label => "fog_#{Time.now.to_i.to_s}", :type => "email", :details => {:address => "test@test.com"} } + collection_tests(service.notifications, options, false) do + + end + +end diff --git a/tests/rackspace/requests/monitoring/list_tests.rb b/tests/rackspace/requests/monitoring/list_tests.rb index dbba57237..0470f3902 100644 --- a/tests/rackspace/requests/monitoring/list_tests.rb +++ b/tests/rackspace/requests/monitoring/list_tests.rb @@ -13,7 +13,7 @@ Shindo.tests('Fog::Rackspace::Monitoring | list_tests', ['rackspace','rackspace_ now = Time.now.to_i SLEEP_TIME= 2 sleep(SLEEP_TIME) unless Fog.mocking? - + tests('success') do tests('#get list of checks').formats(LIST_HEADERS_FORMAT) do account.list_checks(entity_id).data[:headers] @@ -33,6 +33,9 @@ Shindo.tests('Fog::Rackspace::Monitoring | list_tests', ['rackspace','rackspace_ tests('#list notification plans').formats(LIST_HEADERS_FORMAT) do account.list_notification_plans().data[:headers] end + tests('#list notifications').formats(LIST_HEADERS_FORMAT) do + account.list_notifications().data[:headers] + end tests('#get list of data points').formats(LIST_HEADERS_FORMAT) do options = { :points => 1, @@ -50,6 +53,10 @@ Shindo.tests('Fog::Rackspace::Monitoring | list_tests', ['rackspace','rackspace_ account.list_check_types(-1) end # This test has been put on hold due to a bug that incorrectly returns 200 OK to this request + # tests('#fail to list notifications').raises(ArgumentError) do + # account.list_notifications(-1) + # end + # This test has been put on hold due to a bug that incorrectly returns 200 OK to this request #tests('#fail to list metrics').raises(Fog::Rackspace::Monitoring::NotFound) do #account.list_metrics(-1,-1) #end diff --git a/tests/rackspace/requests/monitoring/notification_tests.rb b/tests/rackspace/requests/monitoring/notification_tests.rb new file mode 100644 index 000000000..01f4cd7f5 --- /dev/null +++ b/tests/rackspace/requests/monitoring/notification_tests.rb @@ -0,0 +1,46 @@ +Shindo.tests('Fog::Rackspace::Monitoring | notification_tests', ['rackspace','rackspace_monitoring']) do + account = Fog::Rackspace::Monitoring.new + notification_id = nil + tests('success') do + tests('#create new notification').formats(DATA_FORMAT) do + pending if Fog.mocking? + response = account.create_notification(:label => "Foo", :type => "email", :details => {:address => "test@test.com"}).data + + notification_id = response[:headers]["X-Object-ID"] + response + end + tests('#get notification').formats(LIST_HEADERS_FORMAT) do + account.get_notification(notification_id).data[:headers] + end + tests('#update notification').formats(DATA_FORMAT) do + pending if Fog.mocking? + + options = {:testing => "Bar"} + account.update_notification(notification_id,options).data + end + tests('#delete notification').formats(DELETE_DATA_FORMAT) do + pending if Fog.mocking? + account.delete_notification(notification_id).data + end + end + tests('failure') do + tests('#create new notification(-1)').raises(Fog::Rackspace::Monitoring::BadRequest) do + pending if Fog.mocking? + account.create_notification(:label => "") + end + tests('#get notification(-1)').raises(Fog::Rackspace::Monitoring::NotFound) do + pending if Fog.mocking? + + account.get_notification(-1) + end + tests('#update invalid notification(-1)').raises(Fog::Rackspace::Monitoring::NotFound) do + pending if Fog.mocking? + options = { :testing => "Bar" } + response = account.update_notification(-1,options) + end + tests('#delete notification(-1)').raises(Fog::Rackspace::Monitoring::NotFound) do + pending if Fog.mocking? + account.delete_notification(-1) + end + end +end