Skip to content

Instantly share code, notes, and snippets.

@nquinlan
Last active February 22, 2018 22:25
Show Gist options
  • Save nquinlan/2b0757cdd3daf340da11 to your computer and use it in GitHub Desktop.
Save nquinlan/2b0757cdd3daf340da11 to your computer and use it in GitHub Desktop.
update subusers event notification url
# Runs through all of an Parent's subusers, and updates subusers Event Webhook to use a specified URL
# DOES NOT update Parent.
# Settings push call requires all settings to be defined. This script properly queries each account's settings, and sends them back, with the version updated only.
# Can easily be modified to set all subusers to particular settings.
#
# Requires:
# Credential for Parent account with API permission.
# Folder named 'logs' in same folder as script.
#
# v0.1, 13 Jun 2014, Nick @ SendGrid
# based on https://github.com/jacobmovingfwd/SG_WebAPI_Scripts/blob/master/subuser-event-update.rb
require 'csv'
require 'json'
require 'net/https'
require 'uri'
def log(txt, silent = false)
timestamp = Time.now.strftime("%y%m%d-%H.%M.%S.%L")
txt = txt.to_s
puts "#{timestamp}: " + txt unless silent
@rawLog.write("#{timestamp}: " + txt + "\n")
end
def apiPost(uri)
uri = URI.parse("#{uri}")
http = Net::HTTP.new(uri.host, 443)
http.read_timeout = 5000
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
response = http.request(request)
answer = JSON.parse(response.body)
log(answer, true)
return answer
end
def getSubusers()
subusers = []
answer = apiPost("https://sendgrid.com/apiv2/customer.profile.json?&api_user=#{@api_user}&api_key=#{@api_key}&task=get")
begin
answer.each { |user| subusers << user["username"].to_s}
log("Subusers: " + subusers.to_s)
return subusers
rescue
log("Aborting: " + answer["error"]["message"])
return nil
end
end
def eventSettings(subuser)
event_settings = apiPost("https://sendgrid.com/apiv2/customer.apps.json?api_user=#{@api_user}&api_key=#{@api_key}&name=eventnotify&task=getsettings&user=#{subuser}")
log("Current: {user: #{subuser}, version: #{event_settings["settings"]["version"]}, url: #{event_settings["settings"]["url"]}, processed: #{event_settings["settings"]["processed"]}, dropped: #{event_settings["settings"]["dropped"]}, deferred: #{event_settings["settings"]["deferred"]}, delivered: #{event_settings["settings"]["delivered"]}, bounce: #{event_settings["settings"]["bounce"]}, click: #{event_settings["settings"]["click"]}, open: #{event_settings["settings"]["open"]}, unsubscribe: #{event_settings["settings"]["unsubscribe"]}, spamreport: #{event_settings["settings"]["spamreport"]}}")
return event_settings
end
def subuserUpdate(subusers)
subusers.each do |subuser|
log("")
event_settings = eventSettings(subuser)
if event_settings["settings"]["url"] != @url
apiPost("https://sendgrid.com/apiv2/customer.apps.json?api_user=#{@api_user}&api_key=#{@api_key}&name=eventnotify&task=setup&user=#{subuser}&version=#{event_settings["settings"]["version"]}&url=#{@url}&processed=#{event_settings["settings"]["processed"]}&dropped=#{event_settings["settings"]["dropped"]}&deferred=#{event_settings["settings"]["deferred"]}&delivered=#{event_settings["settings"]["delivered"]}&bounce=#{event_settings["settings"]["bounce"]}&click=#{event_settings["settings"]["click"]}&open=#{event_settings["settings"]["open"]}&unsubscribe=#{event_settings["settings"]["unsubscribe"]}&spamreport=#{event_settings["settings"]["spamreport"]}")
#verify settings by rerunning settings check & logging results
eventSettings(subuser)
else
log("#{subuser} already uses #{@url}. not updating.")
end
end
end
puts "This is the SendGrid script to update all subusers Event Webhook URL to the one provided."
print "\nPlease provide the Parent API User: "
@api_user = gets.chomp
print "Please provide the Parent API Key: "
@api_key = gets.chomp
print "Please provide the URL to update: "
@url = gets.chomp
#open log files
timestamp = Time.now.strftime("%y%m%d-%H.%M.%S")
@rawLog = File.new("logs/subuser-event-update_#{@api_user}_#{timestamp}.log", "a+")
log("api_user: #{@api_user}", true)
subusers = getSubusers()
subuserUpdate(subusers) unless subusers == nil
log("Script done.")
#close log files
@rawLog.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment