Skip to content

Instantly share code, notes, and snippets.

@baskarp
Created January 20, 2012 22:44
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baskarp/1650041 to your computer and use it in GitHub Desktop.
Save baskarp/1650041 to your computer and use it in GitHub Desktop.
Ruby script to create overrides in PagerDuty.
#!/usr/bin/env ruby
# Ruby script to create overrides in PagerDuty.
#
# Copyright (c) 2011, PagerDuty, Inc. <info@pagerduty.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of PagerDuty Inc nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL PAGERDUTY INC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Tested with Ruby 1.8.7
# Requires rubygems, activesupport gem and json
# WARNING: The API is still in beta and not documented -- it's subject to change.
#
require 'rubygems'
require 'net/http'
require "net/https"
require 'uri'
require 'activesupport'
require 'json'
# Monkey patch to get rid of the ssl warning, otherwise it will pollute output
# ref: http://www.5dollarwhitebox.org/drupal/node/64
class Net::HTTP
alias_method :old_initialize, :initialize
def initialize(*args)
old_initialize(*args)
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
end
# Fill in your account subdomain and the email & password of the user account you normally use to log into PagerDuty.
# We'd recommend that you create a user just to do API calls. i.e. API User
# Your subdomain is: http://<subdomain>.pagerduty.com
CREDENTIALS = {
:subdomain => "your-subdomain",
:email => "your-email",
:password => "your-password"
}
def log(message)
puts message
end
def add_override_entry(rotation_id, user_id, start_time, end_time)
success = false
protocol = "https"
base_url_str = "#{protocol}://#{CREDENTIALS[:subdomain]}.pagerduty.com"
path_str = "/api/beta/schedules/#{rotation_id}/overrides"
url = URI.parse(base_url_str + path_str)
req = Net::HTTP::Post.new(url.path, {"Content-Type" => "application/json; charset=UTF-8"})
req.basic_auth(CREDENTIALS[:email], CREDENTIALS[:password])
req_data = {
"override" => {
"start" => start_time.strftime('%Y-%m-%dT%H:%M:%S%z'),
"end" => end_time.strftime('%Y-%m-%dT%H:%M:%S%z'),
"user_id" => user_id
}
}
req.body = JSON.generate(req_data)
log "The request body is: #{req.body}"
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true if protocol == "https"
res = http.start do |http|
res = http.request(req)
log "The response is: #{res.inspect}"
log "The response body is: #{res.body}"
success = (res.code.to_i == 200)
res
end
return success, [res.code.to_i, res.body] # return response code and body for debugging
end
=begin
1. You'll first need to create a schedule before using this script.
2. Next use this tool create create arbitrary overrides in the schedule.
NOTES:
# You can specify the time zone by encoding date string with the time zone information
# (http://en.wikipedia.org/wiki/ISO_8601), otherwise the default calendar time zone will be used
=end
# SCHEDULES
# HOW TO FIND SCHEDULE IDs
# You can find your schedule IDs within PagerDuty’s website itself.
# For instance, under the ‘On-Call Schedules’ tab, click on the schedule that you are interested in.
# The URL for this schedule’s PagerDuty page will contain the rotation ID:
# http://<subdomain>.pagerduty.com/schedules/<schedule_id>
schedule = "PCJI5RZ"
# USERS
# HOW TO FIND USER IDs
# You can find your user IDs within PagerDuty’s website itself.
# For instance, under the ‘Users’ tab, click on the user that you are interested in.
# The URL for this user’s PagerDuty page will contain the user ID:
# http://<subdomain>.pagerduty.com/users/<user_id>
USERS = {
:tim => "P1DK143"
}
puts "Creating overrides..."
# Set the local time zone
# http://en.wikipedia.org/wiki/List_of_tz_database_time_zones for the list of supported values
Time.zone = "America/Los_Angeles"
# put Tim as on-call on every monday for the next six weeks
start_dt = Date.new(2012, 1, 2)
counter = 0
monday = start_dt
while counter < 6
# start time 8am on Monday
start_time = Time.zone.local(monday.year, monday.month, monday.day, 8, 0, 0)
# end time 6pm on Monday
end_time = Time.zone.local(monday.year, monday.month, monday.day, 18, 0, 0)
add_override_entry(schedule, USERS[:tim], start_time, end_time)
monday += 1.week
counter += 1
end
=begin
CURL equivalent:
curl --user your-email:your-password -H "Content-Type: application/json" -X POST -d '{"override":{"start":"2012-01-02T08:00:00-0800","end":"2012-01-02T18:00:00-0800","user_id":"P1DK143"}}' https://your-subdomain.pagerduty.com/api/beta/schedules/PCJI5RZ/overrides
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment